enabling menu items and shortcut keys.

M

MarcG

I have an edit menu with the usual menu items (cut, copy, paste...). In the
EditMenu DropDownOpening event, I enable or disable the individual items
depending on the state of my app. The default condition is disabled.

This works fine and the user can never get to an item that the app is not in
a state to handle since the individual enable/disable state is set when the
user looks at them. However, I have menu shortcut keys (e.g., CTL-C for
copy). If the menu has not been opened, it is disabled and consequently the
shortcut key is also disabled.

This is not the standard operating procedure, as demonstrated by Word for
example.

What is the pattern for enabling menu items and shortcuts? Is there a demo
that shows the "right" way to do this?

Thx
Marc
 
L

Linda Liu[MSFT]

Hi Marc,

IMO, you could set the correct value to the default condition when the form
is loaded and then enable/disable the menu items based on the default
condition, so even if the edit menu has not been opened, its menu items as
well as their shortcut keys are already enabled/disabled correctly.

Hope this helps.
If my suggestion is not appropriate to your scenario or you have any
question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

MarcG

Linda,

There really isn't a "default" condition. "Cut" makes sense if there is
something selected and doesn't if nothing is selected.

Using the Edit menu's DropdownOpening event, I can check the condition and
enable or disable the "Cut" menu item and this gives the user the same
feedback that they would get when they used Word. I have the added benefit
that my internal Cut method knows that it can only be called when there is
something to cut. The Cut method is not textually included in the
CutMenu_Click handler -- it is called from there and anywhere else that makes
sense. It is effectively a Command that can be used from anywhere.

Curiously, keyboard shortcust apparently must be tied to menu items. Again
apparently, typing the shortcut key is equivalent to clicking on the menu
item - and if the menu item is disabled, so is the shortcut key.

The failing scenario is this -- click on Edit. the Edit menu's dropdown
event handler checks an sees that there is no selection and so disables the
Cut menu item. So far, so good. Now I close the menu and select something. I
want to type CTL-X, but this won't work since the last time I opened the Edit
menu, its handler disabled Cut.

The effect is that I should be able to do the cut since IF I opened the Edit
menu after the selection, it would enable the Cut item.

I suspect that what I have to do is always leave the Cut menu item enabled,
and disable it IFF the user opens the Edit menu and there is no selection. In
that case, I must disable it in Edit's DropdownOpening event. BUT, I also
have to re-enable it when the Edit menu closes in case the user later enters
CTL-X.

This works, but it also means that my Cut method can't have a contract that
stipulates that it can only be called when there is something to cut.

Marc
 
L

Linda Liu[MSFT]

Hi Marc,

Thank you for your reply and detailed explanation! I understand your
problem much better now.

I think the key point of your problem is how to get notified when the
selection in the edit control is changed. I don't know what edit control is
you use in your application. If you're using the TextBox as the edit
control, you can derive a new class from the TextBox class and override the
OnKeyUp and OnMouseUp method to raise a custom SelectionChanged event.

The following is a sample.

public class ExtTextBox : TextBox
{
public event EventHandler SelectionChanged;
private void OnSelectionChanged()
{
if (SelectionChanged != null)
{
SelectionChanged(this, new EventArgs());
}
}

protected override void OnMouseUp(MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
OnSelectionChanged();
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
OnSelectionChanged();
}
}

Then you add an instance of the ExtTextBox onto a form and subscribe the
SelectionChanged event of the ExtTextBox to update the state of the
corresponding MenuStripItem.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.extTextBox1.SelectionChanged += new
EventHandler(extTextBox1_SelectionChanged);
}

void extTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (this.extTextBox1.SelectionLength == 0)
{
this.copyMenuStripMenuItem.Enabled = false;
}
else
{
this.copyMenuStripMenuItem.Enabled = true;
}
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top