Solved

TabControl - moves through tabs with left / right arrow

Posted on 2010-08-13
17
456 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:TSFLLC
Comment Utility
Bob,

Any idea where to go from here?
0
 
LVL 16

Expert Comment

by:SriVaddadi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
"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
Comment Utility
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
Comment Utility
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
Comment Utility
Check to make sure your form's KeyPreview property is set to False.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
See previous posting.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now