I've plugged some code into the MouseDown and Click events of a ToolStripMenuItem to generate a menu at run-time, but the menu appears at the upper left corner of the screen instead of under the menu item. It doesn't matter if the code is in MouseDown or Click, the menu is always in the wrong place. What am I doing wrong?
Here's a code sample:
private void windowToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
{
windowToolStripMenuItem.BuildOpenWindowsDropDown(Program.windowManager, (Form f) => (f.SomeProperty == SomeValue));
}
The extension method:
static class ExtensionMethods
{
public static void BuildOpenWindowsDropDown(this ToolStripDropDownItem toModify, WindowManager windowManager, Predicate<Form> constraint)
{
toModify.DropDownItems.Clear();
List<Form> windows = windowManager.FindOpenWindows(constraint);
if (windows != null)
{
windows.ForEach((Form f) =>
{
ToolStripItem tsi = toModify.DropDownItems.Add(f.Text);
tsi.Tag = f;
EventHandler clickHandler = new EventHandler(
(object sender, EventArgs e) =>
{
Form fToShow = (Form)((ToolStripItem)sender).Tag;
fToShow.Show();
});
tsi.Click += clickHandler;
});
}
}
}
And the snippet from the WindowManager class:
public List<Form> FindOpenWindows(Predicate<Form> constraint)
{
var foundTs = from form in windows
where constraint(form)
&& form.Created
select form;
return foundTs.ToList();
}