Mnemonic activated without Alt key

9 posts, 1 answers
  1. Jason
    Jason avatar
    18 posts
    Member since:
    Aug 2014

    Posted 01 Oct 2014 Link to this post

    When I press a key inside a grid, a mnemonic outside the grid is activated, instead of the grid entering edit mode.
    For instance, with a combo box column, I expect the first item starting with the key pressed to be selected.

    To reproduce, add a grid view, and standard win forms button to a form.  Add the following code to the form:
    public Form1()
    {
        InitializeComponent();
        var list = new List<Data>();
        list.Add(new Data(1, "Apple"));
        list.Add(new Data(2, "Banana"));
        list.Add(new Data(3, "Orange"));
      
        var lookupList = list.ToList();
      
        radGridView1.MasterTemplate.AllowAddNewRow = false;
        radGridView1.MasterTemplate.AllowColumnChooser = false;
        radGridView1.MasterTemplate.AllowDeleteRow = false;
        radGridView1.MasterTemplate.AllowDragToGroup = false;
        radGridView1.MasterTemplate.EnableGrouping = false;
        radGridView1.ShowGroupPanel = false;
        radGridView1.Columns.Add(new GridViewComboBoxColumn("Key") { DataSource = lookupList, ValueMember = "Key", DisplayMember = "Value" });
        radGridView1.Columns.Add(new GridViewTextBoxColumn("Value"));
        radGridView1.DataSource = list;
      
        button1.Text = "&Button";
        button1.Click += button1_Click;
    }
      
    private void button1_Click(object sender, EventArgs e)
    {
        RadMessageBox.Show(this, "Button Pressed");
    }

    Add the following class:
    public class Data
    {
        public Data(int key, string value)
        {
            Key = key;
            Value = value;
        }
      
        public int Key { get; set; }
        public string Value { get; set; }
    }

    Click on a combo box cell and type "b".  The button is activated, instead of selecting Banana in the combo.

    Is this a bug?
    Is there a work around?
  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 06 Oct 2014 Link to this post

    Hello Jason,

    Thank you for writing.

    The mnemonic keys are purposed to define an access key (a mnemonic or easy-to-remember name) that allows users to choose a control by pressing a single key no matter which control is currently active on the form. However, you can use the UseMnemonic property to control whether the mnemonics will be activated. In order to stop the mnemonic for the button when the grid is currently active you can refer to the following code snippet:
    public Form1()
    {
        InitializeComponent();
     
        var list = new List<Data>();
        list.Add(new Data(1, "Apple"));
        list.Add(new Data(2, "Banana"));
        list.Add(new Data(3, "Orange"));
     
        var lookupList = list.ToList();
     
        radGridView1.MasterTemplate.AllowAddNewRow = false;
        radGridView1.MasterTemplate.AllowColumnChooser = false;
        radGridView1.MasterTemplate.AllowDeleteRow = false;
        radGridView1.MasterTemplate.AllowDragToGroup = false;
        radGridView1.MasterTemplate.EnableGrouping = false;
        radGridView1.ShowGroupPanel = false;
        radGridView1.Columns.Add(new GridViewComboBoxColumn("Key")
        { DataSource = lookupList, ValueMember = "Key", DisplayMember = "Value" });
        radGridView1.Columns.Add(new GridViewTextBoxColumn("Value"));
        radGridView1.DataSource = list;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
     
        button1.Text = "&Button";
        button1.Click += Button_Click;
     
        radGridView1.GotFocus += radGridView1_GotFocus;
        radGridView1.LostFocus += radGridView1_LostFocus;
    }
     
    private void radGridView1_LostFocus(object sender, EventArgs e)
    {
        this.button1.UseMnemonic = true;
    }
     
    private void radGridView1_GotFocus(object sender, EventArgs e)
    {
        this.button1.UseMnemonic = false;
    }
     
    private void Button_Click(object sender, EventArgs e)
    {
        RadMessageBox.Show(this, "Button Pressed");
    }
     
    public class Data
    {
        public Data(int key, string value)
        {
            Key = key;
            Value = value;
        }
     
        public int Key { get; set; }
     
        public string Value { get; set; }
    }

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jason
    Jason avatar
    18 posts
    Member since:
    Aug 2014

    Posted 06 Oct 2014 in reply to Dess Link to this post

    According to this link: http://msdn.microsoft.com/en-us/library/dn742465.aspx, "Access keys are used to interact with controls directly instead of navigating with Tab. They are combined with the Alt key and indicated with an underlined letter in their label."

    Looking at examples from other applications, such as Internet Explrorer, and Microsoft Word, when an edit control (such as a drop-down) has focus, the access keys are accessed in combination with the Alt key.  When a non-edit control (such as a checkbox or radio button) has the focus, access keys can be accessed without the Alt key.  You can see this behavior in the MS Word options dialog (attached).
    Users expect that when a drop-down has focus, pressing a key will select an item in the drop-down, not activate some other button on the screen.

    You are correct that I could work-around this by disabling the mnemonics when the grid gets focus, but if I have multiple controls with mnemonics, this becomes quite a bit of work, and difficult to maintain.

    I believe this is a bug, and should be fixed.
  5. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 09 Oct 2014 Link to this post

    Hello Jason,

    Thank you for writing back.

    It seems that the MS Button has the mentioned incorrect behavior due to the result from its ProcessMnemonic method. It always returns true. However, our RadButton processes its mnemonic only when the Alt key is pressed as well. You can deal with the Button's mnemonic keys problem creating a derivative of the Button and overriding its ProcessMnemonic method:
    public class CustomButton : Button
    {
        protected override bool ProcessMnemonic(char charCode)
        {
            if((Control.ModifierKeys & Keys.Alt) == Keys.Alt)
            {
                return base.ProcessMnemonic(charCode);;
            }
            return false;
        }
    }

    You can read more information on this topic on the following link: http://stackoverflow.com/questions/8593892/accelerator-mnemonic-key-are-executed-without-pressing-alt-key.

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Desislava
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Jason
    Jason avatar
    18 posts
    Member since:
    Aug 2014

    Posted 09 Oct 2014 in reply to Dess Link to this post

    Reading the following post, still believe this behaviour is wrong:
    http://blogs.msdn.com/b/jfoscoding/archive/2005/01/24/359334.aspx
    The combo box should return true from IsInputChar, and thus the key should never be considered for mnemonic processing.

    However, I'm willing to use the suggested work-around.
  7. Answer
    Dess
    Admin
    Dess avatar
    1601 posts

    Posted 14 Oct 2014 Link to this post

    Hello Jason,

    Thank you for writing back.

    The provided article is greatly appreciated. The IsInputChar method returns true for the DataGridView when pressing "B" as the data grid's editor is already initialized while processing the KeyDown event. That is why the MS Button.Click event is not fired in this case. However, RadGridView's editing lifecycle is a bit different than the DataGridView. It initializes the editor when processing the KeyPress event. Hence, the IsInputChar method has already been called and the editor was not active in that moment. As a result the IsInputChar method returned false and the Button.Click event is processed. I would recommend you to use our RadButton which is not experiencing similar undesired behavior.

    Still, we if there are more requests in this area, we will consider improving this behavior.

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Desislava
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  8. Jason
    Jason avatar
    18 posts
    Member since:
    Aug 2014

    Posted 14 Oct 2014 in reply to Dess Link to this post

    Thanks for the detailed answer - that explains the behavior nicely.  As suggested, I will use either a custom button, or a RadButton.
  9. Manish
    Manish avatar
    2 posts
    Member since:
    Jun 2016

    Posted 24 Jun Link to this post

    We have recently converted our controls from win form control to telerik control.

    Now "_" is being shown for mnemonic all the time (without pressing Alt key).

    Earlier when user press Alt key all the mnemonic get highlighted with _.

    i want the same old visual style. 

    Any help will be really helpful.

  10. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 24 Jun Link to this post

    Hello Manish,

    Thank you for writing. 
     
    By design, the boolean UseMnemonic property controls whether the & character is displayed as the symbol itself or whether it is used to designate a mnemonic (visualized as an underscore). Its default value is true i.e. mnemonics are used by default. That is why this special character is displayed always. In order to show the mnemonic characters only when you press the Alt key, you can override the Form.ProcessCmdKey method and enable/disable the UseMnemonic property considering the pressed key:
    public Form1()
    {
        InitializeComponent();
     
        this.radButton1.UseMnemonic = false;
        this.radButton1.Text = "Button1";
    }
     
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        this.radButton1.Text = "Button1";
        this.radButton1.UseMnemonic = false;
        if (msg.Msg == 0x0104)
        {
            this.radButton1.Text = "Butt&on1";
            this.radButton1.UseMnemonic = true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready