在vb中,怎样 确定text中的类型,
怎样确定往text中输入的值只能是年份或月份
[解决办法]
change事件中判断数字并且判断数值
[解决办法]
你应该想说的是确保文本框中输入的是年份或月份吧:
'首先保证只能输入数字:Private Sub text1_KeyPress(KeyAscii As Integer) 'text1:年份输入文本框If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0End IfEnd SubPrivate Sub text2_KeyPress(KeyAscii As Integer) 'text2:月份输入文本框If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0End IfEnd Sub'然后在lostfocus事件中判断:Private Sub Text1_LostFocus()'判断年份If Val(Text1) < 0 Or Val(Text1) > 9999 Then MsgBox ("输入年份非法!")End IfText1.SetFocusEnd SubPrivate Sub Text2_LostFocus()'判断月份If Val(Text2) < 0 Or Val(Text2) > 12 Then MsgBox ("输入月份非法!")End IfText2.SetFocusEnd Sub
[解决办法]
用dtpicker控件吧,方便
[解决办法]
Private Sub Command1_Click()If IsDate("1900-" & Text1.Text & "-01") <> True Then If IsDate(Text1.Text & "-01-01") <> True Then MsgBox "输入年份或月份非法!", vbCritical Else MsgBox "输入的是年分" End IfElse MsgBox "输入的是月分"End If
[解决办法]
通过值进行判定.先首要判定只能输入数值,其次,若是月份,必需是1-12的数字,否则就不允许录入.
[解决办法]
'首先保证只能输入数字:Private Sub text1_KeyPress(KeyAscii As Integer) 'text1:年份输入文本框If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0End IfEnd SubPrivate Sub text2_KeyPress(KeyAscii As Integer) 'text2:月份输入文本框If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0End IfEnd Sub'然后在lostfocus事件中判断:Private Sub Text1_LostFocus() '判断年份If Val(Text1) < 1 Or Val(Text1) > 9999 Then MsgBox ("输入年份非法!") Text1.SetFocusEnd IfEnd SubPrivate Sub Text2_LostFocus() '判断月份If Val(Text2) < 1 Or Val(Text2) > 12 Then MsgBox ("输入月份非法!") Text2.SetFocusEnd IfEnd Sub
[解决办法]
多考虑各种情况。用户输入字符,数字,数字个数,分割符号(-,/),年月日的顺序等等。建议用Calendar控件。
[解决办法]
输入内容检查永远是程序员最艰巨的工作之一。
比如
全角数字:2011.5
汉字:二〇一一
带年月汉字:2011年5月
2011.5
2011-5
2011-05
2011.05
……