Context menu does not open on first click
I have a C# application where I dynamically populate, then launch开发者_Go百科 a context menu called cmTestplan.
private void cmTestplan_Opening(object sender, CancelEventArgs e)
{
if ((cmTestplan.SourceControl is TextBox) &&
(cmTestplan.SourceControl.Enabled == true) &&
(cmTestplan.SourceControl.Text != ""))
{
RightClickSetup(cmTestplan.SourceControl.Text);
cmTestplan.Show();
}
}
private void RightClickSetup(string Path)
{
this.UseWaitCursor = true;
cmTestplan.Items.Clear();
//Test for file or folder
if (Directory.Exists(Path) == true)
{
cmTestplan.Items.Add("Open folder");
this.UseWaitCursor = false;
}
else if (File.Exists(Path) == true)
{
cmTestplan.Items.Add("Open folder");
cmTestplan.Items.Add("Open file");
this.UseWaitCursor = false;
}
}
For some reason (that I hope one of you fine ladies or gentlemen may be able to provide) the menu does not display on the first time I right-click on a text box with which this cmTestplan is associated. It will display consistently thereafter on a single right-click.
I've tried commenting out all the file/folder checking and still get the problem.
Your menu does not open since the event argument cancel is default true for an empty menu.
simply add e.Cancel = false;
in your Opening event handler;
RightClickSetup(cmTestplan.SourceControl.Text);
e.Cancel = false;
.. you can also skip the "show" call.
See also MSDN
精彩评论