如何使用上下文菜单获取listView中条目的值

时间:2012-06-16 19:05:50

标签: c# .net windows

我在listview上有一个contextmenu控件,虽然我试图在右键单击它时获取listview中所选项的值 - 我查看了很多来源并且没有找到任何令人信服的内容

private void startCheckToolStripMenuItem_Click(object sender, EventArgs e)
{

}

2 个答案:

答案 0 :(得分:1)

只需将发件人转换为ListBox(假设是右键单击的内容)然后您可以遍历选定的项目。

var lbx = sender as ListBox;
foreach (var item in lbx.SelectedItems) ...

[手持钥匙,因此可能是首都化错误等]

答案 1 :(得分:1)

   private void listView1_MouseClick(object sender, MouseEventArgs e)
            {
                ListView listView = sender as ListView;
                this.contextMenuStrip1.Items.Clear();
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    ListViewItem item = listView.GetItemAt(e.X, e.Y);
                    if (item != null)
                    {
                        this.contextMenuStrip1.Items.Add(item.Text);
                        item.Selected = true;
                        contextMenuStrip1.Show(listView, e.Location);
                    }
                }
            }

ListView With ContextMenuStrip