المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How can I to manipulate dynamically created controls?



C# Programming
02-23-2012, 05:23 AM
Lets say I have a form where someone can catalog a multiple people's Name and their State and City?

Assume I already have all the State/Cities in database. (a table for each state, with cities listed in each table)

The Name field will be a TextBox. And the State & City fields will be ComboBox (DropDownLists).

One row (for the entry of one person) already exists in the form. But I want the user to be able to dynamically add rows of entries by pressing an "Add Person" button.

The next step is where I'm struggling. In each dynamically added row of fields, I would like the second ComboBox (Cities) to be populated depending on which State is chosen in the first Combo Box. Also, the Cities ComboBox will remain disabled until the State ComboBox is chosen.

My code looks something like this:

public ComboBox cbState; public ComboBox cbCities; public static int NumberOfPeople = 1; private void btnAddNewPerson_Click(object sender, EventArgs e) { NumberOfPeople++; TextBox txtPerson = new TextBox(); txtPerson.Name = "Person" + NumberOfPeople; Panel.Controls.Add(txtPerson); // ADD State ComboBox cbState = new ComboBox(); cbState.Name = "State" + NumberOfPeople; cbState.Enabled = true; cbState.DropDownStyle = ComboBoxStyle.DropDownList; Panel.Controls.Add(cbState); // ADD City ComboBox cbCity = new ComboBox(); cbCity.Name = "City" + NumberOfPeople; cbCity.DropDownStyle = ComboBoxStyle.DropDownList; cbCity.Enabled = false; cbCity.SelectedValueChanged += new System.EventHandler(this.ChangeState); Panel.Controls.Add(cbCity); } private void ChangeState(object sender, EventArgs e) { ..... Don't know how to properly identify the dynamically created City ComboBox that is in the same row as the State ComboBox that was just changed, and manipulate/populate it..... }
Anyone able to help me solve this issue??

I'd greatly appreciate it!!