By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,393 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

How can I retrive all the textBox from Windows Form

P: n/a
Fox
foreach (TextBox tb in this.Controls)
MessageBox.Show(tb.Name);
//Error Occur for non-textBox, i.e. Button

Thank
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Fox
I may find the answer.

for (int i=0;i<this.Controls.Count;i++)
if
(this.Controls[i].GetType().ToString()=="System.Windows.Forms.TextB ox")
this.Controls[i].Text = "OK";
Any better Method?
"Fox" <fox> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
foreach (TextBox tb in this.Controls)
MessageBox.Show(tb.Name);
//Error Occur for non-textBox, i.e. Button

Thank

Nov 16 '05 #2

P: n/a
foreach(Control c in this.Controls)
if (c is TextBox)
MessageBox.Show(c.Name);
Nov 16 '05 #3

P: n/a
Hi Fox,

As Adrian said, use foreach Control and check for TextBox, but beware this
will only find top level TextBoxes, not TextBoxes inside GroupBoxes,
Panels, TabPages or other container controls. If you have container
controls you may need to do a recursive search.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

P: n/a
Fox
Thank
"Adrian" <ma******@yahoo.com> wrote in message
news:ek**************@TK2MSFTNGP15.phx.gbl...
foreach(Control c in this.Controls)
if (c is TextBox)
MessageBox.Show(c.Name);

Nov 16 '05 #5

P: n/a
Fox,
Remember that you may have to descend into subgroups doing this. If you
have a groupbox on a form the textboxes inside it will be in its' controls
collection not the overall form's Controls collecton.

This will work although it hasn't been optimized

ArrayList boxes = new ArrayList(5);
foreach (Control c in this.Controls)
{
foreach (TextBox tb in GetTextBoxControls(c))
{
boxes.Add(tb);
}
}

private ArrayList GetTextBoxControls(Control c)
{
ArrayList x = new ArrayList(1);
if (c.Controls.Count > 0)
{
foreach (Control c1 in c.Controls)
{
ArrayList y = GetTextBoxControls(c1);
foreach (TextBox tmp in y)
{
x.Add(tmp);
}
}
}
else
{
if (c is TextBox)
{
x.Add(c);
}
}
return x;
}

Ron Allen
"Fox" <fox> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Thank
"Adrian" <ma******@yahoo.com> wrote in message
news:ek**************@TK2MSFTNGP15.phx.gbl...
foreach(Control c in this.Controls)
if (c is TextBox)
MessageBox.Show(c.Name);


Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.