Open + Highlight Context Menu Item

4 posts, 1 answers
  1. Cameron Hart
    Cameron Hart avatar
    17 posts
    Member since:
    May 2008

    Posted 18 May 2009 Link to this post

    Hey guys,

    I'm trying to polish up the keystroke count in an application. I have a gridview. If the gridview is in focus and the enter key is pressed, I want the row context menu to be opened and the first item in that menu to be highlighted.

    Here's my code:

            private void gvwManifestLines_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
            { 
                if (e.KeyCode == Keys.Return) 
                { 
                    this.rowContextMenu.Show(this.gvwManifestLines, 15, 15); 
                    this.rowContextMenu.Items[0].CanFocus = true
                    this.rowContextMenu.Items[0].Focus(); 
                } 
                // remaining code omitted for brevity. 
     

    Interestingly, if I just hit enter twice I am actually activating the menu item in question, so my Keystroke count has gone down by one (it cuts out a 'down' Keystroke). However, the item is not being highlighted when it is granted focus, and this is going to confuse my users.

    Is there a way of getting the context menu to highlight the item?
  2. Answer
    Jack
    Admin
    Jack avatar
    2335 posts

    Posted 20 May 2009 Link to this post

    Hello Cameron Hart,

    You should call SelectItem method of the DropDown. Please consider the code snippet below:

    private void gvwManifestLines_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
        if (e.KeyCode == Keys.Return) 
        { 
            this.rowContextMenu.Show(this.gvwManifestLines, 15, 15); 
            this.rowContextMenu.DropDown.SelectItem(this.rowContextMenu.Items[0]); 
        } 

    I hope this helps. Should you have further questions, don't hesitate to ask.

    Greetings,
    Jack
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Cameron Hart
    Cameron Hart avatar
    17 posts
    Member since:
    May 2008

    Posted 20 May 2009 Link to this post

    Spot on.

    Thanks for that. I've been using these controls for a while now - either I've gotten a lot better with them, or the controls have gotten a much more robust the last few updates. I'm impressed.
  5. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 21 May 2009 Link to this post

    Hi Cameron Hart,

    Thank you for the positive feedback.

    Yes, we constantly work to improve our controls and we hope that they will become even more robust when we release our next version - Q2 2009. Stay tuned.

    If you have any other questions, please don't hesitate to write us.

    Kind regards,
    Jack
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top