开发者

foreach loop with textbox names

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);


Size newSize = new Size(27, 20);
foreach (Control c in this.Controls)
{
   if (c is TextBox && c.Name.EndsWith("txt2"))
   {
      c.Size = newSize;
   }
}


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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜