动态地向一行添加两个radiobutton和一个regular Button



我试图动态添加两个单选按钮和一个常规按钮到每一行。我试过使用this.Controls. adrange和this.Controls。然而,两者都在各自的行上有每个按钮。我该怎么做才能让它们都在同一行呢?

下面是我的代码:
        AddRow();

        RadioButton jumper = new RadioButton();
        RadioButton shortPin = new RadioButton();
        jumper.Text = "Jumper";
        shortPin.Text = "Short";
        this.Controls.AddRange(new Control[] { shortPin, jumper});
        cellIndex++;
AddRow代码
        public bool AddRow()
    {
        bool retval = true;
        this.RowStyles.Add(new RowStyle(SizeType.Absolute,20));
        this.RowCount = this.RowStyles.Count;
        return retval;
    }

您需要设置控件的Location

为了保持控件在同一行,您需要为两个单选按钮设置相同的y轴。

试试这个:

RadioButton jumper = new RadioButton();
jumper.Location = new System.Drawing.Point(10, 20);//x,y axis
jumper.Size = new System.Drawing.Size(80, 30); //width,height
jumper.Text = "Jumper";
RadioButton shortPin = new RadioButton();
//change x-axis but keep same y-axis
shortPin.Location = new System.Drawing.Point(100, 20);
shortPin.Size = new System.Drawing.Size(80, 30); //width,height       
shortPin.Text = "Short";
this.Controls.AddRange(new Control[] { shortPin, jumper});

最新更新