This is a migrated thread and some comments may be shown as answers.

Winforms - RadMessageBox - Shortcut keys

8 Answers 161 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Duane
Top achievements
Rank 1
Duane asked on 03 Mar 2011, 12:42 PM
Hi Guys,

Please can any one advise if there is a way for the RadMessageBox to be set up so when  Yes / No buttons are used, pressing "Y" or "N" on the keyboard will trigger the buttons?

We are using 2009 Q2 controls.

Thanks and kind regards,
Duane.

8 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 03 Mar 2011, 02:40 PM
Hello,

I don't have access to this version of the controls, but I think this should work (I am using the latest version).

This is based on setting up a global shortcut on the form, checking to see if the focused control's parent is a RadMessageBox and then setting the dialog result to the chosen value.

You can find out more about global shortcuts at this help topic

So, to implement this...
1: Desfine a shortcut provider that checks for Crtrl+Y and Ctrl+N and if it gets either of these, then as long as the FocusedControl parent is a RadMessageBox, it will send the response.

MyShortcutProvider
public class MyShortcutProvider : IShortcutProvider
 {
     private RadShortcutCollection shortcuts;
     private bool registered;
     public MyShortcutProvider()
     {
         this.shortcuts = new RadShortcutCollection(this);
     }
     #region IShortcutProvider Members
     public void OnPartialShortcut(PartialShortcutEventArgs e)
     {
         e.Handled = true;
     }
     public void OnShortcut(ShortcutEventArgs e)
     {
         if (e.FocusedControl.GetType() == typeof(RadButton))
         {
             var button = e.FocusedControl;
             if (button != null)
             {
                 if (button.Parent != null)
                 {
                     if (button.Parent.Name == "RadMessageBox")
                     {
                         RadForm rmBox = (RadForm)button.Parent;
                         if (e.Shortcut.ToString().EndsWith("Y"))
                         {
                             rmBox.DialogResult = DialogResult.Yes;
                             e.Handled = true;
                         }
                         else if (e.Shortcut.ToString().EndsWith("N"))
                         {
                             rmBox.DialogResult = DialogResult.No;
                             e.Handled = true;
                         }
                     }
                 }
             
         }
     }
     public void OnShortcutsChanged()
     {
         //Called by the Shortcuts collection when a shortcut is either added or removed from the collection
         //This is used for optimization purposes - e.g. is we do not have shortcuts registered,
         //we do not need to be registered with RadShortcutManager
         if (this.shortcuts.Count > 0)
         {
             if (!this.registered)
             {
                 RadShortcutManager.Instance.AddShortcutProvider(this);
                 this.registered = true;
             }
         }
         else
         {
             if (this.registered)
             {
                 RadShortcutManager.Instance.RemoveShortcutProvider(this);
                 this.registered = false;
             }
         }
     }
     public RadShortcutCollection Shortcuts
     {
         get
         {
             return this.shortcuts;
         }
     }
     #endregion
 }

2: In your form, create a class level field to hold the shortcut provider
MyShortcutProvider provider;

3: Set up the shortcuts
public Form1()
{
    InitializeComponent();
    provider = new MyShortcutProvider();
    provider.Shortcuts.Add(new RadShortcut(Keys.Control, Keys.Y));
    provider.Shortcuts.Add(new RadShortcut(Keys.Control, Keys.N));
}

4: On a button press (for example) launch the RadMessageBox to test
private void radButton1_Click(object sender, EventArgs e)
{
    DialogResult result = RadMessageBox.Show("Some text", this.Text, MessageBoxButtons.YesNo, RadMessageIcon.Info);
    if (result == System.Windows.Forms.DialogResult.Yes)
    {
        MessageBox.Show("Yes was pressed");
    }
    else if (result == System.Windows.Forms.DialogResult.No)
    {
        MessageBox.Show("No was pressed");
    }
}

I hope that helps but let me know if you have any questions
Richard
0
Duane
Top achievements
Rank 1
answered on 03 Mar 2011, 03:03 PM

Hi Richard,

Thanks for that. It does seem a little long winded having to set that up and then assign the use for EVERY form (providing I have understood correctly!) As we have over 500 forms in our application so it will be a bit of a bind to implement. I'm sure before we changed over to the Telerik controls, the standard MS .Net dialogs worked off the shelf with "Y" and "N" key presses triggering the dialog's buttons.

I'll will look at implementing your solution though. Thanks again.

Best regards,

Duane.

0
Richard Slade
Top achievements
Rank 2
answered on 03 Mar 2011, 03:07 PM
Hello,

Yes, you have understood correctly and it is quite long. Perhaps that would be a good feature request to submit to Telerik.
Let me know if you have further questions
Richard
0
Accepted
Svett
Telerik team
answered on 07 Mar 2011, 04:10 PM
Hi Guys,

@Richard: Thank you for your assistance.
@Duane: You can also achieve this behavior by setting a custom localization provider that will be used by all message boxes. You can use the following code snippet:

public class MyRadMessageLocalizationProvider : RadMessageLocalizationProvider
{
    public override string GetLocalizedString(string id)
    {
        if (id == RadMessageStringID.YesButton)
        {
            return "&Yes";
        }
        else if (id == RadMessageStringID.NoButton)
        {
            return "&No";
        }
 
        return base.GetLocalizedString(id);
    }
}

Then you should replace the default localization provider when your application is started:
RadMessageLocalizationProvider.CurrentProvider = new MyRadMessageLocalizationProvider();

Finally, you can show the message box anywhere in your application:
RadMessageBox.Show("Hello", "Test", MessageBoxButtons.YesNo);

Best wishes,
Svett
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Duane
Top achievements
Rank 1
answered on 07 Mar 2011, 06:16 PM
@Svett:
Hi Svett, That is brilliant and just what I need. A fair bit easier to implement than Richard example. Implmented and working.



@Richard: Sorry had to go with Svett's example as easier to implement in such a large project. Thanks for your input though.

Edited for spellos!
0
Richard Slade
Top achievements
Rank 2
answered on 07 Mar 2011, 06:20 PM
Not at all!! - I wish i had thought of that too... always good to see a neater solution and I've picked up something new too.
All the best
Richard
0
Steve
Top achievements
Rank 1
answered on 19 Feb 2015, 04:10 PM
Hi,

I implemented your solution and now the RadMessageBox shows the shortcut underline for "Y" in Yes, however, pressing the "Y" does not provide any action. Is there something I'm doing wrong?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 24 Feb 2015, 11:57 AM
Hello Steve,

Thank you for writing.

Your question has already been answered in the support thread you have opened on the same topic. However, I am posting the answer here as well in order the community to benefit from it. 

In order to achieve your goal, you need to use RadShortcut. Here is a sample code snippet:
Private Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
    Dim yesButton As RadButton = TryCast(RadMessageBox.Instance.Controls("radButton1"), RadButton)
    If yesButton IsNot Nothing Then
        yesButton.ButtonElement.Shortcuts.Add(New RadShortcut(Nothing, Keys.Y))
    End If
  
    Dim nosButton As RadButton = TryCast(RadMessageBox.Instance.Controls("radButton2"), RadButton)
    If nosButton IsNot Nothing Then
        nosButton.ButtonElement.Shortcuts.Add(New RadShortcut(Nothing, Keys.N))
    End If
  
    Dim dr As DialogResult = RadMessageBox.Show("Some message", "Question", MessageBoxButtons.YesNo)
    Console.WriteLine(dr)
End Sub

I hope this information helps. Should you have further questions, I would be glad to help.
 
Regards,
Dess
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.

 
Tags
General Discussions
Asked by
Duane
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Duane
Top achievements
Rank 1
Svett
Telerik team
Steve
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or