Vb.net 如何在文本框中输入最大长度的字符后添加空格

Vb.net 如何在文本框中输入最大长度的字符后添加空格,vb.net,Vb.net,`导入系统.Net 导入System.Net.Sockets 导入系统线程 公开课表格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.Bac

`导入系统.Net 导入System.Net.Sockets 导入系统线程

公开课表格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
专用客户端作为TCPControl
私有子表单1_Load(发送方作为对象,e作为事件参数)处理MyBase.Load
GroupBox2.Enabled=False
GroupBox3.Enabled=False
send.Enabled=False
send.BackColor=Color.Gray
ComboBox1.Enabled=False
porttextbox.Text=“1025”
端接头
私有子连接\单击(发送者作为对象,e作为事件参数)处理连接。单击
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
如果iptextbox.Text=“”或porttextbox.Text=“”,则
MsgBox(“请输入有效的IP或端口号”)
其他的
尝试
客户端=新的TCPControl(iptextbox.Text、porttextbox.Text)
GroupBox2.Enabled=True
GroupBox3.Enabled=True
send.Enabled=False
ComboBox1.Enabled=True
iptextbox.ReadOnly=True
porttextbox.ReadOnly=True
如果client.client.Connected=True,则
MsgBox(“已连接”&vbNewLine&“写入文本”)
如果结束
特例
MsgBox(例如消息)
结束尝试
如果结束
端接头
专用子检查()
如果myinterval>1,则
按钮1.已启用=真
disconnect.Enabled=True
如果结束
端接头
私有子发送\单击(发送者作为对象,e作为事件参数)处理发送。单击
send.Enabled=True
GroupBox2.Enabled=False
GroupBox3.Enabled=False
如果client.client.Connected=True,则
Timer1.Start()
如果BackgroundWorker1.IsBusy=False,则
BackgroundWorker1.RunWorkerAsync()
如果结束
其他的
MsgBox(“请连接”)
如果结束
端接头
次mydelay()
Dim I长度=1时的计数
对于icount=1到10
icount=icount+1
下一个
端接头
Dim I长度=0时的计数
作为整数的Dim len
作为整数的Dim c
变暗t长度=26时的计数
专用子页1()
如果client.client.Connected=True,则
如果BackgroundWorker1.IsBusy=False,则
BackgroundWorker1.RunWorkerAsync()
如果结束
'client.Send(row1.Text&vbNewLine)
“如果row1.Text.Length<26则”用于比较文本框中的字符,并在小于TB的最大大小时添加空格-2019年5月24日(上午10:14
'对于i作为整数=1到行1.MaximumSize
'对于整数形式的i=row1.Text.Length+1到26
'第1行。追加文本(“”)
”“接着呢
"完"
'如果行1.Text.Length>t则
'row1.Text=row1.Text.Substring(0,t计数)
'row1.Text=Trim(row1.Text)
”“否则呢
'row1.Text=row1.Text.PadRight(tcount,“”)
"完"
如果row1.Text.Length<26,则
Dim SelStart As Integer=row1.SelectionStart
row1.Text=row1.Text.PadRight(26).子字符串(0,26)
行1.选择(SelStart,0)
如果结束
client.Send(row1.Text)
'如果row2.Text.Length>t计数,则
'row2.Text=Trim(row2.Text)
'row2.Text=row2.Text.Substring(0,t计数)
”“否则呢
'对于整数形式的i=row2.Text.Length+1到26
'第2行。追加文本(“”)
”“否则呢
”“接着呢
'row2.Text=row2.Text.PadRight(tcount,“”)
'row2.Text=row2.Text.Replace(“,”)
"完"
如果row2.Text.Length<26,则
Dim SelStart As Integer=row2.SelectionStart
row2.Text=row2.Text.PadRight(26).子字符串(0,26)
第2行。选择(SelStart,0)
如果结束
client.Send(row2.Text)
'如果row2.TextLength<26,则
'对于整数形式的i=row2.Text.Length+1到26
'第2行。追加文本(“”)
'
”“接着呢
"完"
'如果行3.Text.Length>t则
'row3.Text=row3.Text.Substring(0,t计数)
'row3.Text=Trim(row3.Text)
”“否则呢
'row3.Text=row3.Text.PadRight(tcount,“”)
"完"
如果row3.Text.Length<26,则
Dim SelStart As Integer=row3.SelectionStart
row3.Text=row3.Text.PadRight(26).子字符串(0,26)
第3行。选择(SelStart,0)
如果结束
client.Send(第3行文本)
'如果row4.TextLength计算,则
row4.Text=row4.Text.Substring(0,row4.MaxLength)
其他的
row4.Text=row4.Text.PadRight(row4.MaxLength,“”)
如果结束
client.Send(第4行文本)
client.Send(row5.Text&vbNewLine+row6.Text&vbNewLine+row7.Text&vbNewLine+row8.Text&vbNewLine+row9.Text&vbNewLine+vbTab&row10.Text&vbNewLine)
如果结束
端接头
专用子页2()
如果client.client.Connected=True,则
如果BackgroundWorker1.IsBusy=False,则
BackgroundWorker1.RunWorkerAsync()
如果结束
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=“您可以编辑或关闭”
如果结束
端接头
我的时间间隔一样长
私有子Combox1\u SelectedIndexChanged(发送方作为对象,e作为事件参数)处理Combox1.Se
Dim str as string = "sometext"
Dim pad as char = " "
str = str.PadRight(26, pad)
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