我正在开发作为用户控件的选项卡按钮,我如何使其表现为单选按钮。 “当用户在一个组中选择一个选项按钮(也称为单选按钮)时,其他人会自动清除”。谢谢。如何使用户控件的行为作为单选按钮
回答
如果您想要单选按钮的行为,请使用单选按钮。
使用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);
}
}
为什么downvote? – Bob
因为你的回答没有意义。什么JavaScript?问题甚至不在于网络。 – CannibalSmith
如果这是ASP.NET,这很合理。他没有具体说明,但他的问题中没有任何内容表明它不是。 – Bob
说出您的自定义选项卡按钮控制被命名为MyTabButton
, 重写和实现平等相待,并 然后在您的自定义控制类的Click事件处理程序,
if (this.Checked)
foreach(Control myBut in Parent.Controls)
if (myBut is MyTabButton && !myBut.Equals(this))
myBut.Checked = false;
需要为如果我的更多信息小号WindowsForms,WPF,ASP.NET等。但
如果是WPF我写了一个帖子,说明解决这个问题我的方法: Grouping and Checkboxes in WPF
编辑以包含答案
您可以通过重写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);
}
}
}
这不提供问题的答案。一旦你有足够的[声誉](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-can- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/17837035) –
它不怎么样?我刚刚给出了一个确切的类似问题的解决方案。模拟RadioButton的行为。它还经过测试,包括代码示例和结果的屏幕截图。 – ThymiosK
尽管这个链接可能回答这个问题,但最好在这里包含答案的基本部分,并提供参考链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/17837035) – Diemuzi
- 1. 我如何使Tcl/Tk按钮的行为像单选按钮?
- 2. 使用div作为单选按钮
- 3. 使用div作为单选按钮
- 4. AjaxToolkit选项卡控件作为按钮
- 5. 如何实现单选按钮行为?
- 6. 如何使用复选框列表作为单选按钮
- 7. 如何使窗口按钮的行为作为网站按钮
- 8. 用户控制作为图像按钮
- 9. 作为选项卡的单选按钮
- 10. 使用单选按钮作为普通按钮
- 11. 如何使用图像作为单选按钮
- 12. 如何创建单选按钮作为按钮?
- 13. 为什么单选按钮单击事件的行为不同
- 14. HTML使常规按钮的行为像一组单选按钮
- 15. 如何使按钮组具有与一组单选按钮相同的行为
- 16. 如何创建按钮的行为作为切换按钮?
- 17. 作为单选按钮组的NSButton组
- 18. 单选按钮控件
- 19. 使用jquery控制按钮行为使用按钮
- 20. 用户控件按钮单击事件
- 21. 与单选按钮的奇怪行为
- 22. 使用ng值为单选按钮(angularjs)
- 23. 如何使用ObservableCollection源实现XAML单选按钮控件?
- 24. 如何使用单选按钮列表控件
- 25. 为什么使用ng-repeat的单选按钮不起作用?
- 26. 使用单选按钮作为复选框
- 27. 使用复选框作为单选按钮
- 28. 如何为已经选定的单选按钮编写事件?
- 29. TabPageControl作为用户控件:字幕和按钮增加
- 30. 根据单选按钮选择注入用户控件(ASP.NET MVC)
您使用的Windows窗体或的Windows Presentation Foundation? – CannibalSmith
使用Windows表格 – volody