| 网站首页 | JAVA文章 | AppServers | Web开发 | 应用开发 | 资源下载 | 论坛
    想学好编程,学好外语很重要  [enadd  2006年12月25日]        
设为首页 加入收藏 联系站长
您现在的位置: 编程笔记网 >> 应用开发 >> vb >> 系统 >> 文章正文
利用[+]按钮和[-]按钮增大和减小日期            【字体:
利用[+]按钮和[-]按钮增大和减小日期
作者:未知    文章来源:-    点击数:    更新时间:2006-8-9

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.

[1]

文章录入:enadd    责任编辑:enadd 
  • 上一篇文章:

  • 下一篇文章: 没有了
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
  • 用VB编写DirectX7.0游戏(下…

  • 用VB编写DirectX7.0游戏(上…

  • 使用OLE DB和ADO调用返回记录…

  • 使用VB调用Oracle程序包内的…

  • VB中用ADO对象动态创建数据库…

  • fffff

  • 如何用MSComm Control传Bina…

  • MultiLink的WinSock Server程…

  • 用VB制作浏览器

  • 用VB编写网络寻呼机

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 管理登录 |