views:

70

answers:

2

Or maybe google is just not so friendly to me?

What I want is this simple thing:

  • constructor that accepts an array of menu item objects
  • Value get/set property that would set all the Checked properties right
  • bind to all Clicked events of the supplied items and provide One event
  • Working DataBind facilities

If you encountered such a nice thing around, please direct me. No need for manual do-it-in-your-form1.cs-class links, please. This I can do myself.

+2  A: 

See: http://msdn.microsoft.com/en-us/library/ms404318.aspx

Summary: You'll have to make a new ToolStripMenuItem subclass that overrides the OnCheckChanged, OnOwnerChanged, and possibly OnPaint methods.

Note that in our case, we keep the check mark for the UI rather than a radio button. But keep the exclusive tick functionality.

Reddog
+1  A: 

OKay, here's my final code. It does something the other one doesn't (Supports binding), and vice versa. Perhaps one could combine. Use at your pleasure.

// Usage example:    
//     
// ric = new RadioItemCoupler(new ToolStripMenuItem[] {     
//    neverToolStripMenuItem,     
//    alwaysToolStripMenuItem,     
//    errorsOnlyToolStripMenuItem     
// });    
// this.Controls.Add(ric);    
// _ric.DataBindings.Add("CheckedIndex", MySettings, "SmsReplyType",     
//    false, DataSourceUpdateMode.OnPropertyChanged);    

public class RadioItemCoupler : Control    
{    
    private int _checkedIndex;    

    // Zero-based    
    [Bindable(true)]    
    public int CheckedIndex    
    {    
        get { return _checkedIndex; }    
        set    
        {    
            _checkedIndex = value;    
            _items[value].Checked = true;    
        }    
    }    

    public event EventHandler CheckedIndexChanged;    

    ToolStripMenuItem[] _items;    

    private delegate void ItemHandler(ToolStripMenuItem item);    

    public RadioItemCoupler(ToolStripMenuItem[] items)    
    {    
        _items = items;    

        foreach (ToolStripMenuItem tsmi in _items)    
        {    
            tsmi.CheckOnClick = true;    
            tsmi.CheckedChanged += new EventHandler(tsmi_CheckedChanged);    
        }    
    }    

    void tsmi_CheckedChanged(object sender, EventArgs e)    
    {    
        ToolStripMenuItem that = sender as ToolStripMenuItem;    

        // Restore check if checked out    
        bool nothingChecked = true;    
        foreach(var item in _items)    
            nothingChecked = nothingChecked && !item.Checked;    
        if (nothingChecked)    
        {    
            _items[_checkedIndex].Checked = true;    
            return;    
        }    

        if (!that.Checked)     
            return;    

        for (int i = 0; i < _items.Length; i++)    
        {    
            if (that != _items[i])    
            {    
                if (_items[i].Checked)    
                    _items[i].Checked = false;    
            }    
            else    
            {    
                _checkedIndex = i;    

                if (CheckedIndexChanged != null)    
                    CheckedIndexChanged(this, EventArgs.Empty);    
            }    
        }    
    }    
}    
Pavel Radzivilovsky