简体   繁体   English

如何将动态创建的对象分配给字符串?

[英]How to assign dynamically created objects to a string?

I have a form with this code assigned to a button: 我有一个将此代码分配给按钮的表单:

    TextBox[] tbxCantServ = new TextBox[1];
    int i;

    for (i = 0; i < tbxCantServ.Length; i++)
    {
        tbxCantServ[i] = new TextBox();
    }

    foreach (TextBox tbxActualCant in tbxCantServ)
    {
        tbxActualCant.Location = new Point(iHorizontal, iVertical);
        tbxActualCant.Visible = true;
        tbxActualCant.Width = 44;
        tbxActualCant.MaxLength = 4;
        this.Controls.Add(tbxActualCant);
        iVertical = iVertical + 35;
    }

And this code creates textboxes dynamically, one for every "button click", so I can have an "add" button to call it and the user can write a list of things that is not limited. 这段代码动态创建了一个文本框,每个“按钮单击”都会创建一个文本框,因此我可以有一个“添加”按钮来调用它,用户可以编写不受限制的内容列表。

The question is: How can I assign these "textboxes.Text" to a string? 问题是:如何将这些“ textboxes.Text”分配给字符串? They haven't got a name :S 他们没有名字:S

something like: 就像是:

string sAllBoxes = tbx1.Text + tbx2.Text + "..." + tbxN.Text;

Thanks!! 谢谢!!

You can do it in the same way you created them. 您可以按照创建它们的相同方式进行操作。

Try this: 尝试这个:

string sAllBoxes="";
foreach (TextBox tbxActualCant in tbxCantServ)
{
     sAllBoxes+=tbxActualCant.Text;
}

OR 要么

Using a StringBuilder : 使用StringBuilder

StringBuilder textBuilder = new StringBuilder();
foreach (TextBox tbxActualCant in tbxCantServ)
{
     textBuilder.Append(tbxActualCant.Text);
}
string allText = textBuilder.ToString();

If your tbxCantServ is defined as local to a method, then you have to assign a Name to your TextBoxes like: 如果将tbxCantServ 定义为方法的本地方法,则必须为tbxCantServ分配一个Name ,例如:

int counter = 0;
foreach (TextBox tbxActualCant in tbxCantServ)
{
    tbxActualCant.Location = new Point(iHorizontal, iVertical);
    tbxActualCant.Name = "tbx" + counter++;
    tbxActualCant.Visible = true;
    tbxActualCant.Width = 44;
    tbxActualCant.MaxLength = 4;
    this.Controls.Add(tbxActualCant);
    iVertical = iVertical + 35;
}

And later in some other method if you want to get the joined text then you can do: 然后,如果要获取合并的文本,则可以使用其他方法:

string sAllBoxes = string.Join(",", this.Controls.OfType<TextBox>()
                                .Where(r => r.Name.StartsWith("tbx"))
                                .Select(r => r.Text));

But if you have tbxCantServ defined at class level then you can do: 但是,如果您在类级别定义了tbxCantServ ,则可以执行以下操作:

string sAllBoxes = string.Join(",", tbxCantServ
                                     .Where(r=> r != null)
                                     .Select(r => r.Text));

In string.Join , you can replace , with an empty string or any string depending on your requirement. string.Join ,可以更换,用一个空字符串或者根据您的要求的任何字符串。

If you have access to your textbox array, you can easily do this: 如果您有权访问文本框数组,则可以轻松执行以下操作:

string sAllBoxes = string.Join(" ", tbxCantServ.Select(x => x.Text));

If you don't then use Control collection of your Form , and give name to your textboxes so you can access them using this.Controls[txtBoxName] . 如果不这样做,则使用Form Control集合,并为文本框命名,以便您可以使用this.Controls[txtBoxName]进行访问。

If you just want to concatanate your texts without a separator, you can also use string.Concat method: 如果只想使用分隔符来合并文本,则也可以使用string.Concat方法:

string sAllBoxes = string.Concat(tbxCantServ.Select(x => x.Text));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM