VB问题,双分支结构

来源:百度知道 编辑:UC知道 时间:2024/09/23 19:19:02
两个文本框text1,text2
在text1中输入字母,text2中会自动动态计数,直到text1中出现"."时停止计数
以下是我的代码,请纠正错误
Private Sub Text2_Change()
If Right$(Text1.Text, 1) <> "." Then
n = n + 1
Text1.Text = n
Print n
Else
Text1.Enabled = False
End If
End Sub
dim n as integer
Private Sub Text1_Change()
If Right$(Text1.Text, 1) <> "." Then
n = n + 1
Text1.Text = n
Else
Text1.Enabled = False
End If
End Sub
书上这么写的

Dim n As Integer
Private Sub Text1_Change()
If Right$(Text1.Text, 1) <> "." Then
n = n + 1
Text2.Text = n
Else
Text2.Enabled = False
End If
End Sub

答案补充:不是你抄错了就是杂牌书,Text1的Change事件里面再对Text1赋值,早晚要堆栈溢出的啊。
=============
看不懂你的代码。你要求在Text1中输入,那怎么事件是Text2的Change?在Text2中计数,用Print干吗?还有那个n干什么用?让Text1=n 更是不理解。所以不好用你的思路修改,下面这个修改之后的代码尽量保留了你的代码部分。
=============
Private Sub Text1_Change()
If Right$(Text1.Text, 1) <> "." Then
Text2.Text = Len(Text1.Text)
Else
Text1.Enabled = False
End If
End Sub

首先,你的代码这样计数是不太好的,如果输入文字后突然按删除键就会无效。
如果要统计n的数量,请用静态变量声明。你的代码修改:
Private Sub Text2_Change()
Static n As Integer
If Right$(Text2.Text, 1) <> "." Then
n = n + 1
Text1.Text = n
Print n
Else
Text1.Enabled = False
End If
End Sub

而我建议给你的代码是
Private Sub Text2_Change()
If Right$(Text2.T