高手帮忙修改VB代码
下面是一段VB计算器的代码,但是不知道为什么,代码运行后,计算器上的9按出来会变成10,高手帮帮忙,谢谢了~~~~PrivateDeclareFunctionSetWin...
下面是一段VB计算器的代码,但是不知道为什么,代码运行后,计算器上的9按出来会变成10,高手帮帮忙,谢谢了~~~~
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const HWND_TOPMOST = -1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Sub Command1_Click(Index As Integer)
If txtlabel.Text = "" Then
txt1.Text = txt1.Text + Trim(Str(Index))
Else
txt2.Text = txt2.Text + Trim(Str(Index))
End If
End Sub
Private Sub m2_Click()
frmAbout.Show 1
End Sub
Private Sub Command2_Click(Index As Integer)
Select Case Index
Case "0"
txtlabel.Text = "+"
Case "1"
txtlabel.Text = "-"
Case "2"
txtlabel.Text = "*"
Case "3"
txtlabel.Text = "/"
End Select
End Sub
Private Sub Command3_Click()
Dim MyResult As Double
Select Case txtlabel.Text
Case "+"
MyResult = Val(txt1.Text) + Val(txt2.Text)
Case "-"
MyResult = Val(txt1.Text) - Val(txt2.Text)
Case "*"
MyResult = Val(txt1.Text) * Val(txt2.Text)
Case "/"
MyResult = Val(txt1.Text) / Val(txt2.Text)
End Select
txtlabel.Text = ""
txt2.Text = ""
txt1.Text = ""
txtresult.Text = MyResult
txtHex.Text = Hex(MyResult)
txtOct.Text = Oct(MyResult)
End Sub
Private Sub Command4_Click()
txt1.Text = ""
txtlabel.Text = ""
txt2.Text = ""
End Sub
Private Sub Command5_Click()
Unload Me
End Sub
Private Sub Command6_Click(Index As Integer)
If txtlabel.Text = "" Then
txt1.Text = -1 * Val(txt1.Text)
Else
txt2.Text = -1 * Val(txt2.Text)
End If
End Sub
Private Sub Command7_Click(Index As Integer)
If txtlabel.Text = "" Then
txt1.Text = txt1.Text & "."
Else
txt2.Text = txt2.Text & "."
End If
End Sub
Private Sub Form_Load()
SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
txt1.Text = ""
txtlabel.Text = ""
txt2.Text = ""
End Sub
Private Sub txtresult_KeyDown(KeyCode As Integer, Shift As Integer)
KeyCode = 0
End Sub
Private Sub txtresult_KeyPress(KeyAscii As Integer)
KeyAscii = 0
End Sub
还有,代码中,我只把计算结果 转换成八和十六进制数,要是我想转换成十进制,代码应该怎么编..? 谢谢帮忙。 展开
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const HWND_TOPMOST = -1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Sub Command1_Click(Index As Integer)
If txtlabel.Text = "" Then
txt1.Text = txt1.Text + Trim(Str(Index))
Else
txt2.Text = txt2.Text + Trim(Str(Index))
End If
End Sub
Private Sub m2_Click()
frmAbout.Show 1
End Sub
Private Sub Command2_Click(Index As Integer)
Select Case Index
Case "0"
txtlabel.Text = "+"
Case "1"
txtlabel.Text = "-"
Case "2"
txtlabel.Text = "*"
Case "3"
txtlabel.Text = "/"
End Select
End Sub
Private Sub Command3_Click()
Dim MyResult As Double
Select Case txtlabel.Text
Case "+"
MyResult = Val(txt1.Text) + Val(txt2.Text)
Case "-"
MyResult = Val(txt1.Text) - Val(txt2.Text)
Case "*"
MyResult = Val(txt1.Text) * Val(txt2.Text)
Case "/"
MyResult = Val(txt1.Text) / Val(txt2.Text)
End Select
txtlabel.Text = ""
txt2.Text = ""
txt1.Text = ""
txtresult.Text = MyResult
txtHex.Text = Hex(MyResult)
txtOct.Text = Oct(MyResult)
End Sub
Private Sub Command4_Click()
txt1.Text = ""
txtlabel.Text = ""
txt2.Text = ""
End Sub
Private Sub Command5_Click()
Unload Me
End Sub
Private Sub Command6_Click(Index As Integer)
If txtlabel.Text = "" Then
txt1.Text = -1 * Val(txt1.Text)
Else
txt2.Text = -1 * Val(txt2.Text)
End If
End Sub
Private Sub Command7_Click(Index As Integer)
If txtlabel.Text = "" Then
txt1.Text = txt1.Text & "."
Else
txt2.Text = txt2.Text & "."
End If
End Sub
Private Sub Form_Load()
SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
txt1.Text = ""
txtlabel.Text = ""
txt2.Text = ""
End Sub
Private Sub txtresult_KeyDown(KeyCode As Integer, Shift As Integer)
KeyCode = 0
End Sub
Private Sub txtresult_KeyPress(KeyAscii As Integer)
KeyAscii = 0
End Sub
还有,代码中,我只把计算结果 转换成八和十六进制数,要是我想转换成十进制,代码应该怎么编..? 谢谢帮忙。 展开
展开全部
9的数字键的是command1(10)吧,你把
Private Sub Command1_Click(Index As Integer)
If txtlabel.Text = "" Then
txt1.Text = txt1.Text + Trim(Str(Index))
Else
txt2.Text = txt2.Text + Trim(Str(Index))
End If
End Sub
中的Trim(Str(Index)改作:
Trim(command1(Index).caption)
要把oct,或hex转成dec进制可试试下面的自定义函数.
Public Function oct2dec(str As String)
oct2dec = 0
For i = Len(str) To 1 Step -1
oct2dec = oct2dec + CInt(Mid(str, i, 1)) * 8 ^ (Len(str) - i)
Next i
End Function
Public Function hex2dec(str As String)
hex2dec = 0
For i = Len(str) To 1 Step -1
b = Mid(str, i, 1)
If Not IsNumeric(b) Then b = CStr(Asc(UCase(b)) - 55)
hex2dec = hex2dec + CInt(b) * 16 ^ (Len(str) - i)
Next i
End Function
Private Sub Command1_Click(Index As Integer)
If txtlabel.Text = "" Then
txt1.Text = txt1.Text + Trim(Str(Index))
Else
txt2.Text = txt2.Text + Trim(Str(Index))
End If
End Sub
中的Trim(Str(Index)改作:
Trim(command1(Index).caption)
要把oct,或hex转成dec进制可试试下面的自定义函数.
Public Function oct2dec(str As String)
oct2dec = 0
For i = Len(str) To 1 Step -1
oct2dec = oct2dec + CInt(Mid(str, i, 1)) * 8 ^ (Len(str) - i)
Next i
End Function
Public Function hex2dec(str As String)
hex2dec = 0
For i = Len(str) To 1 Step -1
b = Mid(str, i, 1)
If Not IsNumeric(b) Then b = CStr(Asc(UCase(b)) - 55)
hex2dec = hex2dec + CInt(b) * 16 ^ (Len(str) - i)
Next i
End Function
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询