【发布时间】:2019-09-12 15:14:31
【问题描述】:
我正在列表视图上创建一个上下文菜单,以便能够对特定项目执行功能。右键单击时,上下文菜单正常显示,部分功能执行但不完全,上下文菜单再次显示。
我试图在表单的加载功能中移动上下文菜单创建和绑定,但似乎不起作用...
private JsonReport _lastItemTag;
public Form1()
{
InitializeComponent();
}
private void ReportTemplateManager_Load(object sender, EventArgs e)
{
// Initialize context menu for template control
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Load", new EventHandler(LoadReport_Click));
template_listview.ContextMenu = cm;
}
private void Template_listview_MouseClick(object sender, MouseEventArgs e)
{
bool match = false;
if (e.Button == MouseButtons.Right)
{
foreach (ListViewItem item in template_listview.Items)
{
if (item.Bounds.Contains(new Point(e.X, e.Y)))
{
template_listview.ContextMenu.Show(template_listview, new Point(e.X, e.Y));
match = true;
_lastItemTag = item.Tag as JsonReport;
break;
}
}
if (!match)
_lastItemTag = null;
}
}
下面的函数被执行,但没有关闭表单,我必须再次单击它才能关闭上下文菜单和表单本身...
private void LoadReport_Click(object sender, EventArgs e)
{
if (_lastItemTag != null)
{
Console.WriteLine("Loading"); // This get executed
_lastItemTag = null;
this.Close(); // This doesnt close the form on the first time
}
}
我不明白为什么LoadReport 函数可以“部分”执行而不关闭表单。
【问题讨论】:
标签: c# forms winforms event-handling contextmenu