Shilpa Soni
Shilpa Soni

Reputation: 2142

Getting text box values through loop

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?

Upvotes: 0

Views: 4204

Answers (8)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26199

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

Upvotes: 0

mehmet mecek
mehmet mecek

Reputation: 2685

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

Upvotes: 0

lauCosma
lauCosma

Reputation: 154

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
            }
        }

Upvotes: 0

Idle_Mind
Idle_Mind

Reputation: 39122

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" ...
            }
        }

Upvotes: 0

user1646737
user1646737

Reputation: 169

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

Upvotes: 0

David Pilkington
David Pilkington

Reputation: 13620

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

Upvotes: 1

Noctis
Noctis

Reputation: 11763

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

Upvotes: 0

lostincomputer2
lostincomputer2

Reputation: 353

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

Upvotes: 0

Related Questions