简体   繁体   中英

C# Write each array element to different textbox on form

Hi all hoping for some direction. I have a text file that I am reading into a C# Form as a simple string array. I want to take each element and put them into their own text box on the form. I know I can do it as

textBox1.Text = myArray[0];
textBox2.Text = myArray[1]; 

and so forth but I was hoping for some sort of for or foreach statement to iterate through the array and textboxes.

For reference the form has 50 boxes on it and the array will always have 50 elements.

Thanks in advance!

Update, I appreciate both comments it got me pointed in the right direction. I was really struggling to think through it. What I ended up coming up with is:

   //Read Data
        string[] lines = File.ReadAllLines(Globals.savePath);


        //Write Data To Form
        for (int i = 1; i <= 20; i++)
        {
            TextBox textBox = (TextBox)groupBox1.Controls["textBox" + i];
            textBox.Text = lines[i-1];
        }
        for (int i = 21; i <= 40; i++)
        {
            TextBox textBox = (TextBox)groupBox2.Controls["textBox" + i];
            textBox.Text = lines[i - 1];
        }
        for (int i = 41; i <= 60; i++)
        {
            TextBox textBox = (TextBox)groupBox3.Controls["textBox" + i];
            textBox.Text = lines[i - 1];
        }
        for (int i = 61; i <= 80; i++)
        {
            TextBox textBox = (TextBox)groupBox4.Controls["textBox" + i];
            textBox.Text = lines[i - 1];
        }

I am sure there is a cleaner way to do this. But this worked for me and was much better then doing the whole

textbox1.Text = myArray[0] 

Plus it had the added benefit of translating over when to the next part where I was editing text boxes and than writing back to the text file.

           string[] lines = new string [81];
        for (int i = 1; i <= 20; i++)
        {
            TextBox textBox = (TextBox)groupBox1.Controls["textBox" + i];
            lines[i - 1] = textBox.Text;
        }
        for (int i = 21; i <= 40; i++)
        {
            TextBox textBox = (TextBox)groupBox2.Controls["textBox" + i];
            lines[i - 1] = textBox.Text;
        }
        for (int i = 41; i <= 60; i++)
        {
            TextBox textBox = (TextBox)groupBox3.Controls["textBox" + i];
            lines[i - 1] = textBox.Text;
        }
        for (int i = 61; i <= 80; i++)
        {
            TextBox textBox = (TextBox)groupBox4.Controls["textBox" + i];
            lines[i - 1] = textBox.Text;
        }
        lines[81] = "End";
        File.Delete(Globals.savePath);
        File.WriteAllLines(Globals.savePath, lines);

Once again Thank You to those that commented!

This is my first attempt at an answer, so fingers crossed this is helpful. I would store the textboxes in a list, then when you loop through your array, just do something like the following:

var maxItems = myArray.Length;
for(int loop = 0; loop < maxItems; loop++)
{
     textBoxItemList[loop].Text = myArray[loop];
}

This could be tweaked to determine the loop size using the smallest array size, so you didn't go out of bounds. The maxItems is there just as an example of where you would set this up. Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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