请问单钮状态切换时为什么鼠标要点二下才能切换?
四个单钮,其上都有“是”、“否”二状态可以切换,但切换时为什么有的钮鼠标要点二下才能切换到另一状态?是程序有什么问题吗?请那位高手指点一下;
代码如下:
Private cmdflag As Byte
Private Sub Command1_Click()
If cmdflag Then
Command1.Caption = "否"
cmdflag = 0
Else
Command1.Caption = "是"
cmdflag = 1
End If
Filenum = FreeFile
Open "flag1.dat" For Output As Filenum
Print #Filenum, Format(cmdflag, 0)
Close Filenum
End Sub
Private Sub Command2_Click()
If cmdflag Then
Command2.Caption = "否"
cmdflag = 0
Else
Command2.Caption = "是"
cmdflag = 1
End If
Filenum = FreeFile
Open "flag2.dat" For Output As Filenum
Print #Filenum, Format(cmdflag, 0)
Close Filenum
End Sub
Private Sub Command3_Click()
If cmdflag Then
Command3.Caption = "否"
cmdflag = 0
Else
Command3.Caption = "是"
cmdflag = 1
End If
Filenum = FreeFile
Open "flag3.dat" For Output As Filenum
Print #Filenum, Format(cmdflag, 0)
Close Filenum
End Sub
Private Sub Command4_Click()
If cmdflag Then
Command4.Caption = "否"
cmdflag = 0
Else
Command4.Caption = "是"
cmdflag = 1
End If
Filenum = FreeFile
Open "flag4.dat" For Output As Filenum
Print #Filenum, Format(cmdflag, 0)
Close Filenum
End Sub
Private Sub Form_Load()
'***************其他次Form_Load用代码****************
Filenum = FreeFile
Open "flag1.dat" For Input As Filenum
cmdflag = Input(1, Filenum)
Close Filenum
If cmdflag Then
Command1.Caption = "是"
Else
Command1.Caption = "否"
End If
Filenum = FreeFile
Open "flag2.dat" For Input As Filenum
cmdflag = Input(1, Filenum)
Close Filenum
If cmdflag Then
Command2.Caption = "是"
Else
Command2.Caption = "否"
End If
Filenum = FreeFile
Open "flag3.dat" For Input As Filenum
cmdflag = Input(1, Filenum)
Close Filenum
If cmdflag Then
Command3.Caption = "是"
Else
Command3.Caption = "否"
End If
Filenum = FreeFile
Open "flag4.dat" For Input As Filenum
cmdflag = Input(1, Filenum)
Close Filenum
If cmdflag Then
Command4.Caption = "是"
Else
Command4.Caption = "否"
End If
End Sub
[解决办法]
你一个标志多处在使用,不出问题才怪。所以出现显示和标志不一致就不奇怪了,也包括你说的有时需要按2次才切换的情况。
最好把思路理清楚了,到底需要多个标志,有没有相互影响等等。
[解决办法]
每个按钮应该对应自己的标志变量。否则自然混乱了。
这种事情,控件的tag属性就是一个合适的可选方法:
Private Sub Command1_Click()
If Command1.tag="" Then
Command1.Caption = "否"
Command1.tag="no"
Else
Command1.Caption = "是"
Command1.tag=""
End If
事实上,就你这个程序来看,也根本不需要其它标志变量,.Caption本身就是标志变量。
Private Sub Command1_Click()
If Command1.Caption = "否"
Command1.Caption = "是"
在这干点啥
Else
Command1.Caption = "否"
在这干点啥
End If
Filenum = FreeFile
Open "flag1.dat" For Output As Filenum
Print #Filenum, Command1.Caption
Close Filenum
End Sub