c#:如何在固定窗口大小的两行中动态添加单选按钮
我尝试在Windows窗体应用程序中动态添加两行单选按钮(在我的示例中,单选按钮的数量总是偶数)。我发现了这一点,这有助于我理解如何添加单选按钮c#:如何在固定窗口大小的两行中动态添加单选按钮,c#,winforms,radio-button,radio-group,C#,Winforms,Radio Button,Radio Group,我尝试在Windows窗体应用程序中动态添加两行单选按钮(在我的示例中,单选按钮的数量总是偶数)。我发现了这一点,这有助于我理解如何添加单选按钮 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threadin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test {
public partial class Form1 : Form {
FlowLayoutPanel pnl = new FlowLayoutPanel();
public Form1() {
InitializeComponent();
}
void radioButtonHandler(object sender, EventArgs e)
{
MessageBox.Show("Radio Button", "Click");
}
private void Form1_Load(object sender, EventArgs e) {
pnl.Dock = DockStyle.Fill;
for (int i = 0; i < 16; i++)
{
pnl.Controls.Add(new RadioButton() { Text = "" + i });
pnl.Controls[i].Click += new EventHandler(this.radioButtonHandler);
}
this.Controls.Add(pnl);
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间测试{
公共部分类Form1:Form{
FlowLayoutPanel pnl=新的FlowLayoutPanel();
公共表格1(){
初始化组件();
}
void radioButtonHandler(对象发送方,事件参数e)
{
MessageBox.Show(“单选按钮”、“单击”);
}
私有void Form1\u加载(对象发送方、事件参数e){
pnl.Dock=DockStyle.Fill;
对于(int i=0;i<16;i++)
{
添加(新的单选按钮(){Text=”“+i});
pnl.Controls[i]。单击+=新建事件处理程序(this.radioButtonHandler);
}
this.Controls.Add(pnl);
}
}
}
问题是,上述问题不包括以下问题:
FlowLayoutPanel flp=新的FlowLayoutPanel();
flp.尺寸=新尺寸(950100);
flp.BorderStyle=BorderStyle.FixedSingle;
flp.AutoScroll=true;
对于(int i=0;i<20;i++)
{
RadioButton rb=新RadioButton();
rb.Text=i.ToString();
rb.AutoSize=false;
rb.尺寸=新尺寸(100,25);
flp.Controls.Add(rb);
}
此.Controls.Add(flp);
FlowLayoutPanel=新的FlowLayoutPanel();
panel.AutoScroll=true;
panel.WrapContents=true;
对于(int i=0;i<16;i++)
{
panel.Controls.Add(新单选按钮()
{
Text=”“+i,
锚点=锚点样式。顶部|锚点样式。底部
|主持人风格。左|主持人风格。右,
AutoSize=true,
Margin=//你的风格,
});
panel.Controls[i]。单击+=新建事件处理程序(this.radioButtonHandler);
}
设置锚点样式有助于保持控件对齐,通过填充或大小等属性,您可以定义元素的大小,如果所有单选按钮的宽度相同,则可以使用“大小”属性。如果它们的大小不相同,则最好使用锚点样式。是否要删除空格?我想定义空格大小。抱歉,我无法确定复制你的例子有困难。。。在我的例子中,在我的窗体窗口中根本没有单选按钮代码>成功了。但它似乎没有产生正确的产出。1.当我缩小窗口大小时,不会出现任何滚动条。单选按钮消失在窗口边框后面。2.根据rb的大小,每个rb上有两个以上的rb。3.似乎所有的rb都属于同一条线,而不仅仅是垂直线上的两个。@Pascal是否设置了WrapContents=true?对于调整大小,您应该为面板设置锚点。此代码适用于所有控件,您可以将radiovutton更改为任何类型。谢谢您的回答。但是它没有像预期的那样工作。当我减小窗口大小时,不会出现任何滚动条。单选按钮消失在窗口边框后面。2.根据rb的大小,每个rb上有两个以上的rb。3.看起来,所有rb都属于一起,而不仅仅是垂直线上的两个。
FlowLayoutPanel flp = new FlowLayoutPanel();
flp.Size = new Size(950, 100);
flp.BorderStyle = BorderStyle.FixedSingle;
flp.AutoScroll = true;
for (int i = 0; i < 20; i++)
{
RadioButton rb = new RadioButton();
rb.Text = i.ToString();
rb.AutoSize = false;
rb.Size = new Size(100, 25);
flp.Controls.Add(rb);
}
this.Controls.Add(flp);
FlowLayoutPanel panel = new FlowLayoutPanel();
panel.AutoScroll = true;
panel.WrapContents= true;
for (int i = 0; i < 16; i++)
{
panel .Controls.Add(new RadioButton()
{
Text = "" + i,
Anchor = AnchorStyles.Top | AnchorStyles.Bottom
| AnchorStyles.Left | AnchorStyles.Right,
AutoSize = true,
Margin = //your styles,
});
panel .Controls[i].Click += new EventHandler(this.radioButtonHandler);
}