A
A
Anton20012021-12-11 20:35:46
C++ / C#
Anton2001, 2021-12-11 20:35:46

How to properly use contextMenuStrip for ListView?

Hello, I have this code

private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 250; i++)
            {
                ListViewItem item = new ListViewItem(new string[] { "Ivan", "Russia" });
                listView1.Items.Add(item);
            }
            listView1.ContextMenuStrip = contextMenuStrip1;
            ToolStripMenuItem item1 = new ToolStripMenuItem();
            item1.Name = "NameMessageName";
            item1.Text = "Name";
            item1.Click += contextMenuStrip1_ItemClick;
            contextMenuStrip1.Items.Add(item1);
        }
        private void contextMenuStrip1_ItemClick(object sender, EventArgs e)
        {
             MessageBox.Show(""); // Как вот тут вывести имя
        }

How can I display a message with the name (now there is only Ivan , for tests) ? those I called the menu context in the Alex USA line and you need to display Alex, I hope I managed to explain)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BasiC2k, 2021-12-12
@Anton2001

After clicking, you need to look at the listView1.SelectedItems array and return a SubItem with a name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question