0

我正在尝试创建一个 Windows 应用程序,我想在面板内单击按钮时显示一组控件(组合框、文本框和一个按钮)。

我已经创建了一个代码来创建一次控件,但我想在按钮单击一个下方的按钮时一次又一次地创建它们。

我正在使用的代码是

public partial class Employee_PayHeads_add : Form
{


private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ComboBox combohead = new ComboBox();



public Employee_PayHeads_add()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.btnAdd.BackColor = Color.Gray;
this.btnAdd.Text = "Remove";
this.btnAdd.Location = new System.Drawing.Point(240, 25);
this.btnAdd.Size = new System.Drawing.Size(70, 25);


this.txtBox.Text = "";
this.txtBox.Location = new System.Drawing.Point(150, 25);
this.txtBox.Size = new System.Drawing.Size(70, 40);

this.combohead.Location = new System.Drawing.Point(10, 25);

panel1.Controls.Add(btnAdd);
panel1.Controls.Add(txtBox);
panel1.Controls.Add(combohead);


}

如果数字控件与空间重叠,我还希望面板中有一个垂直滚动条。

提前致谢

4

3 回答 3

0

在按钮单击事件中创建新对象,而不是使用您之前声明的对象。

尝试这样的事情:

public partial class Employee_PayHeads_add : Form
{
    private TextBox txtBox = new TextBox();
    private Button btnAdd = new Button();
    private ComboBox combohead = new ComboBox();

    private int txtBoxStartPosition = 150;
    private int btnAddStartPosition = 240;
    private int comboheadStartPosition = 10;
}

public Employee_PayHeads_add()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    TextBox newTxtBox = new TextBox();
    Button newBtnAdd = new Button();
    ComboBox newCombohead = new ComboBox();

    newBtnAdd.BackColor = Color.Gray;
    newBtnAdd.Text = "Remove";
    newBtnAdd.Location = new System.Drawing.Point(btnAddStartPosition, 25);
    newBtnAdd.Size = new System.Drawing.Size(70, 25);

    newTxtBox.Text = "";
    newTxtBox.Location = new System.Drawing.Point(txtBoxStartPosition, 25);
    newTxtBox.Size = new System.Drawing.Size(70, 40);

    newCombohead.Location = new System.Drawing.Point(comboheadStartPosition, 25);

    panel1.Controls.Add(newBtnAdd);
    panel1.Controls.Add(newTxtBox);
    panel1.Controls.Add(newCombohead);

    txtBoxStartPosition += 50;
    btnAddStartPosition += 50;
    comboheadStartPosition += 50;
}
于 2013-05-24T06:23:44.607 回答
0

我还没有尝试过你的代码,但它表明它总是在每个单击事件上创建新控件,但是由于你已经指定了按钮的硬编码位置,所以它必须创建相互重叠的新控件。因此您可以动态更改位置并希望它会起作用

于 2013-05-24T06:27:24.697 回答
-1

如果您想一次又一次地添加控件,则必须创建新控件。因此,与其以您的形式定义它们,不如您必须:

private void button1_Click(object sender, EventArgs e)
{
   Button btnAdd = new Button();
   btnAdd.BackColor = Color.Gray;
   btnAdd.Text = "Remove";
   btnAdd.Location = new System.Drawing.Point(240, 25);
   btnAdd.Size = new System.Drawing.Size(70, 25);

   TextBox txtBox = new TextBox();
   txtBox.Text = "";
   txtBox.Location = new System.Drawing.Point(150, 25);
   txtBox.Size = new System.Drawing.Size(70, 40);

   ComboBox combohead = new ComboBox();
   combohead.Location = new System.Drawing.Point(10, 25);

   panel1.Controls.Add(btnAdd);
   panel1.Controls.Add(txtBox);
   panel1.Controls.Add(combohead);
}

现在你可以在你的类之上删除那些私有声明。

于 2013-05-24T06:22:34.377 回答