0

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?

5
  • 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. Commented 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_address10 Commented 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. Commented Nov 11, 2013 at 6:54
  • @user1646737 But this is how my client wants me to do. Commented 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. Commented Nov 11, 2013 at 6:57

8 Answers 8

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

But in this case also I need to declare 10 text boxes for each of my text box. Can it be done by FOR loop in place of foreach loop?
sure you can create textbox dynamically and place it in the form like this :
List<TextBox> lst = new List<TextBox>(); for (int i=0; i<10; i++) { TextBox txt = new TextBox(); lst.Add(txt); form1.Controls.Add(txt); txt.Top = (i + 1) * 20; DoSomething(txt.Text); }
0

You could put them in a list, and iterate over that list ...

Edit: Seems lostincomputer answered twenty seconds ahead of me ... same same ... both will work

Comments

0

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

0

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

Do I need to add some namespace for Controls.Find()? If yes, then Kindly tell me. Because I'm not able to find Find method in intellicense.
You didn't specify...WinForms?, WebForms?, WPF?, something else?
I'm talking about WebForms.
I'm not a web developer, sorry. See andleer's answer over here.
0

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

You will also need to cast the 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.
0
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

0

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());
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.