deny keystroke Textbox Tab key not Working?

P

Powerguy

Hi,

I am trying to allow users to only be able to enter numeric numbers
into a textbox. The problem is that I cannot move focus with the tab
key even if I allow it as an allowable key stroke. I have successfully
achieved this thanks to abit of code I found from within these forums
(thanks who ever posted it!):

Protected Overrides Function ProcessDialogKey(ByVal keydata As
System.Windows.Forms.Keys) As Boolean
If txtboxAB.Focused Then
Select Case keydata
Case Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4,
Keys.D5, Keys.D6, Keys.D7, _
Keys.D8, Keys.D9, Keys.Back, Keys.Decimal,
Keys.OemPeriod, Keys.NumPad0, Keys.NumPad1, Keys.NumPad2,
Keys.NumPad3, _
Keys.NumPad4, Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8,
Keys.NumPad9, Keys.Delete, KEYS.TAB 'accept digits only
Return False
Case Else
Return True
End Select
Else
Return MyBase.ProcessDialogKey(keydata)
End If
End Function

The problem I have is that the Keys.TAB does not seem to work. When it
goes into the function it detects that a TAB key has been pressed (the
same as the rest of the number keys) however it does not move the
focus to the next textbox. Does anyone know how I can make this work??

PowaGuy
 
I

Imran Koradia

Protected Overrides Function ProcessDialogKey(ByVal keydata As
System.Windows.Forms.Keys) As Boolean
If txtboxAB.Focused Then
Select Case keydata
Case Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4,
Keys.D5, Keys.D6, Keys.D7, _
Keys.D8, Keys.D9, Keys.Back, Keys.Decimal,
Keys.OemPeriod, Keys.NumPad0, Keys.NumPad1, Keys.NumPad2,
Keys.NumPad3, _
Keys.NumPad4, Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8,
Keys.NumPad9, Keys.Delete, KEYS.TAB 'accept digits only
Return False

-- allow the base class to handle these keys in the normal way instead of
returning false.
replace 'Return False' with 'Return MyBase.ProcessDialogKey(keydata)'
Case Else
Return True
End Select
Else
Return MyBase.ProcessDialogKey(keydata)
End If
End Function

If you are going to be using this kind of a textbox often, I would suggest
you create a user control derived from the textbox and override the
ProcessCmdKey method in your user control adding this code there so that you
don't have to do it over and over again whenever you need this
functionality.

hope this helps..
Imran.
 
J

J van Rhyn

This change will allow for the tab key to be processed correctly.

Protected Overrides Function ProcessDialogKey(ByVal keydata As System.Windows.Forms.Keys) As Boolean
        If TextBox1.Focused Then
            Select Case keydata
                Case Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, _
                                    Keys.D8, Keys.D9, Keys.Back, Keys.Decimal, Keys.OemPeriod, Keys.NumPad0, Keys.NumPad1, Keys.NumPad2, Keys.NumPad3, _
                    Keys.NumPad4, Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8, Keys.NumPad9, Keys.Delete, Keys.Tab
                    Return MyBase.ProcessDialogKey(keydata)
                Case Else
                    Return True
            End Select
        Else
            Return MyBase.ProcessDialogKey(keydata)
        End If
    End Function



Powerguy wrote:

Hi, I am trying to allow users to only be able to enter numeric numbers into a textbox. The problem is that I cannot move focus with the tab key even if I allow it as an allowable key stroke. I have successfully achieved this thanks to abit of code I found from within these forums (thanks who ever posted it!): Protected Overrides Function ProcessDialogKey(ByVal keydata As System.Windows.Forms.Keys) As Boolean If txtboxAB.Focused Then Select Case keydata Case Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, _ Keys.D8, Keys.D9, Keys.Back, Keys.Decimal, Keys.OemPeriod, Keys.NumPad0, Keys.NumPad1, Keys.NumPad2, Keys.NumPad3, _ Keys.NumPad4, Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8, Keys.NumPad9, Keys.Delete, KEYS.TAB 'accept digits only Return False Case Else Return True End Select Else Return MyBase.ProcessDialogKey(keydata) End If End Function The problem I have is that the Keys.TAB does not seem to work. When it goes into the function it detects that a TAB key has been pressed (the same as the rest of the number keys) however it does not move the focus to the next textbox. Does anyone know how I can make this work?? PowaGuy
 
C

Cor Ligthert

Powerguy,

When you want another sample, this works as well when somebody paste a
charachter in the textbox .

I hope this helps?

Cor

Private Sub textbox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyUp
If e.KeyValue <> 8 Then
If Not IsNumeric(TextBox1.Text) Then
If TextBox1.Text.Length > 0 Then
MessageBox.Show("Only numeric is allowed")
TextBox1.SelectionStart = TextBox1.Text.Length - 1
TextBox1.SelectionLength = 1
End If
End If
End If
End Sub
Private Sub TextBox1_Validating(ByVal sender _
As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
If TextBox1.Text.Length > 0 Then
If Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("There was an error pasting")
TextBox1.Focus()
e.Cancel = True
End If
End If
End Sub
///
 
P

Powerguy

Thanks Imran and Cor!

Both your posts helped me alot. Imran, my testbox's can now accept
and process the TAB keys correctly. I did have a problem with the
shift + Tab key combination thou but I managed to figure it out as
follows:

Protected Overrides Function ProcessDialogKey(ByVal keydata As
System.Windows.Forms.Keys) As Boolean
If txtboxAB.Focused Or txtboxBC.Focused Or txtboxAC.Focused Or
txtboxAE.Focused Or txtboxBE.Focused Or _
txtboxCE.Focused Or txtboxHA.Focused Or txtboxHB.Focused Or
txtboxHC.Focused Or txtboxER.Focused Or txtboxHE.Focus Then
Select Case keydata
Case Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4,
Keys.D5, Keys.D6, Keys.D7, _
Keys.D8, Keys.D9, Keys.Back, Keys.Decimal,
Keys.OemPeriod, Keys.NumPad0, Keys.NumPad1, Keys.NumPad2,
Keys.NumPad3, _
Keys.NumPad4, Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8,
Keys.NumPad9, Keys.Delete, Keys.Tab, Keys.ShiftKey, (Keys.Shift +
keys.Tab) <--- to get Shift and Tab key to work!
Return MyBase.ProcessDialogKey(keydata)
Case Else
Return True
End Select
Else
Return MyBase.ProcessDialogKey(keydata)
End If
End Function

Cor, I forgot completely about the context menu and copying and
pasting. I actually didn't want the textbox to beable to have this
functionality so I managed to iliminate it with the following:

Dim cm As New ContextMenu
txtboxAB.ContextMenu = cm

Thanks again you guys, you helped me a heap!

Powaguy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top