在学校,我必须编写一个程序,将二进制数转换为十进制数并反向转换。我想让它在文本框中输入内容时发生变化
每当我输入一些东西时,我得到的错误是我应该单击由最后一个'Else'触发的文本框中的任何一个。
TextBox1是包含十进制数的文本框,TextBox2包含二进制数。
这是代码:
Public Sub DecBinConverter_Activate()
Dim x As Double
Dim i As Long
Dim Active As String
Active = "TextBox1"
End Sub
Private Sub TextBox1_Change()
Call ConvertDecBin
End Sub
Private Sub TextBox2_Change()
Call ConvertDecBin
End Sub
Public Sub TextBox1_Enter()
Active = "TextBox1"
End Sub
Public Sub TextBox2_Enter()
Active = "TextBox2"
End Sub
Public Sub ConvertDecBin()
If Active = "TextBox1" Then
If TextBox1.Text <> "" Then
If IsNumeric(TextBox1.Text) Then
x = Round(Val(TextBox1.Text), 0)
If x < 10000000000# Then
TextBox1.Text = x
TextBox2.Text = Trim(Str(x Mod 2))
If (x Mod 2) = 0 Then
x = x / 2
Else
x = (x - 1) / 2
End If
Do While x <> 0
TextBox2.Text = Trim(Str(x Mod 2)) & TextBox2.Text
If (x Mod 2) = 0 Then
x = x / 2
Else
x = (x - 1) / 2
End If
Loop
Else
TextBox2.Text = "ERROR: Overload (0-999999999)"
End If
Else
TextBox2.Text = "ERROR: You must fill in a decimal number"
End If
End If
ElseIf Active = "TextBox2" Then
If TextBox2.Text <> "" Then
If IsNumeric(TextBox2.Text) Then
If Len(Str(TextBox2.Text)) > 40 Then
TextBox1.Text = "ERROR: Fill in a number no longer than 40 characters!"
Else
If InStr(Str(TextBox2.Text), "2") Or InStr(Str(TextBox2.Text), "3") Or InStr(Str(TextBox2.Text), "4") Or InStr(Str(TextBox2.Text), "5") Or InStr(Str(TextBox2.Text), "6") Or InStr(Str(TextBox2.Text), "7") Or InStr(Str(TextBox2.Text), "8") Or InStr(Str(TextBox2.Text), "9") Then
TextBox1.Text = "ERROR: You must fill in a binary number!"
Else
x = Round(Val(TextBox2.Text), 0)
TextBox2.Text = x
TextBox1.Text = 0
For i = 1 To Len(Str(x))
If Mid(Str(x), i, 1) = "1" Then
TextBox1.Text = TextBox1.Text * 2 + 1
Else
TextBox1.Text = TextBox1.Text * 2
End If
Next i
End If
End If
Else
TextBox1.Text = "ERROR: You must fill in a binary number!"
End If
End If
Else
MsgBox "An unknown error occurred, please click either of the textboxes.", vbOKOnly, "ERROR"
End If
End Sub
对代码的一些注释:
你不需要有一个"Active"字段,因为你的事件例程
被分配给控件。
当你在一个文本框字段上工作时,"Change"事件就会被执行。
你必须处理它(我找不到链接)。
我建议使用"Option Explicit"。
每个人都有自己的风格;我希望这对你有所帮助:
Option Explicit
' Textbox1 has decimal, textbox2 has binary
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim idec&, s1$
If KeyCode <> 13 Then Exit Sub ' Only act upon "Enter"
s1 = TextBox1.Value
If s1 = "" Then Exit Sub ' blank ok
If Not IsNumeric(s1) Then
MsgBox "Textbox1 is not numeric"
TextBox1.Value = ""
Exit Sub
End If
' -- decimal to binary
idec = Val(s1) ' have decimal number
s1 = "" ' start binary string
Do While idec > 0
If idec Mod 2 = 1 Then s1 = s1 & "1" Else s1 = s1 & "0"
idec = idec 2 ' integer divide
Loop
TextBox2.Value = s1
End Sub
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim idec&, ibin&, ixch&, ch$, s1$
If KeyCode <> 13 Then exit Sub ' Only act upon "Enter"
s1 = TextBox2.Value
If s1 = "" Then Exit Sub ' blank ok
If Not IsNumeric(s1) Then
MsgBox "Textbox2 is not numeric"
TextBox2.Value = ""
Exit Sub
End If
idec = 0 ' start decimal number
For ixch = 1 To Len(s1) ' binary to decimal
ibin = Val(Mid$(s1, ixch, 1)) ' 0 or 1
If ibin < 0 Or ibin > 1 Then
MsgBox "Textbox2 is not binary"
TextBox2.Value = ""
Exit Sub
End If
idec = idec * 2 + ibin
Next ixch
TextBox1.Value = Str$(idec)
End Sub