34. 分别在两个文本框中输入年、月份,单击命令按钮在标签框3中输出该月天数(注意闰年的情况。闰年的判断
34.分别在两个文本框中输入年、月份,单击命令按钮在标签框3中输出该月天数(注意闰年的情况。闰年的判断条件为:年份能被4整除但不能被100整除,或者能被400整除)。...
34. 分别在两个文本框中输入年、月份,单击命令按钮在标签框3中输出该月天数(注意闰年的情况。闰年的判断条件为:年份能被4整除但不能被100整除,或者能被400整除)。
展开
1个回答
展开全部
Option Explicit
Private Sub Command1_Click()
If Text1.Text = "" Or Text2.Text = "" Or Val(Text1.Text) = 0 Or _
Val(Text2.Text) < 1 Or Val(Text2.Text) > 12 Then
MsgBox "---《请先输入正确数字》---", vbOKOnly + vbCritical, "提示"
Exit Sub
End If
Label3.Caption = Text1.Text & "年" & Text2.Text & "月有" & _
dddd(Val(Text1.Text), Val(Text2.Text)) & "天"
End Sub
Private Function dddd(ByVal yyyy As Integer, ByVal mm As Integer) As Integer
Dim a, b
a = Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
b = Array(0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
If (yyyy Mod 4 = 0 And yyyy Mod 100 <> 0) Or (yyyy Mod 400 = 0) Then
dddd = Int(b(mm))
Else
dddd = Int(a(mm))
End If
End Function
'以下的侦错代码可留,亦可移除,操作上意义不同而已。
' 8< ----------------------------------
Private Sub Text1_KeyPress(KeyAscii As Integer)
'限定最多只能输入四位数的数字
If Len(Text1.Text) <= 3 Then
'允许 >= 0 并且 <= 9 的数字
If KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then Exit Sub
End If
If Len(Text1.SelText) >= 1 Then Exit Sub
If KeyAscii = 8 Then Exit Sub '允许倒退删除键8
If KeyAscii = 13 Then Text2.SetFocus '允许回车键13
'前端没经过过滤的输入全部拦截
KeyAscii = 0 '键值为0 即为空(没有输入的状态)
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
'限定最多只能输入二位数的数字
If Len(Text2.Text) <= 1 Then
'允许 >= 0 并且 <= 9 的数字
If KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then Exit Sub
End If
If Len(Text2.SelText) >= 1 Then Exit Sub
If KeyAscii = 8 Then Exit Sub '允许倒退删除键8
If KeyAscii = 13 Then Command1.Value = True '允许回车键13直接运算
'前端没经过过滤的输入全部拦截
KeyAscii = 0 '键值为0 即为空(没有输入的状态)
End Sub
Private Sub Text2_Change()
If Val(Text2.Text) > 12 Then Text2.Text = "12"
End Sub
' ---------------------------------- >8
Private Sub Command1_Click()
If Text1.Text = "" Or Text2.Text = "" Or Val(Text1.Text) = 0 Or _
Val(Text2.Text) < 1 Or Val(Text2.Text) > 12 Then
MsgBox "---《请先输入正确数字》---", vbOKOnly + vbCritical, "提示"
Exit Sub
End If
Label3.Caption = Text1.Text & "年" & Text2.Text & "月有" & _
dddd(Val(Text1.Text), Val(Text2.Text)) & "天"
End Sub
Private Function dddd(ByVal yyyy As Integer, ByVal mm As Integer) As Integer
Dim a, b
a = Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
b = Array(0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
If (yyyy Mod 4 = 0 And yyyy Mod 100 <> 0) Or (yyyy Mod 400 = 0) Then
dddd = Int(b(mm))
Else
dddd = Int(a(mm))
End If
End Function
'以下的侦错代码可留,亦可移除,操作上意义不同而已。
' 8< ----------------------------------
Private Sub Text1_KeyPress(KeyAscii As Integer)
'限定最多只能输入四位数的数字
If Len(Text1.Text) <= 3 Then
'允许 >= 0 并且 <= 9 的数字
If KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then Exit Sub
End If
If Len(Text1.SelText) >= 1 Then Exit Sub
If KeyAscii = 8 Then Exit Sub '允许倒退删除键8
If KeyAscii = 13 Then Text2.SetFocus '允许回车键13
'前端没经过过滤的输入全部拦截
KeyAscii = 0 '键值为0 即为空(没有输入的状态)
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
'限定最多只能输入二位数的数字
If Len(Text2.Text) <= 1 Then
'允许 >= 0 并且 <= 9 的数字
If KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then Exit Sub
End If
If Len(Text2.SelText) >= 1 Then Exit Sub
If KeyAscii = 8 Then Exit Sub '允许倒退删除键8
If KeyAscii = 13 Then Command1.Value = True '允许回车键13直接运算
'前端没经过过滤的输入全部拦截
KeyAscii = 0 '键值为0 即为空(没有输入的状态)
End Sub
Private Sub Text2_Change()
If Val(Text2.Text) > 12 Then Text2.Text = "12"
End Sub
' ---------------------------------- >8
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询