vb如何正确计算闰年?

Private Sub Command1_Click()
Dim year As Integer
year = Val(Text1.Text)
If year Mod 400 = 0 Or (year Mod 4 = 0 And year Mod 100 <> 0) Then
Label2.Caption = Text1.Text + "是闰年"
Else: Label2.Caption = Text1.Text + "不是闰年"
End If
End Sub

year = Val(Text1.Text)赋值是可以计算
但当输入了汉字或字符时 也判断是闰年吖
谁有更好的方法计算吗?
1 楼的很复杂 有没有简单一点的 我才初学~~

Public Function ValiText(KeyIn As Integer, ValidateString As String, Editable As Boolean) As Integer
Dim ValidateList As String
Dim KeyOut As Integer
If Editable = True Then
ValidateList = UCase(ValidateString) & Chr(8)
Else
ValidateList = UCase(ValidateString)
End If
If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
KeyOut = KeyIn
Else
KeyOut = 0
Beep
End If
ValiText = KeyOut
End Function

Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = ValiText(KeyAscii, "0123456789", True)
End Sub

ValiText(KeyAscii, 这里是准许输入的字符, True=支持退格)

这个看似复杂,其实是个通用的
Public Function ValiText(KeyIn As Integer, ValidateString As String, Editable As Boolean) As Integer
这个是个函数,你复制到你的程序里就可以了
然后在你需要限制输入内容 的文本框的 KeyPress 事件里调用这个函数 就OK啦~

调用代码就
KeyAscii = ValiText(KeyAscii, "0123456789", True)
这么一句
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-04-17
你可以限制只能输入数字:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii<48 Or KeyAscii>59 Then
KeyPress=0
End If
End Sub
第2个回答  2021-04-03

闰年是我们生活中再平常不过的事了。不过你知道它是怎么计算的吗?

相似回答