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
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
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.
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
@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);Svett
the Telerik team
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!
All the best
Richard
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?
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 SubI hope this information helps. Should you have further questions, I would be glad to help.
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.
