Solved

TabControl - moves through tabs with left / right arrow

Posted on 2010-08-13
17
465 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
[X]
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
  • 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 86

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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 86

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 86

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 86

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

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.   …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

726 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