Visual Basic.NET - Interceptar y eliminar las flechas de navegacion

 
Vista:
sin imagen de perfil

Interceptar y eliminar las flechas de navegacion

Publicado por Angel (24 intervenciones) el 22/05/2014 18:57:54
Que tal Jovenes tengo un problema que me esta matando
sucede que tengo un texbox al cual le estoy tratando de hacer un override para que solo acepte numeros y perfecto eso funciona sin problemas

el problema es que quiero anular completamente las flechas de desplazamiento para que el usuario no pueda navegar entre los numeros ingresados y solo pueda borrar el numero que tenga de inicio

se que tengo que hacerlo con el evento KeyPress pero el problema es que el evento Key Press no detecta las flechas de desplazamiento y eso creo que es porque estas no tienen codigo ASCII.

para interceptar las letras Comas Puntos etc utilize el evento KeyDown para interceptarlos y darle valor true ó False a una variable Boolena que luego utilizo en el evento KeyPress en e.Handled para que no puedan ingresar letras.

todo eso funciona perfecto e igual al presionar las teclas de navegacion si ocurre el evento KeyDown y asigna Valor True a la Variable Booleana pero al no levantarse el evento KeyPress nunca le asigna True a e.Handled.. Intente lo siguiente

Dim kea As New KeyPressEventArgs(Convert.ToChar("e.KeyCode"))
OnKeyPress(kea)

osea declarar una variable de tipo KeyPress y de esta manera si que consegui levantar el evento KeyPress y si le asigna True a e.Handled pero la flecha de navegacion si se mueve y es lo que no quiero
o si existe alguna otra alternativa que yo no conozca les agradeceria su ayuda, Muchisimas gracias



adjunto el código

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Public Class Currency
 Inherits TextBox
 Private decCurrency As Decimal
 Private blnNotKeyNum As Boolean = True
 Public Property Currency() As Decimal
  Get
   Return decCurrency
  End Get
  Set(ByVal value As Decimal)
   decCurrency = Format(value, "###,##0.00")
  End Set
 End Property
 
 Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
  'Dim Kcode As Keys = e.KeyCode
 
  'If Kcode < Keys.D0 OrElse Kcode > Keys.D9 Then
  ' If Kcode < Keys.NumPad0 OrElse Kcode > Keys.NumPad9 Then
  '  If Kcode <> Keys.Back And Kcode <> Keys.Oemcomma And Kcode <> Keys.OemPeriod And Kcode <> Keys.Decimal Then
  '   blnNotKeyNum = True
  '  End If
  ' End If
  'End If
  MsgBox(e.KeyCode)
  If e.KeyCode = Keys.Left OrElse e.KeyCode = Keys.Right Then
   blnNotKeyNum = True
   Dim kea As New KeyPressEventArgs(Convert.ToChar("k"))
   OnKeyPress(kea)
  End If
 
  'If Kcode < Keys.D0 OrElse Kcode > Keys.D9 Then If Kcode < Keys.NumPad0 OrElse Kcode > Keys.NumPad9 Then If Kcode <> Keys.Back And Kcode <> Keys.Oemcomma And Kcode <> Keys.OemPeriod And Kcode <> Keys.Decimal Then blnNotKeyNum = True
 
  'And (e.KeyCode <> Keys.Back And e.KeyCode <> Keys.Oemcomma And e.KeyCode <> Keys.OemPeriod And e.KeyCode <> Keys.Decimal)
  'EVALUA QUE LA TECLA SEA NUMERICA
  'If Not (e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9) Or (e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9) Then
  ' HASTA EL MOMENTO TODO VA BIEN YA QUE LA TECLA ES NUMERICA
  ' blnNotKeyNum = False
  'Else
  ' blnNotKeyNum = True
  'End If
 End Sub
 
 Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
  'EVALUA QUE LO INGRESADO SEA NUMERO
  e.Handled = blnNotKeyNum 'COMO NO ES NUMERO NO INGRESA EL CARACTER
  blnNotKeyNum = False ' Reinicia el Valor que tenia como 0
 End Sub
End Class

Ignoren lo que esta comentado puesto que eso funciona jejejejeje Muchas gracias amigos!!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Interceptar y eliminar las flechas de navegacion

Publicado por Pico (167 intervenciones) el 22/05/2014 23:08:29
¿Pero para qué usas el OnKeyPress?

Al OnKeyPress llagan aquellas teclas que tienen representación ascii. Si quieres procesar teclas de dirección necesitas sólo el OnKeyDown. Si la tecla es de dirección le pones al e.Handled=true y no llega al control.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Interceptar y eliminar las flechas de navegacion

Publicado por Angel (24 intervenciones) el 22/05/2014 23:30:09
Si exacto amigo y de hecho e.Handled=true era la solucion jejeje
adjunto el Codigo por si le sirve a alguno. Muchas Gracias

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Public Class CurrencyTextBox
 Inherits TextBox
 Private decCurrency As Decimal
 Private strText As String
 Private strBuffer As String
 Private blnNotKeyNum As Boolean
 Private blnRetroceso As Boolean
 
 Public Property Amount() As Decimal
  Get
   Return Format(decCurrency, "#,###,##0.00")
  End Get
  Set(ByVal value As Decimal)
   decCurrency = value
  End Set
 End Property
 
 Public Property buffer() As String
  Get
   Return strBuffer
  End Get
  Set(ByVal value As String)
   strBuffer = value
  End Set
 End Property
 Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
 
  'EVALUA QUE EL VALOR SEA NUMERICO PARA PODER SER PROCESADO
  blnNotKeyNum = (e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9) AndAlso (e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9) AndAlso (e.KeyCode <> Keys.Back And e.KeyCode <> Keys.Oemcomma And e.KeyCode <> Keys.OemPeriod And e.KeyCode <> Keys.Decimal)
 
  e.Handled = True
 End Sub
 
 Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
  ''EVALUA QUE LO INGRESADO SEA NUMERO
  If Not blnNotKeyNum Then
   If e.KeyChar = Convert.ToChar(Keys.Back) AndAlso Me.buffer.Length > 0 Then
    Me.buffer = Me.buffer.Substring(0, Me.buffer.Length - 1)
   ElseIf Not e.KeyChar = Convert.ToChar(Keys.Back) Then
    Me.buffer &= e.KeyChar
   End If
  End If
  Me.Amount = Val(Me.buffer)
  Me.Text = Format(Me.Amount, "#,###,##0.00")
  e.Handled = True
  blnNotKeyNum = False 'LA TECLA ES NUMERICA  
 End Sub
 
End Class
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar