Private Sub command1_click()
if text1(7).text>=text1(6).text then
弹出一个带有红色差差的窗体"您的输入有误"。
(要求点击了提示窗体上的确定按钮后使程序不退出,因为我要把输入错误的信息修改过来)
end if
end sub另外帮我用汉语详细的解释一下下面的代码:怎么都看不懂(我知道它实现了text(0-8)只允许输入0和正数的功能,但是细节不懂)
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If Index >= 2 And Index <= 8 Then
'46 代表小数点 8 代表退格键 13 代表回车键
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Or KeyAscii = 8 Or KeyAscii = 13) Then
KeyAscii = 0
End If
End If
End Sub
if text1(7).text>=text1(6).text then
弹出一个带有红色差差的窗体"您的输入有误"。
(要求点击了提示窗体上的确定按钮后使程序不退出,因为我要把输入错误的信息修改过来)
end if
end sub另外帮我用汉语详细的解释一下下面的代码:怎么都看不懂(我知道它实现了text(0-8)只允许输入0和正数的功能,但是细节不懂)
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If Index >= 2 And Index <= 8 Then
'46 代表小数点 8 代表退格键 13 代表回车键
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Or KeyAscii = 8 Or KeyAscii = 13) Then
KeyAscii = 0
End If
End If
End Sub
if text1(7).text>=text1(6).text then
msgbox "提示信息",vbCritical
exit sub
end if
end sub Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If Index >= 2 And Index <= 8 Then
'46 代表小数点 8 代表退格键 13 代表回车键
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Or KeyAscii = 8 Or KeyAscii = 13) Then
KeyAscii = 0
End If
End If
End Sub
这代码好像是我写的
KeyAscii>=48 and KeyAscii<=57就表示按下数字键
KeyAscii = 46表示按下.键
KeyAscii = 8表示按下退格键
KeyAscii = 13表示按下回车键
整段代码的意思就是,当在一个控件数组text1的index=2 到8的文本框输入时
如果按下的键不是数字、小数点、退格键和回车键时,就取消这次按键,即把KeyAscii设置为0
if text1(7).text>=text1(6).text then
msgbox "提示信息",vbCritical
exit sub
end if
end sub
刚才我试过这段代码了,但是text1(7)和text1(6)内部的数值是从第一位开始比较的,如果第一位相等那么就比较第二位。例如:设text1(7)=2241 这个时侯因为text1(7)的第三位较大,所有“提示信息就出来了”。何解?
text1(6)=22300
1.目前text1(2-8)都不能为负数,我现在希望text1(5)可以为负值,当然也可以为0和正数,对其它的textbox的要求不变.(负号只能出现一次,且在第一位)
2.小数点可不可以只出现一次(出现在第一位也可以),现在的代码对小数点的出现次数没有要求。。请帮改一下
if val(text1(7).text)>=val(text1(6).text) then改成如下试一下
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If Index >= 2 And Index <= 8 Then
'46 代表小数点 8 代表退格键 13 代表回车键
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Or KeyAscii = 8 Or KeyAscii = 13) Then
If KeyAscii = 45 Then
If Index <> 5 Then
KeyAscii = 0
Else
If Text1(Index).Text <> "" Then
KeyAscii = 0
End If
End If
Else
KeyAscii = 0
End If
Else
If KeyAscii = 46 And InStr(Text1(Index).Text, ".") > 0 Then
KeyAscii = 0
End If
End If
End If
End Sub
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If (Index >= 2 And Index < 5) or (Index >5 And Index <=8) Then
If KeyAscii = 45 Then
If Len(Text1.Text) > 1 Then Exit Sub
End If '46 代表小数点 8 代表退格键 13 代表回车键
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Or KeyAscii = 8 Or KeyAscii = 13) Then
KeyAscii = 0
End If
End If
if (Index =5) then
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Or KeyAscii = 8 Or KeyAscii = 13 or KeyAscii = 45) Then
KeyAscii = 0
End If
end if
End Sub
2.可以这样改
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If (Index >= 2 And Index < 5) or (Index >5 And Index <=8) Then
If KeyAscii = 45 Then
If Len(Text1.Text) > 1 Then Exit Sub
End If
If KeyAscii = 46 Then
dim pointpos as integer
Dim strText As String
strText=text1.text
pointpos=InStr(1, strText,".")
if pointpos <> 0 then '第一次出现小数点
strText = Right(Text1.Text, Len(Text1.Text) - pointpos)
pointpos=InStr(1, strText,".")
if pointpos <> 0 then
KeyAscii=0
exit sub '第二次出现小数点直接退出
end if
endif
If Len(Text1.Text) > 1 Then Exit Sub
End If
'46 代表小数点 8 代表退格键 13 代表回车键
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Or KeyAscii = 8 Or KeyAscii = 13) Then
KeyAscii = 0
End If
End If
if (Index =5) then
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Or KeyAscii = 8 Or KeyAscii = 13 or KeyAscii = 45) Then
KeyAscii = 0
End If
end if
end sub
假如光标位置是intPos
那么只要把
If Text1(Index).Text <> "" Then
改为
If intPos<>1 or instr("T" & Text1(Index).Text,"-")>0 Then
即可