Calling ProcessCmdKey in control

C

Colin Graham

I am using the following code on my form to capture whether the tab
control has been pressed.

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As
System.Windows.Forms.Keys) As Boolean

Select Case keyData
Case Keys.Tab
MsgBox("tab pressed")
Return True

End Select

End Function

this code works fine to capture the tab press on the form but i wish
it only to occur on a specific control. what i really wish to do is to
find out if the tab has been pressed when i have got focus on a
specific control e.g. textbox1.text

Can anyone tell me how i can do this e.g. how do i call the function??
or is it a case that i must create a custom control??

Many thanks

CG
 
K

Ken Tucker [MVP]

Hi,

You have to make a control that inherits from textbox to have access
to processcmdkey.

Ken
 
C

Colin Graham

Thanks very much for that its helped me alot. i now have another tiny
problem though see code below.

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As
System.Windows.Forms.Keys) As Boolean


If Me.ActiveControl Is yyn_WeightLoss Or _
Me.ActiveControl Is yyn_RectalBleeding Or _
Me.ActiveControl Is yyn_TubeDifficulties Or _
Me.ActiveControl Is yyn_XRayReport Or _
Me.ActiveControl Is yyn_CarriedOut _
AndAlso keyData = Keys.Tab = VK_TAB Then

fun_TabMoveNext()

ElseIf keyData = Keys.Tab And Control.ModifierKeys =
Keys.ShiftKey Then
If Me.ActiveControl Is yyn_AbdPain Or _
Me.ActiveControl Is yyn_Diarrhoea Or _
Me.ActiveControl Is yyn_PoorIntake Or _
Me.ActiveControl Is txt_OtherReason Or _
Me.ActiveControl Is dtp_ProcedureDate Then

fun_TabMoveBack()

Return True
End If
End If

Return MyBase.ProcessCmdKey(msg, keyData)

End Function

Im trying to catch the shift tab for navigation through the tab
control i.e., shift + tab (back) and tab (Forward). My problem seems
to be that it sees the shift key as a tab key key and moves forwards
when i press shift and im on one of the fields for moving forwards.
any idea who i can trap for purely just shift + tab?? ive read a few
other threads with no avail.

Thanks

CG
 
M

Mick Doherty

OK, so then you should be working in the ProcessTabKey Method.

Protected Overrides Function ProcessTabKey(ByVal forward As Boolean) As
Boolean
Dim ac As Control = Me.ActiveControl
If forward Then
If ac Is TextBox1 Then
fun_TabMoveNext()
Return True
End If
Else
If ac Is TextBox1 Then
fun_TabMoveBack()
Return True
End If
End If
Return MyBase.ProcessTabKey(forward)
End Function
 

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

Top