×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Finding control on form / Setting text value of TextBox

Finding control on form / Setting text value of TextBox

Finding control on form / Setting text value of TextBox

(OP)
Hi Folks

Working on a C# app to practice - I've been a VB.NET programmer for years and I'm trying to get used to working in C# now.

The app I'm writing includes over 80 text boxes in about 8 groups, each group in more or less in a grid pattern. They are named "txt<GroupName>1_1" for the box in the upper left corner, then "txt<GroupName>1_2" for the next box to the right and so on. The values to be displayed in the boxes are stored in an array in the code behind.

When I want to update the text value in a specific textbox I am having issues. I can derive the correct textbox name easily, but to update it I believe I need to iterate through the Controls collection (using a foreach function) until I find the correct textbox in order to get to its Text property. I know I have done this in VB by looking for the control type (because there are labels and command buttons on the form as well) and then reading its name - but I can't seem to find the control type as a property on the object in the Controls collection. Don't know what I'm doing wrong.

Can someone point me to the proper way to do this, or at least help me find the control type property?

Thanks

Craig

RE: Finding control on form / Setting text value of TextBox

(OP)
OK so I've found a better way to do this (I think) using the controls collection and the Find method of it. I have the following code example below, and there is a text box on the form with the name "txtPW1_1". The null test says it isn't null, but the read on the first element of the array TB blows up. Again, not sure where I've gone wrong here...


CODE --> C#

for (int X = 1; X < 7; X++)
                {
                    for (int Y = 1; Y < 5; Y++)
                    {
                        ControlCollection Inst = new ControlCollection(this);
                        Control [] TB;
                        string sCtrlName = "txtPW" + X + "_" + Y;
                        TB = Inst.Find(sCtrlName, true);
                        if (TB != null)
                        {
                            if (TB[0].Text != "")
                            {
                                Byte[] info = new UTF8Encoding(true).GetBytes(sCtrlName + "," + TB[0].Text);
                                fs.Write(info, 0, info.Length);
                            }
                        }
                    }
                } 

Again any help would be greatly appreciated.

Craig

RE: Finding control on form / Setting text value of TextBox

Windows or Web forms?...

Either way you should be able to directly iterate the controls, (and their child controls), on a page or form. Both the System.Web.UI.Page and System.Windows.Forms.Form class have a 'Controls' property, which is Control Collection object. What you'll need to do is a two phase iteration along the lines of the below, walking through the tree of control collections, checking type of control and processing appropriately. I'm not going to worry about what you are trying to acheive or best practice etc. as, (as you say), you're doing this as a learning exercise so have a look at the below and see if it makes any sense...

...also, be aware that the below is untested or checked, I've just scratched it up from memory.

CODE

private void IterateControls()
{
	IterateControl(this as Control);
}

private void IterateControl(Control control)
{
	if (control.Controls != null && control.Controls.Count > 0)
	{
		foreach (Control childControl in control.Controls)
		{
			IterateControl(childControl);
		}
	}

	if (control is TextBox)
	{
		// Do something here...
		string text = (control as TextBox).Text;
	}
} 

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein

RE: Finding control on form / Setting text value of TextBox

(OP)
Thank you Rhys, that makes sense - I will try it out tonight when I get home.

Craig

RE: Finding control on form / Setting text value of TextBox

(OP)
Well you definitely put me on the right track - I eventually handled it thusly:

CODE

sTrackGroup = "PW";                
      for (int X = 1; X < 7; X++)
          {
              for (int Y = 1; Y < 5; Y++)
              {
                  sCtrlName = "txt" + sTrackGroup + X + "_" + Y;
                  TextBox TB = this.Controls.Find(sCtrlName, true).FirstOrDefault() as TextBox;
                  if (TB != null && TB.Text.Length > 0)
                  {
                      writer.WriteLine(sCtrlName + "," + TB.Text);
                  }
              }
          } 

Of course, sCtrlName and sTrackGroup are strings defined at a higher level. This works really well though, thank you very much!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close