I've 10 text boxes txt_Address1, txt_Address2...txt_Address10 and 10 columns to store their values in database i.e. Address1, Address2...Address10. Now I want to get each value of text box and store it into its corresponding column. For this rather than writing 10 lines of code for each text box, I want to do it by FOR loop. Can anybody suggest me the suitable solution?
-
you can create an array and add all the txt_Ad... to it. TextBox[] textBoxes = new TextBox[] { txt_Addr1, txt_Addr2,...}. And loop over that.hawk– hawk2013-11-11 06:34:08 +00:00Commented Nov 11, 2013 at 6:34
-
see my answer, you can add all your textbox into a list or added it into an array and loop it, the TextBox is a reference type which means that declaring a TextBox a = txt_address10 does not make a copy of it, instead it creates a pointer to txt_address10lostincomputer2– lostincomputer22013-11-11 06:36:37 +00:00Commented Nov 11, 2013 at 6:36
-
Make sure you really want to do this. People are telling you how, but it looks like a bad database design and a consequent waste of time. See my answer below.user1646737– user16467372013-11-11 06:54:02 +00:00Commented Nov 11, 2013 at 6:54
-
@user1646737 But this is how my client wants me to do.Shilpa Soni– Shilpa Soni2013-11-11 06:56:00 +00:00Commented Nov 11, 2013 at 6:56
-
Sometimes, you have to follow orders, but I'd explain the problem to the client and tell him how a modest change can make his end product infinitely more versatile. If he declines, then, you can write all the poor code for a poorly-designed database.user1646737– user16467372013-11-11 06:57:29 +00:00Commented Nov 11, 2013 at 6:57
8 Answers
When you create the textboxes, store them in a collection
List<TextBox> textboxControls = new List<TextBox>();
Then when you create them, add them to the collection
textboxControls.Add(control);
Then you can loop over them and access their values
foreach(var control in textboxControls )
DoSomethingWithText(control.Text);
Comments
Just reference the textbox in an array of TextBox;
TextBox txt1 = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();
TextBox txtN = new TextBox();
TextBox[] allTextBoxes = new TextBox[] { txt1, txt2, txt3, txtN };
foreach(TextBox item in allTextBoxes)
{
StoreValue(item.Text);
}
OR you can use
List<TextBox> lst = new List<TextBox>();
lst.Add(txt1);
lst.Add(txt2);
lst.Add(txt3);
foreach(TextBox item in lst)
{
StoreValue(item.Text);
}
3 Comments
Before you spend (waste) time writing code to work for a database designed like that, you should re-design your database. It is not a good idea to have 10 columns in a table for 10 addresses. You should be able to have anywhere from 0 - infinity addresses. Look up how to make a relational database.
Basically:
Table: Customer
CustomerID
Name
Etc.
Table: CustomerAddresses
CustomerID
Address
City
State
Zip
Comments
You can use Controls.Find() like this:
for (int i = 1; i <= 10; i++)
{
Control[] matches = this.Controls.Find("txt_Address" + i.ToString(), true);
if (matches.Length > 0 && matches[0] is TextBox)
{
TextBox tb = (TextBox)matches[0];
// ... do something with "tb" ...
}
}
4 Comments
Or you can access them from the form without the list:
foreach(Control control in MyForm.Controls)
{
if(control is TextBox)
{
//do what you want
}
}
Or if you have them in a groupBox
foreach(Control control in myGroupBox.Controls)
{
if(control is TextBox)
{
//do what you want
}
}
Hope this helps!
Or with the FOR loop:
//Controls is the Controls collection of the form
for(int i=0;i<Controls.Count;i++)
{
if(Controls[i] is TextBox)
{
//do what you want
}
}
1 Comment
control as a TextBox in this case. Perhaps it will be better to cast is first control as Textbox and do a null check. This way you only cast once.var columns = new Dictionary<string, string>();
for (int i = 1; i <= 10; i++) columns.Add("Address" + i, string.Empty);
var textBoxes = Controls.Find("txt_Address", true).Where(t => t is TextBox).ToList();
columns.ToList().ForEach(c =>
{
var index = c.Key.Replace("Address", string.Empty);
var textBox = textBoxes.FirstOrDefault(t => index.Equals(t.Name.Replace("txt_Address", string.Empty)));
if (textBox != null) columns[c.Key] = textBox.Text;
});
Comments
Step1 :
you can go through the all Form controls and only consider the TextBox controls.
Step 2: from all Form TextBox Controls filter the TextBox's that contain Name as "txt_Address%" here % canbe anything like 1,2,3,4...etc.,
Code is as below:
List<String> txtValues=new List<string>();
foreach (var control in this.Controls)
{
if((control is TextBox) && (((TextBox) control).Name.Contains("txt_Address")))
txtValues.Add(((TextBox) control).Text.ToString());
}