Answered by:
Get a value from a dynamically referenced Object??

Question
-
I have a collection of Text Boxes named _txtName1, _txtName2, _txtName3, etc..
I have a FOR loop that one at a time sends an email to each address in these boxes. i would like to create a STRING variable for the address during my loop following this logic:
//Count is set elsewhere based on the last entered address for (int i = 1; i <= Count; i++) {//gets the Text value of the textbox _txtName# where # is equal to i for the current loop string Address = ("_txtName" + i.ToString()).Text; SendEmail(Address) }
of course that won't work. but is there some way to achieve the desired result without IF's?
Answers
-
Sambeet's answer is probably what you're looking for, with the caveat that it will only work if all of your textboxes are directly inside the form (not in any panels, groupboxes, etc). If you want to get multiple layers deep, it may get a little uglier.
As a disclaimer, I don't think what you're asking for is actually a good idea. If you are creating a variable number of textboxes dynamically (i.e. creating textboxes in a loop and adding them to the form) you're better off keeping an array of them around and looping through that array. If you added the textboxes through the designer or you always have the same number of textboxes, you should just refer to each one by name.
In order to keep your code as maintainable as possible, you should only simplify things that actually need to be simplified. If you have 10 textboxes and you need to send emails for all 10, do the obvious thing - call SendEmail and pass in the text from each one individually. Putting it in a loop doesn't help readability and doesn't improve performance - it just cuts a few lines out of the code, but that really doesn't do anybody any good.
Check out My Blog for tech news, development tips, and other information for geeks like me.- Marked as answer by Bradlee S_ Friday, August 5, 2011 3:31 PM
-
Thanks all for the pointers.. However, I found it easiest to just do a group of IF/ELSE IF's since we're only working with 5 fields. Still would be nice to have a workable solution if that list was ever scaled up to say 20+fields...
- Marked as answer by Bradlee S_ Friday, August 5, 2011 3:30 PM
All replies
-
You can access controls by names. You have not specified whether it is a windows form or an asp.net page. Assuming that the textboxes reside in the form or the page, here is an idea
1) Forms:
Use the method this.Controls.Find method and pass the name as the key. You will get an Control[] array, take the first element [as you know that there is only one textbox] and type cast it to Textbox to then get the text:
Control[] colTextBoxes = this.Controls.Find("_txtName" + i.ToString(), true); TextBox myBox = (TextBox)colTextBoxes[0];
2) ASP.Net Page:
Exact same approach, except you have to use Page.FindControl method. This returns a single instance of control which you can convert to TextBox.
- Proposed as answer by Jackie-Sun Friday, August 5, 2011 8:50 AM
-
Sambeet's answer is probably what you're looking for, with the caveat that it will only work if all of your textboxes are directly inside the form (not in any panels, groupboxes, etc). If you want to get multiple layers deep, it may get a little uglier.
As a disclaimer, I don't think what you're asking for is actually a good idea. If you are creating a variable number of textboxes dynamically (i.e. creating textboxes in a loop and adding them to the form) you're better off keeping an array of them around and looping through that array. If you added the textboxes through the designer or you always have the same number of textboxes, you should just refer to each one by name.
In order to keep your code as maintainable as possible, you should only simplify things that actually need to be simplified. If you have 10 textboxes and you need to send emails for all 10, do the obvious thing - call SendEmail and pass in the text from each one individually. Putting it in a loop doesn't help readability and doesn't improve performance - it just cuts a few lines out of the code, but that really doesn't do anybody any good.
Check out My Blog for tech news, development tips, and other information for geeks like me.- Marked as answer by Bradlee S_ Friday, August 5, 2011 3:31 PM
-
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(TextBox))
{
SendEmail(((TextBox)c).Text)
}
}To get all controls and sub-controls recursively of text box type, use this extension method:
public static IEnumerable<TControl> GetChildControls(this Control control) where TControl : Control { var children = (control.Controls != null) ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>(); return children.SelectMany(c => GetChildControls(c)).Concat(children); }
And then you can access all text boxes by
foreach (TextBox tb in this.GetChildControls<TextBox>()) { SendEmail( tb.Text); }
My Blogs http://earningmoneyonbux.blogspot.com/
-
Thanks all for the pointers.. However, I found it easiest to just do a group of IF/ELSE IF's since we're only working with 5 fields. Still would be nice to have a workable solution if that list was ever scaled up to say 20+fields...
- Marked as answer by Bradlee S_ Friday, August 5, 2011 3:30 PM