0

is there anyway to set a condition such as:

when i open a file, it loads some infos on a ListView, the fifth subitem (should be 4 on index count) is loading text, if it is "ETDF", enable an item in the contextmenustrip, if not, disable it, i tried this inside the contextmenustrip but it just gave me an exeption:

if (listView1.SelectedItems[4].ToString() != "ETDF")
        {
            editToolStripMenuItem.Enabled = false;
        }
        else if (listView1.SelectedItems[4].ToString() == "ETDF")
        {
            editToolStripMenuItem.Enabled = true;
        }

am i doing something wrong ?

Omarrrio
  • 1,075
  • 1
  • 16
  • 34

3 Answers3

1

Set the Enabled property within the Opening event.

private void MyContextMenuStrip_Opening(object sender, CancelEventArgs e)
{
    editToolStripMenuItem.Enabled = (listView1.SelectedItems[4].ToString() == "ETDF");
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • still same exception (ArgumentOutOfRangeException), and i was doing it before. – Omarrrio Jun 29 '13 at 23:38
  • Does `listView1.SelectedItems` actually have 5 values in it? `ArgumentOutOfRangeException` seems like your issue is with accessing a non-existent index in `SelectedItems`. – keyboardP Jun 29 '13 at 23:40
  • it depends, what i mean to is the subitem, but i can't access that with my code :/ – Omarrrio Jun 29 '13 at 23:44
  • What I mean is that the exception seems to be thrown with the line `listView1.SelectedItems[4].ToString()` because you're trying to access the 4th index but there might not be 5 items. In your code, what line actually throws the exception? – keyboardP Jun 29 '13 at 23:48
  • that line, exactly the number 4, it says out of range, just to shorten my question, is there anyway i can access subitems from selected items, the condition is about the subitem[4] of the selected item. – Omarrrio Jun 29 '13 at 23:52
  • 2
    @Omarrrio I think you want to talk about 4 as the index of a subitem. You may want to try `if(listView1.SelectedItems.Count == 1) editToolStripMenuItem.Enabled = listView1.SelectedItems[0].SubItems[4].ToString() == "ETDF");"` – King King Jun 29 '13 at 23:56
  • thanks King, i changed the ToString() to Text as suggested TerryBozzio, and it worked – Omarrrio Jun 30 '13 at 00:09
1

Did you get an ArgumentOutOfRangeException. If so, please check the count.

        if (listView1.Items.Count >= 5)
        {
            if (listView1.SelectedItems[4].ToString() != "ETDF")
            {
                editToolStripMenuItem.Enabled = false;
            }
            else if (listView1.SelectedItems[4].ToString() == "ETDF")
            {
                editToolStripMenuItem.Enabled = true;
            }
        }
rgrano
  • 351
  • 1
  • 6
1
if (listView1.SelectedItems[4].Text != "ETDF")
        {
            editToolStripMenuItem.Enabled = false;
        }
        else if (listView1.SelectedItems[4].Text == "ETDF")
        {
            editToolStripMenuItem.Enabled = true;
        }

this should solve your problem,error was on calling tostring() when should be text

terrybozzio
  • 4,424
  • 1
  • 19
  • 25
  • it actually helped me find the true answer, thanks, i can't post the answer myself, King found it before, here it is: (king post it so i can mark as answer) `if (listView1.SelectedItems.Count == 1) editToolStripMenuItem.Enabled = listView1.SelectedItems[0].SubItems[4].Text == "ETDF";` – Omarrrio Jun 30 '13 at 00:07