2012-02-22 9 views
1

私は誰かが複数の人の名前と州と市をカタログ化できる形式を持っていると言いますか?動的に作成されたコントロールをどのように操作できますか?

私はすでにデータベースにすべての州/都市があるとします。 (各状態のテーブルで、各テーブルの都市が表示されます)

名前フィールドはTextBoxになります。また、州の&都市はComboBox(DropDownLists)になります。

フォームに1行(1人のエントリ用)が既に存在します。しかし、私は、ユーザーが「人物を追加」ボタンを押してエントリーの行を動的に追加できるようにしたい。

次のステップは、私が苦労しているところです。各動的に追加されたフィールドの行では、最初のコンボボックスで選択されているステートに応じて、2番目のComboBox(Cities)を作成します。また、State ComboBoxが選択されるまで、都市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 city = CityToStateMap[(ComboBox)sender]; 

答えて

0

あなたは(

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

その後、辞書を使用することができます1行のすべてのコントロールを含むRowという名前)。したがって、1人のコントロールを1つのオブジェクトにカプセル化することができます。
あなたのフォームクラスは
List<Row> _rows = new List<Row>();のような行オブジェクトのリストメンバーを取得します。

このクラスのコンストラクタでを作成し、1行のコントロールを作成し、コンボボックスコントロールのSelectedValueChangedイベントにイベントハンドラを割り当てます。

0

ファーストをchangestateときに、行、その後

CityToStateMap[cbState] = cbCity; 

を追加するとき

0

コントロールにデータオブジェクトを添付するために、Control.Tagを使用できます。この例では、コントロールが互いにどのように導入されているかを示します。代わりに、それぞれ他のコンボボックスと握手で、あなたは、などあなたのTextBoxコントロール、行番号を、両方のコンボボックスを保持するクラスを作成し、可能性が

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

注に、すべてのこれらのコントロールのタグを設定しますクラスそのもの。

+0

感謝のコードです。上記の最初の方法は動作しますが、一度に1行ずつ記入する場合に限ります。私は5を事前に追加し、次にcbState ComboBoxを変更しても、最後の行のcbCity ComboBoxには影響しません。あなたが各行のコントロールを保持するクラスを使用する2番目のオプションを試してみたら、それはこの問題を修正するでしょうか? – NWNewell

+0

どのオプションを選択しても、コードは都市と州のコンボボックスの両方に対して新しいインスタンスを作成し、それらをリンクします。 答えを更新し、すべてのコードをbtnAddNewPerson_Click()からAddPersonRow()に移動しました。イベントハンドラは実際に行を作成するコードを呼び出します。 あなたはあなたの質問に、あなたは既にフォームに1つの行があると言っています。空のフォームから始め、AddPersonRow()を呼び出すことで作成したことを確認してください。この方法では、事前に追加する行の数は関係ありません。 – AVIDeveloper

0

最初に、操作したいcomboBoxの名前を見つける必要があります。

私たちが見つけた名前のパネル内のコントロールを検索することができます。ここ

は、応答、AVIDeveloperのためにその

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 
     } 
    } 
関連する問題