【问题标题】:How to make usercontrol to behave as radiobutton如何使用户控件表现为单选按钮
【发布时间】:2017-11-04 23:04:39
【问题描述】:

我正在开发标签按钮作为用户控件,我怎样才能使它像单选按钮一样。 “当用户在组中选择一个选项按钮(也称为单选按钮)时,其他选项按钮会自动清除”。谢谢。

【问题讨论】:

  • 您使用的是 Windows 窗体还是 Windows Presentation Foundation?

标签: c# .net winforms


【解决方案1】:

假设你的自定义Tab按钮控件名为MyTabButton, 覆盖并实现 Equals,以及 然后在自定义控件类的 Click 事件处理程序中,

if (this.Checked)
   foreach(Control myBut in Parent.Controls)
       if (myBut is MyTabButton && !myBut.Equals(this))
          myBut.Checked = false;

【讨论】:

    【解决方案2】:

    如果您想要单选按钮的行为,请使用单选按钮。

    使用 javascript 隐藏单选按钮并创建标签按钮来代替原来的单选按钮。将来自选项卡按钮的点击事件提供给原始单选按钮。您可能还希望有一个通用事件来取消选择其他选项卡按钮。

    如果禁用 javascript,您的按钮也会很好地降级。

    由于您提到您使用的是winforms,而不是使用Javascript,您可以覆盖派生的RadioButton 类的paint 方法,以将您的Radiobutton 绘制为选项卡。这是一个基本的例子

    public class ButtonRadioButton : RadioButton {
    
        protected override void OnPaint(PaintEventArgs e) {
            PushButtonState state;
            if (this.Checked)
                state = PushButtonState.Pressed;
            else
                state = PushButtonState.Normal;
    
            ButtonRenderer.DrawButton(e.Graphics, e.ClipRectangle, state);
        }
    
    }
    

    【讨论】:

    • 因为你的回答没有意义。什么javascript?这个问题甚至与网络无关。
    • 如果这是 ASP.NET 就很有意义。他没有具体说明,但他的问题中没有任何内容表明它不是。
    【解决方案3】:

    obviously you need a container for the button, and whenever a usercontrol is selected, fire the event to container, and container deselect the other usercontrol

    【讨论】:

      【解决方案4】:

      需要更多关于这是否是 WindowsForms、WPF、ASP.NET 等的信息。但是

      如果是 WPF,我写了一篇文章来解释我解决此问题的方法: Grouping and Checkboxes in WPF

      【讨论】:

        【解决方案5】:

        已编辑以包含答案

        您可以通过覆盖 OnClick 或 OnMouseClick 事件来实现。我不明白您所说的“清除按钮”是什么意思,所以我只是更改了它的背景色。您可以轻松地使其适应您的财产或其他需求。

        using System;
        using System.Linq;
        using System.Windows.Forms;
        
        namespace StackOverflow
        {
            public partial class FormMain : Form
            {
                public FormMain()
                {
                    InitializeComponent();
                }
            }
        
            public partial class MyRadioButton : Button
            {
                //Override OnClick event. - THIS IS WHERE ALL THE WORK IS DONE
                protected override void OnClick(EventArgs e)
                {
                    do
                    {
                        /*
                            This is where you select current MyRadioButton. 
                            I'm changing the BackColor for simplicity. 
                        */
                        this.BackColor = System.Drawing.Color.Green;
        
                        /*
                           If parent of current MyRadioButton is null, 
                           then it doesn't belong in a group.
                        */
                        if (this.Parent == null)
                            break;
        
                        /*
                            Else loop through all other MyRadioButton of the same group and clear them. 
                            Include System.Linq for this part.
                        */
                        foreach (MyRadioButton button in this.Parent.Controls.OfType<MyRadioButton>())
                        {
                            //If button equals to current MyRadioButton, continue to the next RadioButton
                            if (button == this)
                                continue;
        
                            //This is where you clear other MyRadioButton
                            button.BackColor = System.Drawing.Color.Red;
                        }
                    }
                    while (false);
        
                    //Continue with the regular OnClick event.
                    base.OnClick(e);
                }
            }
        }
        

        【讨论】:

        猜你喜欢
        • 2010-11-28
        • 2017-06-11
        • 1970-01-01
        • 2022-12-22
        • 1970-01-01
        • 2018-08-05
        • 2011-10-01
        • 2011-08-20
        • 2018-02-06
        相关资源
        最近更新 更多