1

i was just wondering if something like this is possible:

for(int i = 0; i < 7; i++)
{
    textbox + i + .text = aString;
}

I want to change a piece of code to work on multiple textboxes, without having to type the whole code 6 times. does anyone know if this is possible and how? Thanks :3

Bv dL
  • 61
  • 1
  • 6
  • 2
    Possible duplicate of [Create dynamic variable name](http://stackoverflow.com/questions/20857773/create-dynamic-variable-name) – Matthias Jan 07 '16 at 16:12
  • There are better ways of getting a group of controls, you'd be better asking about that. Although, show your research alongside this new question. – Sayse Jan 07 '16 at 16:13
  • looks complicated, but thanks :) – Bv dL Jan 07 '16 at 16:14
  • The title sounds like it's a different question, by the way. – Mr Lister Jan 07 '16 at 16:16
  • If you are going to operate over collection of controls than you should stop using their name (or use them indirectly, e.g. as dictionary key). Very basic array of controls will allow to perform you operation over many controls by using **index**. – Sinatr Jan 07 '16 at 16:16

5 Answers5

1

in C# you can find your control in your page aspx.

Example:

for (int i = 0; i < 10; i++)
 {

     TextBox textBox = this.Page.FindControl("textbox" + i.ToString()) as TextBox;
     if (textBox != null)
     {
        textBox.Text = "change Text";
     }
  }
rdn87
  • 739
  • 5
  • 18
0

Well, just telling you that i'm newbie....

public void AddStringToTextbox(int i, string value)
{
 switch(i)
 {
 case: 0:
 break;

 case: 1:
 textbox1.text = value;
 break;

 case: 2:
 textbox2.text = value;
 break;

 case: 3:
 textbox3.text = value;
 break;

 //and more
 }
}

//how to use

for(int i = 0; i < 7; i++)
{
 AddStringToTextbox(i, aString);
}
Fumy
  • 1
0

Try this one for Winforms, should be a bit safer.

      var textBoxes = this.Controls.OfType<TextBox>();

      for (int i = 0; i < 10; i++)
      {
        var textBox = textBoxes.FirstOrDefault<TextBox>(t => t.Name.Equals("textBox" + i.ToString()));
        if(textBox != null)
        {
          textBox.Text = i.ToString();
        }
      }
KMB
  • 473
  • 1
  • 5
  • 15
0

Try putting your controls in an array or list and reference them from there. E.g.:

public class MyClass
{
    private List<TextBox> textboxes;

    public MyClass()
    {
        this.InitializeComponent();
        textboxes = new List<TextBox>(){ textbox1, textbox2, textbox3 };
    }

    private void UpdateTextBoxes(string aString)
    {
        for(var i = 0; i < textboxes.Count; i++)
        {
            textboxes[i].Text = aString;
        }
    }
}

Or, if it is a simple an update as your question suggests, try

textbox1.Text = 
textbox2.Text = 
textbox3.Text = 
textbox4.Text = 
textbox5.Text = 
textbox6.Text = 
textbox7.Text = 
    aString;
user1325543
  • 523
  • 3
  • 12
0

May be this help you. Make a list of TextBox and use where ever you need.

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

        textBoxs.Add(textBox1);
        textBoxs.Add(textBox2);
        textBoxs.Add(textBox3);
        textBoxs.Add(textBox4);
        textBoxs.Add(textBox5);
        textBoxs.Add(textBox6);
        textBoxs.Add(textBox7);

        for (int i = 0; i < 7; i++)
        {
            textBoxs[i].Text = aString;
        }
arshad
  • 1
  • 2