winforms 如何根据整数变量的值动态创建许多标签和文本框?

ht4b089n  于 2023-05-18  发布在  其他
关注(0)|答案(4)|浏览(133)

当我们知道'n'的值后,例如单击“显示”按钮,是否有任何方法动态创建和显示'n'个标签与'n'个相应的文本框。
如果有什么不明白的地方,请告诉我。谢谢大家!
VS C# Express 2010 Windows Form

woobm2wo

woobm2wo1#

我会创建一个用户控件,其中包含一个标签和一个文本框,并简单地创建该用户控件的示例'n'次。如果你想知道一种更好的方法来做到这一点,并使用属性从用户控件中获得标签和文本框的值,请告诉我。
简单的方法是:

int n = 4; // Or whatever value - n has to be global so that the event handler can access it

private void btnDisplay_Click(object sender, EventArgs e)
{
    TextBox[] textBoxes = new TextBox[n];
    Label[] labels = new Label[n];

    for (int i = 0; i < n; i++)
    {
        textBoxes[i] = new TextBox();
        // Here you can modify the value of the textbox which is at textBoxes[i]

        labels[i] = new Label();
        // Here you can modify the value of the label which is at labels[i]
    }

    // This adds the controls to the form (you will need to specify thier co-ordinates etc. first)
    for (int i = 0; i < n; i++)
    {
        this.Controls.Add(textBoxes[i]);
        this.Controls.Add(labels[i]);
    }
}

上面的代码假设你有一个按钮btnDisplay,它有一个onClick事件分配给btnDisplay_Click事件处理程序。您还需要知道n的值,并且需要一种方法来确定将所有控件放置在哪里。控件还应该指定宽度和高度。
要使用用户控件执行此操作,只需执行以下操作。
好的,首先创建一个新的用户控件,并在其中放置一个文本框和标签。
我们称之为txtSomeTextBoxlblSomeLabel。在后面的代码中添加以下代码:

public string GetTextBoxValue() 
{ 
    return this.txtSomeTextBox.Text; 
} 

public string GetLabelValue() 
{ 
    return this.lblSomeLabel.Text; 
} 

public void SetTextBoxValue(string newText) 
{ 
    this.txtSomeTextBox.Text = newText; 
} 

public void SetLabelValue(string newText) 
{ 
    this.lblSomeLabel.Text = newText; 
}

现在生成用户控件的代码如下所示(MyUserControl是您给予用户控件的名称):

private void btnDisplay_Click(object sender, EventArgs e)
{
    MyUserControl[] controls = new MyUserControl[n];

    for (int i = 0; i < n; i++)
    {
        controls[i] = new MyUserControl();

        controls[i].setTextBoxValue("some value to display in text");
        controls[i].setLabelValue("some value to display in label");
        // Now if you write controls[i].getTextBoxValue() it will return "some value to display in text" and controls[i].getLabelValue() will return "some value to display in label". These value will also be displayed in the user control.
    }

    // This adds the controls to the form (you will need to specify thier co-ordinates etc. first)
    for (int i = 0; i < n; i++)
    {
        this.Controls.Add(controls[i]);
    }
}

当然,您可以在usercontrol中创建更多方法来访问属性并设置它们。或者简单地说,如果你必须访问很多,只需输入这两个变量,你就可以直接访问文本框和标签:

public TextBox myTextBox;
public Label myLabel;

在用户控件的构造函数中执行以下操作:

myTextBox = this.txtSomeTextBox;
myLabel = this.lblSomeLabel;

然后在你的程序中,如果你想修改任何一个的文本值,就这样做。

control[i].myTextBox.Text = "some random text"; // Same applies to myLabel

希望有帮助:)

mkshixfv

mkshixfv2#

下面是一个简单的示例,它可以让您继续添加一些可以充当占位符到winform中的东西,例如TableLayoutPanel
然后添加控件

for ( int i = 0; i < COUNT; i++ ) {

    Label lblTitle = new Label();
    lblTitle.Text = i+"Your Text";
    youlayOut.Controls.Add( lblTitle, 0, i );

    TextBox txtValue = new TextBox();
    youlayOut.Controls.Add( txtValue, 2, i );
}
bxpogfeg

bxpogfeg3#

假设你有一个按钮,当按下时,它将n设置为5,你就可以像这样在表单上生成标签和文本框。

var n = 5;
for (int i = 0; i < n; i++)
{
    //Create label
    Label label = new Label();
    label.Text = String.Format("Label {0}", i);
    //Position label on screen
    label.Left = 10;
    label.Top = (i + 1) * 20;
    //Create textbox
    TextBox textBox = new TextBox();
    //Position textbox on screen
    textBox.Left = 120;
    textBox.Top = (i + 1) * 20;
    //Add controls to form
    this.Controls.Add(label);
    this.Controls.Add(textBox);
}

这不仅会将它们添加到表单中,而且还会将它们适当地定位。

w1e3prcc

w1e3prcc4#

你可以试试这个:

int cleft = 1;
intaleft = 1;
private void button2_Click(object sender, EventArgs e) 
{
     TextBox txt = new TextBox();
     this.Controls.Add(txt);
     txt.Top = cleft * 40;
     txt.Size = new Size(200, 16);
     txt.Left = 150;
     cleft = cleft + 1;
     Label lbl = new Label();
     this.Controls.Add(lbl);
     lbl.Top = aleft * 40;
     lbl.Size = new Size(100, 16);
     lbl.ForeColor = Color.Blue;
     lbl.Text = "BoxNo/CardNo";
     lbl.Left = 70;
     aleft = aleft + 1;
     return;
}
private void btd_Click(object sender, EventArgs e)
{ 
    //Here you Delete Text Box One By One(int ix for Text Box)
    for (int ix = this.Controls.Count - 2; ix >= 0; ix--)
    //Here you Delete Lable One By One(int ix for Lable)
    for (int x = this.Controls.Count - 2; x >= 0; x--)
    {
        if (this.Controls[ix] is TextBox) 
        this.Controls[ix].Dispose();
        if (this.Controls[x] is Label) 
        this.Controls[x].Dispose();
        return;
    }
}

相关问题