如何在输入文本框的字符后增加空格,最大长度为

时间:2019-05-24 08:45:03

标签: vb.net

`导入System.Net 导入System.Net.Sockets 导入System.Threading

公共类表格1

Private client As TCPControl

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    GroupBox2.Enabled = False
    GroupBox3.Enabled = False
    send.Enabled = False
    send.BackColor = Color.Gray
    ComboBox1.Enabled = False

    porttextbox.Text = "1025"

End Sub


Private Sub connect_Click(sender As Object, e As EventArgs) Handles connect.Click
    Label24.Text = row1.Text.Length
    Label25.Text = row2.Text.Length
    Label26.Text = row3.Text.Length
    Label27.Text = row4.Text.Length
    Label28.Text = row5.Text.Length
    Label29.Text = row6.Text.Length
    Label30.Text = row7.Text.Length
    Label31.Text = row8.Text.Length
    Label32.Text = row9.Text.Length
    Label33.Text = row10.Text.Length
    Label34.Text = row11.Text.Length
    Label35.Text = row12.Text.Length
    Label36.Text = row13.Text.Length
    Label37.Text = row14.Text.Length
    Label38.Text = row15.Text.Length
    Label39.Text = row16.Text.Length
    Label40.Text = row17.Text.Length
    Label41.Text = row18.Text.Length
    Label42.Text = row19.Text.Length
    Label43.Text = row20.Text.Length
    If iptextbox.Text = "" Or porttextbox.Text = "" Then
        MsgBox("Please Enter valid IP or PORT NO.")
    Else
        Try
            client = New TCPControl(iptextbox.Text, porttextbox.Text)
            GroupBox2.Enabled = True
            GroupBox3.Enabled = True
            send.Enabled = False
            ComboBox1.Enabled = True

            iptextbox.ReadOnly = True
            porttextbox.ReadOnly = True

            If client.client.Connected = True Then
                MsgBox("CONNECTED" & vbNewLine & "WRITE TEXT")

            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If
End Sub
Private Sub check()
    If myinterval > 1 Then

        Button1.Enabled = True
        disconnect.Enabled = True

    End If

End Sub

Private Sub send_Click(sender As Object, e As EventArgs) Handles send.Click
    send.Enabled = True
    GroupBox2.Enabled = False
    GroupBox3.Enabled = False
    If client.client.Connected = True Then
        Timer1.Start()

        If BackgroundWorker1.IsBusy = False Then

            BackgroundWorker1.RunWorkerAsync()
        End If
    Else
        MsgBox("PLEASE CONNECT")

    End If

End Sub

Sub mydelay()
    Dim icount As Long = 1
    For icount = 1 To 10
        icount = icount + 1
    Next
End Sub

Dim icount As Long = 0
Dim len As Integer
Dim c As Integer
Dim tcount As Long = 26


Private Sub page1()

    If client.client.Connected = True Then
        If BackgroundWorker1.IsBusy = False Then
            BackgroundWorker1.RunWorkerAsync()
        End If

        'client.Send(row1.Text & vbNewLine)
        'If row1.Text.Length < 26 Then       ' For comparing character in textbox and adding spaces if less than maximum size of TB - 24/5/2019(10:14 am
        'For i As Integer = 1 To row1.MaximumSize
        'For i As Integer = row1.Text.Length + 1 To 26
        'row1.AppendText(" ")

        'Next
        'End If
        'If row1.Text.Length > tcount Then
        'row1.Text = row1.Text.Substring(0, tcount)
        'row1.Text = Trim(row1.Text)
        'Else

        'row1.Text = row1.Text.PadRight(tcount, " ")
        'End If

        If row1.Text.Length < 26 Then
            Dim SelStart As Integer = row1.SelectionStart
            row1.Text = row1.Text.PadRight(26).Substring(0, 26)
            row1.Select(SelStart, 0)


        End If
        client.Send(row1.Text)

        'If row2.Text.Length > tcount Then
        'row2.Text = Trim(row2.Text)
        'row2.Text = row2.Text.Substring(0, tcount)
        'Else
        '   For i As Integer = row2.Text.Length + 1 To 26
        '  row2.AppendText(" ")
        'Else
        ' Next
        'row2.Text = row2.Text.PadRight(tcount, " ")
        'row2.Text = row2.Text.Replace(" ", "")
        'End If
        If row2.Text.Length < 26 Then
            Dim SelStart As Integer = row2.SelectionStart
            row2.Text = row2.Text.PadRight(26).Substring(0, 26)
            row2.Select(SelStart, 0)


        End If
        client.Send(row2.Text)




        'If row2.TextLength < 26 Then
        'For i As Integer = row2.Text.Length + 1 To 26
        'row2.AppendText(" ")
        '
        'Next
        'End If
        'If row3.Text.Length > tcount Then
        'row3.Text = row3.Text.Substring(0, tcount)
        'row3.Text = Trim(row3.Text)
        'Else

        'row3.Text = row3.Text.PadRight(tcount, " ")
        'End If

        If row3.Text.Length < 26 Then
            Dim SelStart As Integer = row3.SelectionStart
            row3.Text = row3.Text.PadRight(26).Substring(0, 26)
            row3.Select(SelStart, 0)


        End If


        client.Send(row3.Text)

        'If row4.TextLength <= 26 Then
        'For i As Integer = row4.Text.Length + 1 To 26
        ''row4.AppendText(" ")

        ''Next
        'End If
        If row4.Text.Length > tcount Then
            row4.Text = row4.Text.Substring(0, row4.MaxLength)
        Else
            row4.Text = row4.Text.PadRight(row4.MaxLength, " ")
        End If

        client.Send(row4.Text)

        client.Send(row5.Text & vbNewLine + row6.Text & vbNewLine + row7.Text & vbNewLine + row8.Text & vbNewLine + row9.Text & vbNewLine + vbTab & row10.Text & vbNewLine)

        End If

End Sub

Private Sub page2()

    If client.client.Connected = True Then
        If BackgroundWorker1.IsBusy = False Then

            BackgroundWorker1.RunWorkerAsync()
        End If

        client.Send(row11.Text & vbNewLine)
        client.Send(row12.Text & vbNewLine)
        client.Send(row13.Text & vbNewLine + row14.Text & vbNewLine + row15.Text & vbNewLine + row16.Text & vbNewLine + row17.Text & vbNewLine + row18.Text & vbNewLine + row19.Text & vbNewLine + vbTab & row20.Text & vbNewLine)
        'Label44.Text = "YOU CAN EDIT OR CLOSE"

    End If

End Sub

Dim myinterval As Long

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    send.Enabled = True

    send.BackColor = Color.Green

    If Me.ComboBox1.SelectedIndex = 0 Then
        myinterval = 6000
    ElseIf Me.ComboBox1.SelectedIndex = 1 Then
        myinterval = 120000
    ElseIf Me.ComboBox1.SelectedIndex = 2 Then
        myinterval = 180000
    ElseIf Me.ComboBox1.SelectedIndex = 3 Then
        myinterval = 240000
    ElseIf Me.ComboBox1.SelectedIndex = 4 Then
        myinterval = 300000
    ElseIf Me.ComboBox1.SelectedIndex = 5 Then
        myinterval = 360000

    End If

End Sub

Dim counter As Integer = 0

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    CheckForIllegalCrossThreadCalls = False

    'counter += 1
    For counter As Integer = 0 To myinterval - 1
        If client.client.Connected = True Then
            If BackgroundWorker1.CancellationPending = True Then
                e.Cancel = True
                Exit For
            End If

            'counter = 0
            page1()
            Thread.Sleep(myinterval)
            page2()
            Thread.Sleep(myinterval)
            'Else
            'counter = 0
        End If
        Timer1.Interval = myinterval
        myinterval = myinterval + 1
    Next
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    If BackgroundWorker1.IsBusy = False Then

        BackgroundWorker1.RunWorkerAsync()
    Else

        If BackgroundWorker1.WorkerSupportsCancellation Then

            BackgroundWorker1.CancelAsync()
        End If

    End If

    row10.ScrollToCaret()

    'If Label49.Location.X + Label49.Width < 0 Then
    'Label49.Location = New Point(Label49.Location.X, Label49.Location.Y = 1)
    'Else
    'Label49.Location = New Point(Label49.Location.X - 3, Label49.Location.Y)
    'End If

    'Label47.Text = row10.Text
    'Label48.Text = row20.Text
    'If Label47.Right = 0 Then
    'Label47.Left = Width
    'Else
    'Label47.Left -= 1
    'End If
    'If Label48.Right = 0 Then
    'Label48.Left = Width
    'Else
    'Label48.Left -= 1
    'End If
    'client.Send(Label47.Text)
    'client.Send(Label48.Text)
End Sub

Private Sub disconnect_Click(sender As Object, e As EventArgs) Handles disconnect.Click
    If iptextbox.Text = "" Or porttextbox.Text = "" Then
        MsgBox("Connection not established")
    Else
        Try
            client.client.Close()
            client.Datastream.Flush()
            GroupBox2.Enabled = False
            GroupBox3.Enabled = False
            send.Enabled = False
            send.BackColor = Color.Gray
            ComboBox1.Enabled = False

            iptextbox.ReadOnly = False
            porttextbox.ReadOnly = False

            MsgBox("Connection Close")

            Timer1.Stop()

        Catch ex As Exception
            MsgBox("Disconnected")
        End Try
    End If
End Sub


Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    Try
        If client.client.Connected = True Then
            client.Datastream.Close()
            client.client.Close()

        End If

    Catch ex As Exception
        MsgBox("Close the Utility")
    End Try

End Sub


Private Sub row1_TextChanged(sender As Object, e As EventArgs) Handles row1.TextChanged
    If row1.Text.Length < 26 Then
        Dim SelStart As Integer = row1.SelectionStart
        row1.Text = row1.Text.PadRight(26).Substring(0, 26)
        row1.Select(SelStart, 0)


    End If
    Label24.Text = row1.Text.length
End Sub

Private Sub row2_TextChanged(sender As Object, e As EventArgs) Handles row2.TextChanged
    Label25.Text = row2.Text.Length
End Sub

Private Sub row3_TextChanged(sender As Object, e As EventArgs) Handles row3.TextChanged
    Label26.Text = row3.Text.Length
End Sub

Private Sub row4_TextChanged(sender As Object, e As EventArgs) Handles row4.TextChanged
    Label27.Text = row4.Text.Length
End Sub

Private Sub row5_TextChanged(sender As Object, e As EventArgs) Handles row5.TextChanged
    Label28.Text = row5.Text.Length
End Sub


Private Sub row6_TextChanged(sender As Object, e As EventArgs) Handles row6.TextChanged
    Label29.Text = row6.Text.Length
End Sub

Private Sub row7_TextChanged(sender As Object, e As EventArgs) Handles row7.TextChanged
    Label30.Text = row7.Text.Length
End Sub

Private Sub row8_TextChanged(sender As Object, e As EventArgs) Handles row8.TextChanged
    Label31.Text = row8.Text.Length
End Sub

Private Sub row9_TextChanged(sender As Object, e As EventArgs) Handles row9.TextChanged
    Label32.Text = row9.Text.Length
End Sub

Private Sub row10_TextChanged(sender As Object, e As EventArgs) Handles row10.TextChanged
    Label33.Text = row10.Text.Length

End Sub

Private Sub row11_TextChanged(sender As Object, e As EventArgs) Handles row11.TextChanged
    Label34.Text = row11.Text.Length
End Sub

Private Sub row12_TextChanged(sender As Object, e As EventArgs) Handles row12.TextChanged
    Label35.Text = row12.Text.Length
End Sub

Private Sub row13_TextChanged(sender As Object, e As EventArgs) Handles row13.TextChanged
    Label36.Text = row13.Text.Length
End Sub

Private Sub row14_TextChanged(sender As Object, e As EventArgs) Handles row14.TextChanged
    Label37.Text = row14.Text.Length
End Sub

Private Sub row15_TextChanged(sender As Object, e As EventArgs) Handles row15.TextChanged
    Label38.Text = row15.Text.Length
End Sub

Private Sub row16_TextChanged(sender As Object, e As EventArgs) Handles row16.TextChanged
    Label39.Text = row16.Text.Length
End Sub

Private Sub row17_TextChanged(sender As Object, e As EventArgs) Handles row17.TextChanged
    Label40.Text = row17.Text.Length
End Sub

Private Sub row18_TextChanged(sender As Object, e As EventArgs) Handles row18.TextChanged
    Label41.Text = row18.Text.Length
End Sub

Private Sub row19_TextChanged(sender As Object, e As EventArgs) Handles row19.TextChanged
    Label42.Text = row19.Text.Length
End Sub

Private Sub row20_TextChanged(sender As Object, e As EventArgs) Handles row20.TextChanged
    Label43.Text = row20.Text.Length
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    GroupBox2.Enabled = True
    GroupBox3.Enabled = True

    If client.client.Connected = True Then
        Try
            client.Datastream.Flush()
            client.Datastream.Close()
            Timer1.Stop()

        Catch ex As Exception
            MsgBox(" Cannot close")
        End Try
    End If
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles edit.Click

    Password.Show()
    If iptextbox.Text = "" Or porttextbox.Text = "" Then
        MsgBox("Please Enter valid IP or PORT NO.")
    Else
        Try
            client = New TCPControl(iptextbox.Text, porttextbox.Text)
            GroupBox2.Enabled = True

            GroupBox3.Enabled = True
            send.Enabled = False
            ComboBox1.Enabled = True

            iptextbox.ReadOnly = True
            porttextbox.ReadOnly = True

            If client.client.Connected = True Then


            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If


    Me.Enabled = False


End Sub

结束班级

`文本框的总长度为26个字符。 当我输入字符<26时,应该添加空格。 有什么解决办法吗? row1 abcdefghijklmnop .......最多26 row2 abcdefghijklmnop .......最多26 row3 abcdefghijklmnop .......最多26 .. 。 。 .. 。 。 直到第10行 这是预期的输出

实际输出: row1 abcdefghijklmnop .......最多26 row2 abcdefghijklmnop .......最多25和1个空格 row3 2个空格+ abcdefghijklmnop .......最多24个

请帮助我

2 个答案:

答案 0 :(得分:0)

您必须padrighz

Dim str as string = "sometext"
Dim pad as char = " "
str = str.PadRight(26, pad)

答案 1 :(得分:0)

尝试一下

Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
    If TextBox1.Text.Length < 26 Then
        Dim SelStart As Integer = TextBox1.SelectionStart
        TextBox1.Text = TextBox1.Text.PadRight(26).Substring(0, 26)
        TextBox1.Select(SelStart, 0)
    End If
End Sub