RadioButtons事件

时间:2011-02-22 13:00:05

标签: c# winforms

这是我的问题。

我有一些 RadioButtons 。如果我点击表单上的第一个 Radiobutton 创建 TextBox ,如果点击第二个 - 创建第二个 TextBox ,如果我再次点击 RadioButton ,又一个 TextBox ,是否可能?

请给我一点想法。

没有属性可见

3 个答案:

答案 0 :(得分:1)

这里动态创建和销毁控件没有多大意义。这只是一件令人头疼的事,确保位置,尺寸和标签顺序正确无误。如果您喜欢这个选项,只需让文本框可见:

    private void radioButton2_CheckedChanged(object sender, EventArgs e) {
        textBox1.Visible = radioButton2.Checked;
    }

在设计器中将文本框'Visible property设置为False。

答案 1 :(得分:0)

尝试这样的事情(这只是其中之一):

TextBox t;

private void radio_CheckedChanged(object sender, System.EventArgs e)
{
    if (radio.Checked) {
          t = new TextBox();
          t.Top = radio.Top;
          t.Left = radio.Left + radio.Width;
          this.Controls.Add(t);
          t.Show();
        } else {
          if (t!=null)t.Dispose();
        }       
}

答案 2 :(得分:0)

仅查看foreach和无效TextBoxes

using System;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;


class MyForm : Form
{
    private const int 
        HeightTextBox = 40, WidthTextBox = 25, //размер textboxes
        DistanceBetweenTexBoxHeight = 25, DistanceBetweenTexboxWigth = 25; //растояние между ними
    private int DimentionalTextBox = 3;
    private const int
        RadioButtonNumbers = 3, // количество радио кнопок
        DistanceBetweenRadiobutton = 50,
        RadioButtonFirstGroupStartPositionX = 5,
        RadioButtonSecondGroupStartPositionX = 0,
        RadioButtonFirstGroupStartPositionY = 0,
        RadioButtonSecondGroupStartPositionY = 0,
        RadioButtonSize = 25;

    public MyForm()
    {
        //Size of window
        ClientSize = new System.Drawing.Size(7 * HeightTextBox + 8 * DistanceBetweenTexBoxHeight,
            7 * WidthTextBox + 8 * DistanceBetweenTexboxWigth);

        //Create RaioButton
        int x = RadioButtonFirstGroupStartPositionX;
        int y;
        RadioButton[] DimRadioButtons = new RadioButton[RadioButtonNumbers];
        for (int i = 0; i < RadioButtonNumbers; i++)
        {
            DimRadioButtons[i] = new RadioButton();
            DimRadioButtons[i].Name = "RadioButton" + (i + 2);
            DimRadioButtons[i].Text = Convert.ToString(i + 2);
            DimRadioButtons[i].SetBounds(x, RadioButtonFirstGroupStartPositionY, RadioButtonSize, RadioButtonSize);
            x += DistanceBetweenRadiobutton;
            Controls.Add(DimRadioButtons[i]);
        }

        //Watch dimention
        // And catch even click on RadioButton
        foreach (var a in this.Controls)
        {
            if (a is RadioButton)
            {
                if (((RadioButton)a).Checked)
                {
                    DimentionalTextBox = Convert.ToInt16(((RadioButton)a).Text);
                    ((RadioButton)a).Click += new EventHandler(this.TextBoxes);
                }
            }
        }
    }

    // Create-Delete TextBoxes
    private void TextBoxes(object sender, EventArgs e)
    {
        RadioButton rb_click = (RadioButton)sender;
        int x = RadioButtonFirstGroupStartPositionX;
        int y = 30;
        int dim = Convert.ToInt16(rb_click.Text);
        TextBox[,] MatrixTextBoxes = new TextBox[dim, dim];
        for (int i = 0; i < dim; i++)
        {
            for (int j = 0; j < dim; j++)
            {
                MatrixTextBoxes[i, j] = new TextBox();
                MatrixTextBoxes[i, j].Top = rb_click.Top;
                MatrixTextBoxes[i, j].Name = "MatrixTextBox" + i + j;
                MatrixTextBoxes[i, j].Text = i + " " + j;
                MatrixTextBoxes[i, j].SetBounds(x, y, WidthTextBox, HeightTextBox);
                x += DistanceBetweenTexboxWigth;
                this.Controls.Add(MatrixTextBoxes[i, j]);
                MatrixTextBoxes[i, j].Show();
            }
            y += DistanceBetweenTexBoxHeight;
            x = RadioButtonFirstGroupStartPositionX;
        }
    }
}


class MyClassMain : MyForm
{
    public static void Main()
    {
        Application.Run(new MyClassMain());
    }
}