how do i properly refresh a context menu strip?

I wouldn’t suggest continually refreshing the context strip. Instead, it’s much cleaner to just show and hide the items you need. The ToolStripItemCollection.Add method returns a reference to the newly added button, so you can just store those references for later use. Then use the ToolStripItem.Visible property to show/hide them as required. For example:

// Somewhere to keep references to the buttons you need to modify
private static ToolStripItem _stopButton;
private static ToolStripItem _startButton;
private static ToolStripItem _pauseButton;

static ContextMenuStrip GetContext()
{
    ContextMenuStrip CMS = new ContextMenuStrip();

    // Store references to the buttons
    _pauseButton = CMS.Items.Add("Pause", null, new EventHandler(Stop_Click));
    _stopButton = CMS.Items.Add("Stop", null, new EventHandler(Stop_Click));
    _startButton = CMS.Items.Add("Start", null, new EventHandler(Start_Click));

    CMS.Items.Add("Set Automatic", null, new EventHandler(Auto_Click));
    CMS.Items.Add("Set Manual", null, new EventHandler(Manual_Click));

    CMS.Items.Add("Exit", null, new EventHandler(Exit_Click));
    return CMS;
}

Then in the method where you change the status of the app, for example:

public void SetButtonStatus(bool running)
{
    // Only show this when not running
    _startButton.Visible = !running; 

    _stopButton.Visible = running;
    _pauseButton .Visible = running;
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top