tags:

views:

203

answers:

3

how do i make a foreach loop from this, i want to change the size only of the textboxes that ends with txt2

        br1txt2.Size = new Size(27, 20);
        br2txt2.Size = new Size(27, 20);
        br3txt2.Size = new Size(27, 20);
        br4txt2.Size = new Size(27, 20);
        br5txt2.Size = new Size(27, 20);
+3  A: 
Size newSize = new Size(27, 20);
foreach (Control c in this.Controls)
{
   if (c is TextBox && c.Name.EndsWith("txt2"))
   {
      c.Size = newSize;
   }
}
Philip Wallace
so just test for c.Name.EndsWith("txt2")
Josh Pearce
Maybe you should check if c is a textbox?
Stephan Keller
Done. I missed that part on my first read!
Philip Wallace
very nice, thank you mate.
Darkmage
changed the "and" to || and it builds just fine if (c is TextBox || c.Name.EndsWith("txt2"))
Darkmage
Philip Wallace
great, thanks mate.
Darkmage
A: 

haven't done any winforms for years, but I think this might do the trick

IEnumerable<TextBox> textBoxes = GetTextBoxes() //Get your textboxes from wherever
Size newSize = new Size(27, 20);
foreach (Control c in textBoxes.Where(c=>c.Name.EndsWith("txt2")))
{
   c.Size = newSize;
}
borisCallens
A: 
TextBox[] tBoxesForSizeChange = new TextBox[2] {br1txt2, br2txt2 };
Size newSize = new Size(27, 20);

foreach(TextBox tBox in tBoxesForSizeChange)
{
   tBox.Size = newSize;
}

The code might not compile.
But, I hope it gives you idea to build further.

shahkalpesh