Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 483
  • Last Modified:

TabControl - moves through tabs with left / right arrow

I use the attached code in capturing keys for my custom-designed autocomplete function.  Everything has always worked well except for the use of left/right arrows.

The left/right arrows always work properly when the textboxes being used are outside of a tabcontrol.  Inside the tabcontrol they move according to the currently selected tab.

I saw another posting associated with some ideosynchrocies of using a keycapture function but I don't have the background or understanding to know how to resolve this.
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, _
             ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

    Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" _
        (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer
    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If glUpKey Then
            If msg.WParam.ToInt32() = CInt(Keys.Up) Then
                If lbAutoComplete.Visible = True Then
                Else
                    SendKeys.Send("{UP}")
                    keybd_event(Keys.Up, MapVirtualKey(Keys.Up, 0), 2, 0) ' Down Arrow Up
                    glUpKey = False
                End If
                Return True
            End If
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function 'ProcessCmdKey

Open in new window

0
TSFLLC
Asked:
TSFLLC
  • 9
  • 4
  • 2
  • +1
1 Solution
 
TSFLLCAuthor Commented:
Also included is my sub that actually handles a keyup.
The private variable referred to in the previous code (glUpKey) is shown at the bottom of this code.
    Private Sub txtAutoComplete_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyUp
        If Activating Then Exit Sub
        Activating = True
        If lbAutoComplete.Visible = True Then
            Select Case e.KeyCode
                Case Keys.Home
                    lbAutoComplete.SetSelected(0, True)
                Case Keys.End
                    lbAutoComplete.SetSelected(lbAutoComplete.Items.Count - 1, True)
                Case Keys.PageUp
                    If lbAutoComplete.SelectedIndex > 10 Then
                        lbAutoComplete.SetSelected(lbAutoComplete.SelectedIndex - 10, True)
                    Else
                        lbAutoComplete.SetSelected(0, True)
                    End If
                Case Keys.PageDown
                    If lbAutoComplete.Items.Count - lbAutoComplete.SelectedIndex > 10 Then
                        lbAutoComplete.SetSelected(lbAutoComplete.SelectedIndex + 10, True)
                    Else
                        lbAutoComplete.SetSelected(lbAutoComplete.Items.Count - 1, True)
                    End If
                Case Keys.Escape, Keys.Left, Keys.Right
                    lbAutoComplete.Visible = False
                Case Keys.Up
                    If lbAutoComplete.SelectedIndex > 0 Then
                        lbAutoComplete.SetSelected(lbAutoComplete.SelectedIndex - 1, True)
                    Else
                        lbAutoComplete.SetSelected(lbAutoComplete.Items.Count - 1, True)
                    End If
                Case Keys.Down
                    If lbAutoComplete.SelectedIndex < lbAutoComplete.Items.Count - 1 Then
                        lbAutoComplete.SetSelected(lbAutoComplete.SelectedIndex + 1, True)
                    Else
                        lbAutoComplete.SetSelected(0, True)
                    End If
                Case Keys.Tab
                    Select Case lbAutoComplete.Tag
                        Case "txtLoadStatus"
                            CheckVal(txtLoadStatus)
                            UpdateLoadStatus()
                        Case "txtDriver"
                            CheckVal(txtDriver)
                            ValidateDriverStatus1()
                            UpdateFromDriver(glDriverID)
                    End Select
                    lbAutoComplete.Visible = False
            End Select
        Else
        End If
        If e.KeyCode = Keys.Up Then
            glUpKey = True
        End If
        Activating = False
    End Sub

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So you want the currently selected TabPage to change when left/right are pressed?
Public Class Form1

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Dim index As Integer = TabControl1.SelectedIndex
        Select Case keyData
            Case Keys.Left, Keys.Right
                index = index + IIf(keyData = Keys.Left, -1, 1)
                If index = -1 Then
                    index = TabControl1.TabPages.Count - 1
                ElseIf index = TabControl1.TabPages.Count Then
                    index = 0
                End If
                TabControl1.SelectedIndex = index
                Return True

        End Select

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

End Class

Open in new window

0
 
TSFLLCAuthor Commented:
Hi Bob,

No.  When I have textbox.focus and it contains a value, I want to be able to move left/right in the text field.  What is happening is that the tabcontrol DOES change tabpages and I don't want that to happen.

Also, if I use the up/down button while in a textbox, it does move left/right depending on which I press (up/down).

Basically, I'm losing my left/right key when on text.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
TSFLLCAuthor Commented:
Bob,

Any idea where to go from here?
0
 
SriVaddadiCommented:
set e.Handled = true where e is KeyEventArgs in the KeyUp\KeyDown event handlers on your textbox
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So basically you do NOT want the Tabs to change with left/right arrows when a TextBox has focus?

*Obviously this will only work for TextBoxes...if you want to exclude other types of controls then you'll have to add additional checks in the conditional statement:
Public Class Form1

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Dim ctl As Control = Me.ActiveControl
        If Not IsNothing(ctl) AndAlso Not (TypeOf ctl Is TextBox) Then
            Dim index As Integer = TabControl1.SelectedIndex
            Select Case keyData
                Case Keys.Left, Keys.Right
                    index = index + IIf(keyData = Keys.Left, -1, 1)
                    If index = -1 Then
                        index = TabControl1.TabPages.Count - 1
                    ElseIf index = TabControl1.TabPages.Count Then
                        index = 0
                    End If
                    TabControl1.SelectedIndex = index
                    Return True

            End Select
        End If

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

End Class

Open in new window

0
 
TSFLLCAuthor Commented:
SriVaddadi,

Are you saying that I need to create a sub handling the KeyUp\KeyDown of any text box within my tabcontrols and place e.Handled = true in it?


At any rate,


I think I need to re-phrase my explanation at the beginning of my posting as it relates to:

>> Inside the tabcontrol they move according to the currently selected tab.

What I mean to say is that when I move left or right in a text box that is located inside a tab control, the action is that it changes to the left or right tab according to the key (left/right).

I need to get it to move throught the text and not change tabs....that's a given but I wanted to verbalize it.
0
 
SriVaddadiCommented:
well in that case, you have to handle the keypress event on the textbox and set the e.Handled to true once you have handled the event
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"I need to get it to move throught the text and not change tabs"

See my last post.  This will allow the left/right to be handled by the TextBox as normal but for any other control it will change the currently selected tab:
http://www.experts-exchange.com/Programming/Languages/.NET/Q_26401739.html#33898395
0
 
TSFLLCAuthor Commented:
Mike,

I tried it before but I was taking Lortab at the time.  Just tested it again and it still is immediately changing tabs when I am in a textbox containing text....provided it is a textbox within the tabcontrol.
0
 
TSFLLCAuthor Commented:
Mike,

Your code says 'AndAlso Not (TypeOf ctl Is TextBox) Then'.

TypeOf is suppose to be TextBox isn't.  When debugging, it drops right out of the If statement.


0
 
ZhaolaiCommented:
Check to make sure your form's KeyPreview property is set to False.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure why it's dropping out...here is a screencast showing it on my system (VS2010):
Idle-Mind-358648.flv
0
 
TSFLLCAuthor Commented:
Sorry guys,

I think I just figured out my problem.  Absent-minded.  I have used a custom TabControl for a long time, without even thinking about it.  I'm sure I need to debug through it to see what it's doing.  This has to be the problem.  My apologies.....

Mike I see that yours works.  For some reason it dawned on me about the above.
0
 
TSFLLCAuthor Commented:
Need to close this question.  Found that I forgot about a custom TabControl from previous years.  Completely open to applying equal points to all for their efforts.

Leave up to moderators.
0
 
TSFLLCAuthor Commented:
See previous posting.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now