简体   繁体   English

c#:如何在固定 window 大小的两行中动态添加单选按钮

[英]c#: How to add radio buttons dynamically in two rows with a fixed window size

I try to add radio buttons dynamically to a Windows Forms application in two rows (there is always an even amount of radio buttons in my case).我尝试在两行中动态地将单选按钮添加到 Windows Forms 应用程序中(在我的情况下,单选按钮的数量总是相等的)。 I found this question , which helps me to understand how to generally add radio buttons.我发现了这个问题,这有助于我了解如何通常添加单选按钮。

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

在此处输入图像描述

The problem is, the mentioned question doesn't cover the following questions:问题是,提到的问题不包括以下问题:

  1. How to set the distance between the radio buttons?如何设置单选按钮之间的距离?
  2. How to create a second row with a defined distance below the first row?如何在第一行下方创建具有定义距离的第二行?
  3. How to fix the buttons and insert a horizontally scroll bar if the buttons need more space than the current window has?如果按钮需要比当前 window 更多的空间,如何修复按钮并插入水平滚动条?
  4. OPTIONAL: In my case the two radio buttons along the vertical line always belong together.可选:在我的情况下,沿垂直线的两个单选按钮始终属于一起。 Is it possible to automatically deselect the upper or lower one, if the other one is selected?如果选择了另一个,是否可以自动取消选择上一个或下一个? There must only be one selected radio button per vertical line.每条垂直线只能有一个选中的单选按钮。 Maybe with a radio group?也许与广播集团?
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);
}

setting AnchorStyles help you to keep your controls aligned and by proerties like padding or size you can define size of you elements if all radiobuttons ahave same width you can use size attribute.if they dont have a same size its better to use AnchorStyles.设置 AnchorStyles 可帮助您保持控件对齐,并且通过填充或大小等属性,您可以定义元素的大小,如果所有单选按钮具有相同的宽度,则可以使用 size 属性。如果它们的大小不同,则最好使用 AnchorStyles。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM