views:

244

answers:

3

I have been asked to write c# winforms app that will give users the ability to select options from a checkbox list and have it automatically redraw/repaint a toolstrip with the selected items.

I am new to winforms so I am not sure how to approach it. Should I be using the BackgroundWorker Process? Invalidate()?

Just alittle confused.

Any assistence of pointing in the right direction would be appreciated.

+1  A: 

You probably don't want a BackgroundWorker as that's run on a non-UI thread and would cause problems when you try to modify the toolstrip (you can only work with the UI on the thread the UI was created on). Handle the CheckedChanged events on the checkboxes and then add or remove items from the toolstrip. The repainting should be automatic.

James Cadd
A: 

A toolstrip contains controls by itself - it does not just "paint" buttons you can press. In order to have the toolstrip display different buttons depending on different conditions, you can:

  1. Clear the toolstrip items and re-create the ones that are needed in the current context in code when items are checked in the list you mentioned
  2. Add all the items and design time (with property Visible = false) and only set the necessary ones to Visible = true upon selection in your check listbox

No need to do any painting :-)

Thorsten Dittmar
A: 

You need to keep tooltips for all options some where (if Tag property of checkboxes is free the put it there). Then when an option is selected or deselected, you need to update tooltips.

Let's suppose you are adding all the checkboxes in a IList. then things will work as follows:

   private IList<CheckBox> options= new List<CheckBox>();

    private void UpdateTTip()
    {
        toolTip1.RemoveAll();
        foreach (CheckBox c in options)
        {
            if (c.Checked)
                toolTip1.SetToolTip(c, c.Tag.ToString());
        }
    }

Now you need to call this on checkedchanged event of options check boxes:

    private void chk_CheckedChanged(object sender, EventArgs e)
    {
          UpdateTTip();
    }
TheVillageIdiot