繁体   English   中英

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

[英]How can I to manipulate dynamically created controls?

可以说,我有一个表格,可以在该表格中列出多个人的姓名及其州和城市?

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

名称字段将是一个文本框。 州和城市字段将为ComboBox(DropDownLists)。

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

下一步是我要努力的地方。 在每个动态添加的字段行中,我希望根据在第一个组合框中选择哪个州来填充第二个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.....

    }

谁能帮助我解决这个问题?

我将不胜感激!

首先,您应该找到要操作的comboBox的名称。

然后您可以在面板中搜索具有我们刚刚找到的名称的控件,然后您就可以执行所需的操作。

这是该代码

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

你可以用字典

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

然后当您添加一行

CityToStateMap[cbState] = cbCity;

然后当你改变状态

ComboBox city = CityToStateMap[(ComboBox)sender];

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

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

您可以使用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设置为该类本身。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM