Search code examples
c#radio-buttonwindows-forms-designercontextmenustriptoolstripcontrolhost

How to access the event when a list of radio button is selected at a context menu strip?


I'm creating the radio buttons in the ContextMenuStrip using the ToolStripControlHost, this way

RadioButton taskRb = new RadioButton();
taskRb.Text = DataGridTable.getTasks()[i].name.ToString();
taskRb.Checked = false;
ToolStripControlHost tRb = new ToolStripControlHost(taskRb);
contextMenuStrip2.Items.Add(tRb);

I need an event like CheckedChanged for the radio buttons in this list, so I can perform some actions when one of the buttons is checked.

What is the best way to do this? since I can't use this event with the ToolStripControlHost.


Solution

  • You can register an event handler for the CheckedChanged event of the RadioButton:

    RadioButton taskRb = new RadioButton();
    
    taskRb.CheckedChanged += new EventHandler(taskRb_CheckedChanged);
    taskRb.Text = DataGridTable.getTasks()[i].name.ToString();
    taskRb.Checked = false;
    
    ToolStripControlHost tRb = new ToolStripControlHost(taskRb);
    contextMenuStrip2.Items.Add(tRb);
    
    protected void taskRb_CheckedChanged(object sender, EventArgs e)
    {
        // Do stuff
    }