如何在c#中定义一个文本框数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10771048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:11:11  来源:igfitidea点击:

how to define an array of textboxes in c#?

c#arraysloopstextboxnaming

提问by Sarp Kaya

Hi when I create textboxes on Windows Application Form I cannot name it as box[0], box[1] and so on. The purpose why I want to do like this is because I want to use them in a loop.

嗨,当我在 Windows 应用程序表单上创建文本框时,我无法将其命名为 box[0]、box[1] 等。我这样做的目的是因为我想在循环中使用它们。

采纳答案by Sarp Kaya

Actually I found TextBox[] array = { firstTextBox, secondTextBox };works too!

其实我也找到了TextBox[] array = { firstTextBox, secondTextBox };作品!

回答by Jeff Halverson

How about making a list of them after you create them? In your form initialization function, you can do something like:

创建它们后,如何列出它们?在您的表单初始化函数中,您可以执行以下操作:

List<TextBox> myTextboxList = new List<TextBox>();
myTextBoxList.Add(TextBox1);
myTextBoxList.Add(TextBox2);
mytextBoxList.Add(TextBox3);

Now you can itterate through with your "myTextboxList" with something like below:

现在,您可以使用“myTextboxList”进行迭代,如下所示:

Foreach (TextBox singleItem in myTextboxList) {
    // Do something to your textboxes here, for example:
    singleItem.Text = "Type in Entry Here";
}

回答by SimpleVar

You can create textboxes on runtime and just put them in an array...

您可以在运行时创建文本框并将它们放入数组中...

If you want to do it in design time, you will have to do some control filtering logic on the whole this.Controlsarray in order to access only the wanted textboxes. Consider if (currControl is TextBox)if all textboxes in the form are ones you want in the array.

如果您想在设计时执行此操作,则必须对整个this.Controls数组执行一些控制过滤逻辑,以便仅访问所需的文本框。考虑if (currControl is TextBox)表单中的所有文本框是否都是您想要在数组中的文本框。

Another option for design time, is putting all wanted textboxes in a panel which will be their parent, and then iterating over the panel's children (controls) and cast them to TextBox.

设计时的另一个选择是将所有想要的文本框放在一个面板中,该面板将成为它们的父项,然后迭代面板的子项(控件)并将它们投射到 TextBox。

A runtime solution would be something like:

运行时解决方案类似于:

var arr = new TextBox[10];

for (var i = 0; i < 10; i++)
{
    var tbox = new TextBox();

    // tbox.Text = i.ToString();

    // Other properties sets for tbox

    this.Controls.Add(tbox);
    arr[i] = tbox;
}

回答by Md Kamruzzaman Sarker

TextBox[] t = new TextBox[10];
for(int i=0;i<required;i++)
{ 
   t[i]=new TextBox();
   this.Controls.Add(t[]);
}

回答by William Smith

I wouldn't use an array for this, personally. I would use some form of generic collection, like List.

我个人不会为此使用数组。我会使用某种形式的通用集合,比如 List。

    List<TextBox> textBoxList = new List<TextBox>();

    //Example insert method
    public void InsertTextBox(TextBox tb)
    {
        textBoxList.Add(tb);
    }

    //Example contains method
    public bool CheckIfTextBoxExists(TextBox tb)
    {
        if (textBoxList.Contains(tb))
            return true;
        else
            return false;
    }

You don't necessarily have to use the Contains method, you could also use Any(), or maybe even find another way- all depends on what you're doing. I just think using a generic collection gives you more flexibility than a simple array in this case.

您不一定必须使用 Contains 方法,您也可以使用 Any(),或者甚至可以找到另一种方法 - 这一切都取决于您在做什么。我只是认为在这种情况下使用泛型集合比简单的数组更灵活。

回答by Declan

for C#just use this to create an array of text boxes

对于C#只是用它来创建文本框的数组

public Text [] "YourName" = new Text ["how long you want the array"];

then add the text boxes to the array individually.

然后将文本框单独添加到数组中。

回答by Alex Jolig

TextBox Array using C#

使用 C# 的文本框数组

 // Declaring array of TextBox
private System.Windows.Forms.TextBox[] txtArray;

private void AddControls(int cNumber)
{

            // assign number of controls

            txtArray = new System.Windows.Forms.TextBox[cNumber + 1]; 

            for (int i = 0; i < cNumber + 1; i++)

            {

                        // Initialize one variable

                        txtArray[i] = new System.Windows.Forms.TextBox();

            }
}