Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

TabControl - moves through tabs with left / right arrow

Posted on 2010-08-13
17
460 Views
Last Modified: 2012-08-13
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
Comment
Question by:TSFLLC
  • 9
  • 4
  • 2
  • +1
17 Comments
 

Author Comment

by:TSFLLC
ID: 33432196
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33432878
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
 

Author Comment

by:TSFLLC
ID: 33432992
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:TSFLLC
ID: 33512888
Bob,

Any idea where to go from here?
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 33898357
set e.Handled = true where e is KeyEventArgs in the KeyUp\KeyDown event handlers on your textbox
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33898395
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
 

Author Comment

by:TSFLLC
ID: 33898601
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
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 33898655
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33898775
"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
 

Author Comment

by:TSFLLC
ID: 33898856
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
 

Author Comment

by:TSFLLC
ID: 33898893
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
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33898942
Check to make sure your form's KeyPreview property is set to False.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33898947
Not sure why it's dropping out...here is a screencast showing it on my system (VS2010):
Idle-Mind-358648.flv
0
 

Author Comment

by:TSFLLC
ID: 33898988
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
 

Accepted Solution

by:
TSFLLC earned 0 total points
ID: 34519011
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
 

Author Closing Comment

by:TSFLLC
ID: 34609051
See previous posting.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question