winform中的radioButton不用容器怎么分组

aideng 2009-11-12 09:34:34
在一个窗体中有四个radiobutton,分别为Radiobutton1,Radiobutton2,Radiobutton3,Radiobutton4;
我想把Radiobutton1,Radiobutton2分为一组,Radiobutton3,Radiobutton4分为一组,不用groupbox,panel等容器控件,怎么实现.在网上查了一下,说可能用程序实现,有那位给段代码.或者有其它方法实现,谢谢?????????
...全文
3556 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
caidonghan 2012-06-13
  • 打赏
  • 举报
回复
用panl或者用groupbox
cloudtian101 2011-12-20
  • 打赏
  • 举报
回复
不用容器
思嘉 2011-09-27
  • 打赏
  • 举报
回复
看答案的
ly576107712 2011-09-16
  • 打赏
  • 举报
回复
看答案的
oturer 2011-08-09
  • 打赏
  • 举报
回复
放个panel
  • 打赏
  • 举报
回复
4楼你行不行?
糖炒栗子No1 2011-02-28
  • 打赏
  • 举报
回复
白痴们,人家问的是winform
pilha 2010-11-30
  • 打赏
  • 举报
回复
白痴们,人家问的是winform
YIDENGBONE 2010-01-12
  • 打赏
  • 举报
回复
路过
aideng 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kxy0931 的回复:]
果要对RadioButton控件进行分组,就要使用到其GroupName属性,GroupName值相同的控件被认为是一组的,在同一组RadioButton控件中你始终只能选择一项,但RadioButton控件的分组属性GroupName似乎也仅仅是起分组的作用而已,对获取选中项的值一点帮助都没有(个人观点),而使用RadioButtonList似乎是更好的方案,同一个RadioButtonList的选项自然被认为是一组,并且获取选中项的值也比RadioButton好多了。

Test.aspx:

复制内容到剪贴板 程序代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>RadioButton分组 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButton ID="RadioButton1" Checked="true" runat="server" GroupName="Color" Text="Red" />
        <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Color" Text="White" />
        <asp:RadioButtonList ID="Sex" runat="server" RepeatColumns="3">
            <asp:ListItem Value="1" Selected="true">Boy </asp:ListItem>
            <asp:ListItem Value="2">Girl </asp:ListItem>
        </asp:RadioButtonList> <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /> <br /> <br />
        <asp:Label ID="Label1" runat="server" Text="Label"> </asp:Label>
    </div>
    </form>
</body>
</html>

Test.aspx.cs:

复制内容到剪贴板 程序代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //
    }

    /// <summary>
    /// 显示选中值
    /// </summary>
    /// <param name="sender"> </param>
    /// <param name="e"> </param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "";

        //遍历Color  GroupName属性仅起分组作用,对取值毫无帮助? 有等研究!
        if (RadioButton1.Checked) Label1.Text += "Color" + ":" + RadioButton1.Text;
        if (RadioButton2.Checked) Label1.Text += "Color" + ":" + RadioButton2.Text;

        Label1.Text += " <br/>";

        //获取Sex值
        Label1.Text += "Sex" + ":" + Sex.SelectedItem.Value.ToString() + "(" + Sex.SelectedItem.Text + ")";
    }
}

注意RadioButtonList控件的RepeatColumns属性,将设置同一行上显示的选项数目,这对要将多个选项横向排列时特别有用!

[/Quote]

看准了我问的是什么
kxy0931 2009-11-19
  • 打赏
  • 举报
回复
果要对RadioButton控件进行分组,就要使用到其GroupName属性,GroupName值相同的控件被认为是一组的,在同一组RadioButton控件中你始终只能选择一项,但RadioButton控件的分组属性GroupName似乎也仅仅是起分组的作用而已,对获取选中项的值一点帮助都没有(个人观点),而使用RadioButtonList似乎是更好的方案,同一个RadioButtonList的选项自然被认为是一组,并且获取选中项的值也比RadioButton好多了。

Test.aspx:

复制内容到剪贴板 程序代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>RadioButton分组</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButton ID="RadioButton1" Checked="true" runat="server" GroupName="Color" Text="Red" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="Color" Text="White" />
<asp:RadioButtonList ID="Sex" runat="server" RepeatColumns="3">
<asp:ListItem Value="1" Selected="true">Boy</asp:ListItem>
<asp:ListItem Value="2">Girl</asp:ListItem>
</asp:RadioButtonList><br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /><br /><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>

Test.aspx.cs:

复制内容到剪贴板 程序代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//
}

/// <summary>
/// 显示选中值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "";

//遍历Color GroupName属性仅起分组作用,对取值毫无帮助? 有等研究!
if (RadioButton1.Checked) Label1.Text += "Color" + ":" + RadioButton1.Text;
if (RadioButton2.Checked) Label1.Text += "Color" + ":" + RadioButton2.Text;

Label1.Text += "<br/>";

//获取Sex值
Label1.Text += "Sex" + ":" + Sex.SelectedItem.Value.ToString() + "(" + Sex.SelectedItem.Text + ")";
}
}

注意RadioButtonList控件的RepeatColumns属性,将设置同一行上显示的选项数目,这对要将多个选项横向排列时特别有用!
zl194 2009-11-19
  • 打赏
  • 举报
回复
你试试将所有的Radiobutton的名字设置为相同的。
herotang101 2009-11-19
  • 打赏
  • 举报
回复
分组是什么意思?
你既然都知道Radiobutton1,Radiobutton2为一组了
那程序直接操作就是了
zl194 2009-11-19
  • 打赏
  • 举报
回复
设成数组。

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧