El sitio más completo sobre programación, recursos web y herramientas online para Webmasters
Fecha: 25/11/2005

Incrementar y decrementar fechas con las teclas + y -

Trucos visual-basic:
If youve ever used Quicken, youve probably notice a handy little feature in that programs date fields


Código Visual Basic


If you've ever used Quicken, you've probably notice 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 ' subtract 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.

Autor: Mike Coleman
Incrementar y decrementar fechas con las teclas + y - Categoría: Trucos, Visual Basic


Scripts Similares:

» Como obtener el nombre del equipo en visual basic
» Hacer parpadear el caption de una ventana
» Obtener y modificar la configuración regional
» Abrir componentes del Panel de Control Rundll32
» Abrir el explorador de windows
» Abrir el navegador por defecto en una URL determinada
» Abrir la aplicación asociada a un documento
» Abrir la ventana de Buscar archivos
» Abrir una base de datos con seguridad en ADO
» Abrir y cerrar la puerta del CD

Privacidad ּ Publicidad ּ Prensa ּ Premios ּ Colaborar ּ Enlazarnos ּ Mapa del Sitio ּ Aviso legal ּ Boletín ּ Contacto
2001 - 2011 | Sitio creado y mantenido por Gabriel Marcelo Rodríguez
Hosting de alta calidad a bajo costo Neolo.com.ar