找到按钮状态代码,但还有不足之处,请那位高手优化一下:
问题:四个钮状态0-1需要建四个TXT文本文件,能否优化成一个TXT文件?代码如何写?
代码如下:
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