1

I have 16 text boxes in my Form whose names are suffixed sequentially from 1 to 16 respectively.

i.e. The 16 test boxes are names TextBox1, 'TextBox2, .... all the way until the 16th one, which is namedTextBox16`.

I would like to read the contents of these 16 text boxes in a loop and modify the ith TextBox's contents or properties based on a certain condition.

How do I do this?

Eugene S.
  • 3,256
  • 1
  • 25
  • 36
Kotz
  • 11
  • 1

4 Answers4

0

If you use WinForms, easiest way is to store text boxes references in array, in constructor of window: TextBox[] data = new TextBox[16]{textBox1,textBox2, [...],textBox16};

then you can use for loop to access them.

mjpolak
  • 721
  • 6
  • 24
0

You can try something like this:

    Dictionary<string, string> yourValues = new Dictionary<string, string>();
    foreach (Control x in this.Controls)
    {
        if (x is TextBox)
        {
            yourValues.Add(((TextBox)x).Name, ((TextBox)x).Text);
        }
    }

NOTE: On your future question please provide more information and make your question more clear.

NicoRiff
  • 4,803
  • 3
  • 25
  • 54
0

i would try to find and modify using Linq:

using System.Linq; 
//...

int x = 5; //number of textbox to search
var textbox = this.Controls.OfType<TextBox>().Single(tb => tb.Name.EndsWith(x.ToString()));
textbox.Text = "abc";

In case you have to loop thru all the texboxes in form, you can do something like this:

List<TextBox> textboxes = this.Controls.OfType<TextBox>().ToList();
foreach (TextBox box in textboxes)
{
    box.Text = "something";
}
Nino
  • 6,931
  • 2
  • 27
  • 42
0

Easiest way according to what you specified is to use Linq.

Let's assume you have 3 TextBoxes :

// 1st -> which is named meTextBox1
// 2nd -> which is named meTextBox2
// 3rd -> which is named meTextBox3

As you can see from above every line differs only by the number ( index .. call it whatever you want ).

Now you can make your base "query" which would look like :

const string TB_NAME = "meTextBox{0}";

And as you can presume right now this will be used inside of string.Format method. Now to retrieve desired TextBox all you have to do is to make Linq statement :

string boxName = string.Format(TB_NAME, 7); // retrieve 7th text box
TextBox tBox = Controls.OfType<TextBox>().FirstOrDefault(tb => tb.Name == boxName);

This example does not consider nested Controls but you can make do this recursively which will retrieve nested Controls:

TextBox ByIndex(int idx, Control parent)
{
    TextBox result = null;
    string searchFor = string.Format(TB_NAME, idx);
    foreach(Control ctrl in parent.Controls)
    {
        if(!(ctrl is TextBox) && ctrl.HasChildren)
        {
            result = ByIndex(idx, ctrl);
            if( result != null)
                break;
        }
        else if(ctrl is TextBox)
        {
            if(ctrl.Name = searchFor)
            {
                result = ctrl as TextBox;
                break;
            }
        }
    }
    return result;
}

To use above method you can just call it like such :

public class MeForm : Form
{
    //.. your code here ..

    void meImaginaryMethodToRetrieveTextBox()
    {
        int meRandomIndex = 7;
        TextBox tb = ByIndex(meRandomIndex, this);
        // rest of the code...
    }
}
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30