2009-11-27 40 views
2

我正在开发作为用户控件的选项卡按钮,我如何使其表现为单选按钮。 “当用户在一个组中选择一个选项按钮(也称为单选按钮)时,其他人会自动清除”。谢谢。如何使用户控件的行为作为单选按钮

+0

您使用的Windows窗体或的Windows Presentation Foundation? – CannibalSmith

+0

使用Windows表格 – volody

回答

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

} 
+0

为什么downvote? – Bob

+0

因为你的回答没有意义。什么JavaScript?问题甚至不在于网络。 – CannibalSmith

+0

如果这是ASP.NET,这很合理。他没有具体说明,但他的问题中没有任何内容表明它不是。 – Bob

1

很明显,你需要一个容器的按钮,只要选择一个用户控件,触发事件到容器中,容器取消选择其他用户控件

+0

你如何做到这一点?谢谢 – volody

3

说出您的自定义选项卡按钮控制被命名为MyTabButton, 重写和实现平等相待,并 然后在您的自定义控制类的Click事件处理程序,

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

需要为如果我的更多信息小号WindowsForms,WPF,ASP.NET等。但

如果是WPF我写了一个帖子,说明解决这个问题我的方法: Grouping and Checkboxes in WPF

-1

编辑以包含答案

您可以通过重写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); 
     } 
    } 
} 

enter image description here

+0

这不提供问题的答案。一旦你有足够的[声誉](https://stackoverflow.com/help/whats-reputation),你将可以[对任何帖子发表评论](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/17837035) –

+0

它不怎么样?我刚刚给出了一个确切的类似问题的解决方案。模拟RadioButton的行为。它还经过测试,包括代码示例和结果的屏幕截图。 – ThymiosK

+0

尽管这个链接可能回答这个问题,但最好在这里包含答案的基本部分,并提供参考链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/17837035) – Diemuzi