上一篇 | 下一篇

利用[+]按钮和[-]按钮增大和减小日期

发布: 2008-6-26 09:41 | 作者: admin | 来源: | 查看: 1次

Increment and decrement dates with the [+] and [-] keys

By Mike Coleman, Mike.Coleman@anixter.com

If you注释:ve ever used Quicken, you注释:ve probably noticed a handy little feature in that program注释:s date fields. You can press the [+] key to increment one day, [-] to decrement one day, [PgUp] to increment one month, and [PgDn] to decrement one month. In this tip, we注释:ll show you how to emulate this behavior with Visual Basic.

First, insert a text box on a form (txtDate). Set its text property to "" and its Locked property to TRUE.

Now place the following code in the KeyDown event:

Private Sub txtDate_KeyDown(KeyCode As Integer, Shift As Integer)

注释:

注释: 107 = "+" KeyPad

注释: 109 = "-" KeyPad

注释: 187 = "+" (Actually this is the "=" key, same as "+" w/o the shift)

注释: 189 = "-"

注释: 33 = PgUp

注释: 34 = PgDn

注释:

Dim strYear As String

Dim strMonth As String

Dim strDay As String

注释:

If txtDate.Text = "" Then

txtDate.Text = Format(Now, "m/d/yyyy")

Exit Sub

End If

注释:

strYear = Format(txtDate.Text, "yyyy")

strMonth = Format(txtDate.Text, "mm")

strDay = Format(txtDate.Text, "dd")

注释:

Select Case KeyCode

Case 107, 187 注释: add a day

txtDate.Text = Format(DateSerial(strYear, strMonth, strDay) + 1, "m/d/yyyy")

Case 109, 189 注释: sbutract a day

txtDate.Text = Format(DateSerial(strYear, strMonth, strDay) - 1, "m/d/yyyy")

Case 33 注释: add a month

txtDate.Text = Format(DateSerial(strYear, strMonth + 1, strDay), "m/d/yyyy")

Case 34 注释: subtract a month

txtDate.Text = Format(DateSerial(strYear, strMonth - 1, strDay), "m/d/yyyy")

End Select

注释:

End Sub

The one nasty thing about this is that if you have characters that are not the characters usually in a date (i.e., 1-9, Monday, Tuesday, or /) you get errors in the format command. To overcome this, I set the Locked property to True. This way, the user can注释:t actually type a character in the field, but the KeyDown event still fires.

字号: | 推荐给好友

评分:0

我来说两句