Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding control on form / Setting text value of TextBox 1

Status
Not open for further replies.

Amesville

Programmer
Oct 10, 2011
93
US
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
 
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:
                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
 
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
 
Thank you Rhys, that makes sense - I will try it out tonight when I get home.

Craig
 
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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top