1、①启动VB新建工程1,在form1的合适位置画出3个Label框、2个Text框以及1个Command按钮(可以预先对各控件的Caption等属性进行修改)。
2、②双击“统计”(即Command1)按钮,在弹出的代码框中编写如下代码:Private Sub Command1_Click() Dim x As String a = Text1.Text b = Text2.Text n = 0 For i = 1 To Len(a) x = Mid(a, i, 1) If x = b Then n = n + 1 Next i Label3.Caption = "字符" & b & "在字符串”" & a & "“中" & vbCrLf & "共出现了" & n & "次。"End Sub这样便达到了检索所有字符、统计某个字符出现次数的需求了。
3、③为了使输入操作更加的方便,我们可以加入SetFocus语句:Private Sub text1_keypress(keyascii As Integer) If keyascii = 13 Then Text2.SetFocusEnd Sub这样当输入光标停留在text1,点击回车(Enter)键时,光标便会跳到text2中,方便对text2进行输入。
4、④同理,对text2进行设置,当光标停留在text2中,按下回车键时,光标将跳到Command上,方便操作:Private Sub text2_keypress(keyascii As Integer) If keyascii = 13 Then Command1.SetFocusEnd Sub
5、⑤最后点击运行试试看功能怎么样吧!