为什么没有人为 Winforms 编写 RadioMenuItems 类?

发布于 2024-09-04 00:24:45 字数 271 浏览 9 评论 0原文

或者也许谷歌对我不太友好?

我想要的是这个简单的事情:

  • 接受菜单项对象数组的构造函数
  • Value get/set 属性,该属性将设置所有 Checked 属性,正确
  • 绑定到所提供项目的所有 Clicked 事件,并提供一个事件
  • 工作 DataBind 设施

如果您遇到这样的情况周围有一件好事,请指导我。请不需要手动 do-it-in-your-form1.cs-class 链接。这个我自己可以做。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬心 2024-09-11 00:24:45

请参阅:http://msdn.microsoft.com/en-us/library/ ms404318.aspx

摘要:您必须创建一个新的 ToolStripMenuItem 子类来重写 OnCheckChanged、OnOwnerChanged 和可能的 OnPaint 方法。

请注意,在我们的示例中,我们保留 UI 的复选标记而不是单选按钮。但保留专有的勾选功能。

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.

救赎№ 2024-09-11 00:24:45

好的,这是我的最终代码。它可以做其他一个不做的事情(支持绑定),反之亦然。也许可以结合起来。随意使用。

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

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);    
            }    
        }    
    }    
}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文