i want to do the following: when a certain menuitem is being clicked, this one should become checked and the ones lying directly next to it should become unchecked.. I tried to solve the first part of this problem as follows:
private void runningToolStripMenuItem_Click(object sender, EventArgs e)
{
MenuItem mi = sender as MenuItem;
mi.Checked = true;//causes nullpointer exception
menuStrip1.Items[mi.index+1].Checked=false;
menuStrip1.Items[mi.index-1].Checked=false;
}
also this "solution" causes a nullpointer exception..
Your sender
value is not a MenuItem
. That's why you ge the exception. You should instead cast to to ToolStripItem
see here
However since ToolStripItem
doesn't have the Checked
property you may want to cast it to ToolStripMenuItem
which derives from ToolStripItem
.
var mi = sender as ToolStripMenuItem;
if (mi == null)
{
return; // not a menu item
}
// do your stuff here