Tab Key 2.0?

Ok you could get away with doing this in 1.1 but now in 2.0 its a no go
Any help would be great
Thanks
Joe

'Size Group 5 Leave with Tab Click ***

Private Sub cboBU_SizeGroup5_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBU_SizeGroup5.Leave

Dim k As Keys

If k.Tab Then

If cboBU_SizeGroup5.SelectedValue > 0 Then

cboBU_SizeGroup5.SelectedValue = cboBU_SizeGroup4.SelectedValue

End If

End If

End Sub



Answer this question

Tab Key 2.0?

  • reachrishikh

    Really, I am trying to help you, but you haven't described the trouble you are having.  Is it not compiling   Are you finding that the "Do something" code is not run   Is it always running and you don't expect that

    The code



    Dim k As Keys
    If k.Tab Then
    "Do something"
    End If

     


    does compile, and run, VS2005 Beta 2.  As I said in my first response, however, the if clause in this specific code is not useful.  Keys is an enum, and Keys.Tab = 9.  The value 9, when coerced (implicitly in this case) to a boolean, is True, because VB boolean coerces any non-zero integer value to True.  VS2005 Beta 2 behaves the same was as 1.1. I can't speak for earlier versions of VS2005 (I don't have them installed anymore).  Keys.Tab does not .Equal(True), because Keys.Tab is an Integer, and True is a Boolean. 

    All this is a red herring, though. If you are looking to run the "Do something" when you leave the control, then remove the surrounding If clause.  If you are looking to determine how the user left the control, whether by tabbing or mousing, then that is something else altogether. 


  • sgtpazo

    Ah, that is a little more complicated.  Perhaps there is a simpler way (anyone ), but one approach is to create your own LeaveExtended event that tells you what initiated the leave.  Something like following. Put this code in a ComboBoxEx.cs class.  Sorry, it is in C#:



    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;

    namespace ComboBoxEx1
    {
        class ComboBoxEx : ComboBox
        {
            protected bool _tabKeystroke;

            public event EventHandler<ComboBoxExLeavingEventArgs> LeaveExtended;

            protected LeaveReason leaveReason
            {
                get
                {
                    if (_tabKeystroke)
                        return LeaveReason.Tab;
                    else
                        return LeaveReason.MouseOut;
                }
            }
           
            protected override bool ProcessDialogKey(Keys keyData)
            {
                // Cache off whether the keystroke was a Tab
                _tabKeystroke = (keyData == Keys.Tab);
                return base.ProcessDialogKey(keyData);
            }

            protected void OnLeaveExtended(ComboBoxExLeavingEventArgs e)
            {
                if (this.LeaveExtended != null)
                {
                    this.LeaveExtended(this, e);
                }
            }

            protected override void OnLeave(EventArgs e)
            {
                // Fire OnLeaveExtended
                this.OnLeaveExtended(new ComboBoxExLeavingEventArgs(this.leaveReason));
               
                // Reset keystroke cache
                _tabKeystroke = false;
                base.OnLeave(e);
            }


        }

        public enum LeaveReason
        {
            Tab = 0
           , MouseOut
        }

        public class ComboBoxExLeavingEventArgs : EventArgs
        {
            private LeaveReason _reason;
            public LeaveReason Reason
            {
                get { return _reason; }
            }

            public ComboBoxExLeavingEventArgs(LeaveReason reason)
                : base()
            {
                _reason = reason;
            }
        }
    }

     




  • thepaul

    First of the code above works fine in 1.1 always has !!!!

    Here is what I am doing.

    There are 4 combo box in a row once the person selects the value for combo < xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />box 1. They can then hit the tab key. The tab index will take them to combo box 2 once they do this if the value they have selected is not 0 "select one" then set combo box value 2 to = combo box value 1.

    And yes they have the same data, filled from a datable vie sql.
    And yes they all contain the same data (Combo boxes 1-4)
    And yes they all work.

    Pretty simple

    So if you can tell how to repeat this using 2.0 that would be great

    Thanks
    Joe< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


  • williamwon

    Sorry if I am not being clear.

    "If you are looking to determine how the user left the control, whether by tabbing or mousing, then that is something else altogether.  "

    This is what i need

  • ericsmoth

    Firstly, k.Tab will be coerced to a boolean, and since k.Tab is 9, it will always be treated as True, and so it is essentially useless and should be removed.  Nothing in this code truly has anything to do with a tab keystroke.

    Secondly, please describe specifically the problem you are having, rather than simply saying it doesn't work.  I am assuming you are working with comboboxes, based upon your naming convention, but you don't say.  Depending on how you are populating the combobox, SelectedValue may never have a value (i.e., it is always Nothing).  SelectedItem or SelectedIndex may be what you should be examining.

    Finally, it seems like you are trying validate the value selected, so the Validating event may be a better place for your logic.


  • onur6n

    How is it behaving differently in .NET 2.0   Are you using DataBinding or are you loading the comboboxes manually by running through the sql result set
  • jay2068

    Just remove the If k.Tab Then piece as it will never do what you are expecting it to do.  Why it "works" in 1.1 and not 2.0, I'm not sure.  But, I do know that nowhere in your code are you checking whether or not the Tab key was pressed.



  • jh2005

    Ok first off you are on the wrong track.
    The combo boxes work fine.

    The problem lies here< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    1.1
    Dim
    k As Keys
    If k.Tab Then
    "Do something"
    End If

    So now it 2.0 that no longer works so I tried several different way including the one below

    2.0
    If cbool(keys.tab.equals(True) Then
    "Do something"
    End IF

    That dosen't work any ideas why
    Or any ideas on how to accomplish this in 2.0




  • Worren

    That's pretty much the way you have to handle it.  The only missing item here, I think, is no consideration for Shift+Tab.  You could probably hack an alternative solution with a keypress event, but that has the potential to be inconsistent. 

    My question for the OP is, why is tabbing out of the control different than mousing   The would seem counterintuitive to me as a user.

  • Tab Key 2.0?