473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How can I retrive all the textBox from Windows Form

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

Thank
Nov 16 '05 #1
5 13921
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
foreach(Control c in this.Controls)
if (c is TextBox)
MessageBox.Show(c.Name);
Nov 16 '05 #3
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Gidi | last post by:
Hi, For the last week, i'm looking for a way to make a TextBox always write in English (No matter what the OS default language is). i asked here few times but the answers i got didn't help me. i...
3
by: epigram | last post by:
I've tried asking this question several times and have received many good answers, but ones that don't quite answer my question. This leads me to believe that I am trying to do something very...
11
by: Keith | last post by:
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely reading them all - but I think I have a differing...
10
by: Dennis | last post by:
I have a simple form with one button and one text box. In the Form, I create an array list to track the events by adding a descriptive string item to the arraylist in each event. I first Click on...
3
by: DotNetNewbie | last post by:
I am reading the book Teach Yourself Microsoft Visual Basic .Net 2003 in 21 Days. I am having trouble getting one of the exercises to work at the end of day 4. Exercises: 1. Create a new...
3
by: Brad Rogers | last post by:
All, Being immersed in vb.net and trying CSharp after almost a year I forgot the differences. I like vb fixing the uppercase/lowercase names and seeming to be more flexible to code entry. ...
0
by: varaprasad204 | last post by:
hai all..., i used to store my mp3 file into database by <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <%@ import Namespace="System.IO" %> <%@...
1
by: ganesh22 | last post by:
Hi, Here the below code is for dynamically creating textboxs, its creating fine but after user enters some values in textboxs how can i retrive that values? using System; using System.Data;...
1
by: abirami elango | last post by:
Hi, i have created a web application in vb.net. i have assigned a value to the textbox during page UNLOAD event as below.. ......... Protected Sub Page_Unload(ByVal sender As Object, ByVal e As...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.