如何操作动态创建的控件?

时间:2012-02-22 21:37:53

标签: c# dynamic controls

假设我有一个表格,有人可以列出多人姓名及其州和市?

假设我已经拥有数据库中的所有州/城市。 (每个州的表格,每个表中列出的城市)

“名称”字段将是TextBox。和国家&城市字段将是ComboBox(DropDownLists)。

表格中已存在一行(用于输入一个人)。但我希望用户能够通过按“添加人员”按钮动态添加条目行。

下一步是我在努力的地方。在每个动态添加的字段行中,我希望根据在第一个组合框中选择的状态来填充第二个ComboBox(Cities)。此外,在选择State ComboBox之前,Cities ComboBox将保持禁用状态。

我的代码看起来像这样:

    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 City ComboBox that is in the same row as the State ComboBox that was just changed, and manipulate/populate it.....

    }

任何人都能帮我解决这个问题吗?

我非常感谢!!

4 个答案:

答案 0 :(得分:0)

你可以使用字典

Dictionary<ComboBox,ComboBox> CityToStateMap = new Dictionary<ComboBox,ComboBox>();

然后当你添加一行

CityToStateMap[cbState] = cbCity;

然后当你改变状态

ComboBox city = CityToStateMap[(ComboBox)sender];

答案 1 :(得分:0)

首先,我将创建一个包含一行所有控件的附加类(例如,名称为Row)。因此,您可以将一个人的控件封装在一个对象中 您的Form类获取此类行对象的列表成员,如
List<Row> _rows = new List<Row>();

在该类 Row 的构造函数中,您可以创建一行的控件,并将事件处理程序分配给组合框控件的SelectedValueChanged事件。

答案 2 :(得分:0)

您可以使用Control.Tag将数据对象附加到控件。请参阅示例中控件如何相互引入。

private void btnAddNewPerson_Click( object sender, EventArgs e )
{
    AddPersonRow();
}

private void AddPersonRow()
{
    // Create combo boxes
    ComboBox cbCity = new ComboBox();
    ComboBox cbState = new ComboBox();

    // Introduce them to each other
    cbCity.Tag = cbState;
    cbState.Tag = cbCity;

    //  ADD State ComboBox
    cbState.Name = "State" + NumberOfPeople;
    cbState.Enabled = true;
    cbState.DropDownStyle = ComboBoxStyle.DropDownList;
    cbState.SelectedValueChanged += new EventHandler( cbState_SelectedValueChanged );
    panel.Controls.Add( cbState );

    // Populate the states sombo
    PopulateStateCombo( cbState );

    //  ADD City ComboBox
    cbCity.Name = "City" + NumberOfPeople;
    cbCity.DropDownStyle = ComboBoxStyle.DropDownList;
    cbCity.Enabled = false;
    cbCity.SelectedValueChanged += new EventHandler(cbCity_SelectedValueChanged);
    panel.Controls.Add( cbCity );
}

void cbState_SelectedValueChanged( object sender, EventArgs e )
{
    ComboBox cbState = sender as ComboBox;
    ComboBox cbCity = cbState.Tag as ComboBox;

    cbCity.Enabled = true;

    // .. Go ahead and populate cbCity by cbState's selected value ..
}

void cbCity_SelectedValueChanged( object sender, EventArgs e )
{
    // Up to you...
}

请注意,您可以创建一个包含ComboBox,TextBox控件,行号等的类,而不是与其他ComboBox握手,然后将所有这些控件的Tag设置为类本身。 / p>

public class PersonRow
{
    public int RowNum { get; private set; }
    public TextBox NameTextBox { get; private set; }
    public ComboBox CityCombo { get; private set; }
    public ComboBox StateCombo { get; private set; }

    public PersonRow( int rowNum )
    {
        RowNum = rowNum;

        // Create the controls
        NameTextBox = new TextBox();
        CityCombo = new ComboBox();
        StateCombo = new ComboBox();

        // Bind them to this instance
        NameTextBox.Tag = this;
        CityCombo.Tag = this;
        StateCombo.Tag = this;

        //.. continue as in the prev. example..
    }
}

答案 3 :(得分:0)

首先,您应该找到要操作的组合框的名称。

然后您可以在面板中搜索具有我们刚刚找到的名称的控件,然后您可以按照自己的意愿进行操作。

这是

的代码
    private void ChangeState(object sender,EventArgs e){
        ComboBox stateComboBox= (ComboBox)sender;
        //find the name of target City ComboBox
        string cityComboName = "City"+stateComboBox.Name.Substring(5); // 5 is the Length of 'State'
        ComboBox cityComboBox=null;
        foreach(Control cntrl in panel1.Controls){
            if (cntrl.Name == cityComboName) {
                cityComboBox= (ComboBox)cntrl;
                break;
            }
        }
        if (cityComboBox!= null) {
            cityComboBox.Enabled = true;
            // now you have the both cityComboBox and stateComboBox Of the same Row
        }
    }