开发者

How come nobody wrote RadioMenuItems class for Winforms?

开发者 https://www.devze.com 2023-01-02 02:40 出处:网络
Or maybe google is just not so friend开发者_开发知识库ly to me? What I want is this simple thing:

Or maybe google is just not so friend开发者_开发知识库ly 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.


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.


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);    
            }    
        }    
    }    
}    
0

精彩评论

暂无评论...
验证码 换一张
取 消