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
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
CODE --> C#
Again any help would be greatly appreciated.
Craig
RE: Finding control on form / Setting text value of TextBox
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
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
Craig
RE: Finding control on form / Setting text value of TextBox
CODE
Of course, sCtrlName and sTrackGroup are strings defined at a higher level. This works really well though, thank you very much!