VB 在文本框2中显示文本框1内最长的字符

来源:百度知道 编辑:UC知道 时间:2024/05/19 15:04:59
在文本框1内输入任意字符,以空格间隔.
比如:asdf asdf df asfsa sdfsadfas d
那么,点击按钮后,在文本框2内显示的最长字符是:sdfsadfas

Private Sub Command1_Click()
Dim i As Integer
Dim s As String
Dim t As String
i = 0
s = ""
t = ""
For i = 1 To Len(Text1)
If Mid(Text1, i, 1) = " " Then
If Len(t) > Len(s) Then
s = t
End If
t = ""
Else
t = t & Mid(Text1, i, 1)
End If
Next i
If Len(t) > Len(s) Then
s = t
t = ""
End If
Text2 = s

End Sub

Private Sub Command1_Click()
s = Text1.Text
s = Split(s, " ")
s1 = ""
For i = 0 To UBound(s) - 1
If Len(s1) < Len(s(i)) Then s1 = s(i)
Next i
Text2.Text = s1
End Sub

'窗体上画,两个文本框 Text1和Text2,一个按钮Command1,代码如下

Option Explicit

Private Sub Command1_Click()
    Dim tmp
    Dim i As Long
    Dim iMax As Long