tab key, mouse click, or select method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fred

    tab key, mouse click, or select method

    In the enter event of a combo box, how to know how this control got the
    focus :
    - by clicking on it with the mouse
    - with the tab key
    - with the Select method of the control class
    I will want to do an action only if the combo box received focus after a
    tab key press.

    Thanks
    Fred
    (France -imperfect english)
  • Kevin Spencer

    #2
    Re: tab key, mouse click, or select method

    Set the Form's KeyPreview property to true, and override the OnKeyDown
    method. Create a Form-level boolean variable and if it is a Tab Key, set the
    variable to true. Otherwise set the variable to false. Also handle the
    MouseDown event for the combo box, and set the variable to false there. Then
    the Focus event handler for the combo box can check the variable to find out
    if the Tab key was just pressed, or if it was entered via a Mouse Click or
    some other Key event.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Ministry of Software Development
    Thoughts and Ideas about programming, philosophy, science, arts, life, God, and related subjects.


    I just flew in from Chicago with
    a man with a wooden leg named Smith
    who shot an elephant in my pajamas.
    So I bit him.


    "Fred" <fb_x_ng@fr_x_e e.frwrote in message
    news:454c9abc$0 $7749$426a34cc@ news.free.fr...
    In the enter event of a combo box, how to know how this control got the
    focus :
    - by clicking on it with the mouse
    - with the tab key
    - with the Select method of the control class
    I will want to do an action only if the combo box received focus after a
    tab key press.
    >
    Thanks
    Fred
    (France -imperfect english)

    Comment

    • Jani Järvinen [MVP]

      #3
      Re: tab key, mouse click, or select method

      Hi Fred,
      I will want to do an action only if the combo box received focus after a
      tab key press.
      You can use the solution that Kevin has given. However, I started to think
      your application and the requirement you have, as to me it fights against
      the normal Windows user interface conventions.

      What kind of activity would you want to do when the user presses the Tab key
      to move to another control instead of using for example the mouse? How about
      the Alt hotkeys that you can assign to a label control?

      --
      Regards,

      Mr. Jani Järvinen
      C# MVP
      Helsinki, Finland
      janij@removethi s.dystopia.fi



      Comment

      • Fred

        #4
        Re: tab key, mouse click, or select method

        Hi,

        First : thanks for responses.
        I have a form with a lot of input fields (text and combo boxes). I want
        that filling these fields be easy and quick for user. So if it navigates
        through controls with the tab key, when he arrives on a combo box, i
        want to automatically open the combo box. For this I created a handler
        for the Enter event, as it :
        private void cb_Enter(object sender, EventArgs e)
        {
        ComboBox cb = (ComboBox)sende r;
        if (cb.SelectionIn dex == -1) cb.DroppedDown = true;
        }
        This works fine when the user uses the tab key; but when he clicks with
        the mouse, then the combo box is opened and closed immediatly : I will
        want to prevent this.

        Fred


        Jani Järvinen [MVP] wrote:
        Hi Fred,
        >
        >
        >>I will want to do an action only if the combo box received focus after a
        >>tab key press.
        >
        >
        You can use the solution that Kevin has given. However, I started to think
        your application and the requirement you have, as to me it fights against
        the normal Windows user interface conventions.
        >
        What kind of activity would you want to do when the user presses the Tab key
        to move to another control instead of using for example the mouse? How about
        the Alt hotkeys that you can assign to a label control?
        >

        Comment

        Working...