I need to copy 160 textbox's text and put it onto the clipboard.

Here is what I have so far:

List<TextBox> boxes = new List<TextBox>() { textBoxX1, textBoxX2, textBoxX3, textBoxX4, textBoxX5, textBoxX4, textBoxX4, textBoxX5, textBoxX6, textBoxX7, textBoxX8, textBoxX9, textBoxX10, textBoxX11, textBoxX12, textBoxX13, textBoxX14, textBoxX15, textBoxX16, textBoxX17, textBoxX18, textBoxX19, textBoxX20, textBoxX21, textBoxX22, textBoxX23, textBoxX25, textBoxX26, textBoxX27, textBoxX28, textBoxX29, textBoxX30, textBoxX31, textBoxX32, textBoxX33, textBoxX34, textBoxX35, textBoxX36, textBoxX37, textBoxX38, textBoxX39, textBoxX40, textBoxX41, textBoxX42, textBoxX43, textBoxX44, textBoxX45, textBoxX46, textBoxX47, textBoxX48, textBoxX49, textBoxX50, textBoxX51, textBoxX52, textBoxX53, textBoxX54, textBoxX55, textBoxX56, textBoxX57, textBoxX58, textBoxX59, textBoxX60, textBoxX61, textBoxX62, textBoxX63, textBoxX64, textBoxX65, textBoxX66, textBoxX67, textBoxX68, textBoxX69, textBoxX70, textBoxX71, textBoxX72, textBoxX73, textBoxX74, textBoxX75, textBoxX76, textBoxX77, textBoxX78, textBoxX79, textBoxX80, textBoxX81, textBoxX82, textBoxX83, textBoxX84, textBoxX85, textBoxX86, textBoxX87, textBoxX88, textBoxX89, textBoxX90, textBoxX91, textBoxX92, textBoxX93, textBoxX94, textBoxX95, textBoxX96, textBoxX97, textBoxX98, textBoxX99, textBoxX100, textBoxX101, textBoxX102, textBoxX103, textBoxX104, textBoxX105, textBoxX106, textBoxX107, textBoxX108, textBoxX109, textBoxX110, textBoxX111, textBoxX112, textBoxX113, textBoxX114, textBoxX115, textBoxX116, textBoxX117, textBoxX118, textBoxX119, textBoxX120, textBoxX121, textBoxX122, textBoxX123, textBoxX124, textBoxX125, textBoxX126, textBoxX127, textBoxX128, textBoxX129, textBoxX130, textBoxX131, textBoxX132, textBoxX133, textBoxX134, textBoxX135, textBoxX136, textBoxX137, textBoxX138, textBoxX139, textBoxX140, textBoxX141, textBoxX142, textBoxX143, textBoxX144, textBoxX145, textBoxX146, textBoxX147, textBoxX148, textBoxX149, textBoxX150, textBoxX151, textBoxX152, textBoxX153, textBoxX154, textBoxX155, textBoxX156, textBoxX157, textBoxX158, textBoxX159, textBoxX160};
            var query = from textBoxX in boxes
            select textBoxX.Text;
            string combinedText = string.Join(string.Empty, query.ToArray());

Bloody hell.
Right after the form1:form, put:

TextBox[] textboxes = new TextBox[160]

Or, if you do not have a fixed amount, do it in a list, as in your example.
Under the InitializeComponent(), put:

for (int i = 0; i < 160 /*LIST.Count if you used a list*/; i++)
{
    TextBox textboxes[i] = new TextBox;
    textboxes[i].Tag = i; // Should you need to identify it
    // Set the parameters, such as width, height
    // Stack them on top of each other:
    textboxes[i].Top = i * textboxes[i].Height;
}

If you wish to get all the text, use:

private string GetText()
{
    string str = "";
    foreach (TextBox tb in textboxes)
        str += tb.Text;
    return str;
}

To set the clipboard text to that, use:

Clipboard.SetText(Getext());

I didn't type this in the IDE, so sorry about errors. This method is much better- having so many textboxes individually created is very messy.

Edited 6 Years Ago by bbman: n/a

Comments
Good suggestion!

Could you revise this part?

for (int i = 0; i < 160 /*LIST.Count if you used a list*/; i++)
{
    TextBox textboxes[i] = new TextBox;
    textboxes[i].Tag = i; // Should you need to identify it
    // Set the parameters, such as width, height
    // Stack them on top of each other:
    textboxes[i].Top = i * textboxes[i].Height;
}

Could you revise this part?

for (int i = 0; i < 160 /*LIST.Count if you used a list*/; i++)
{
    TextBox textboxes[i] = new TextBox;
    textboxes[i].Tag = i; // Should you need to identify it
    // Set the parameters, such as width, height
    // Stack them on top of each other:
    textboxes[i].Top = i * textboxes[i].Height;
}

If you go into designer.cs (expand the form in the solution browser), there should be a lot of stuff that creates the components. What I usually do is copy that code and paste it into the loop, and for stuff that is not consistent, such as the top, I just times a value by i, so that there is not a massive overlap. You will need to set the width.
I think I forgot to add, at the bottom (still in the loop), you must add

this.Controls.Add(textboxes[i]);

Otherwise it doesn't actually add it.
Hope this helps.

Just a quick note, if you're going to add events, in the event, to get the textbox, you would use the following:

private void TextBoxClick(object sender, EventArgs e)
{
((TextBox)sender).etc.
}

Edited 6 Years Ago by bbman: n/a

This article has been dead for over six months. Start a new discussion instead.