6

ComboBox別のクラスにある配列の PART を設定しようとしています。顧客、在庫、注文を作成するアプリケーションを作成する必要があります。注文フォームで、それぞれ顧客クラスと在庫クラスにある配列から顧客 ID と在庫 ID 情報を取得しようとしています。配列には、顧客 ID、名前、住所、州、郵便番号など、複数の種類の情報が含まれています。在庫 ID、名前、割引額、価格。

これは私の配列が次のように設定されているものです:

public static Customer[] myCustArray = new Customer[100];

public string customerID;
public string customerName;
public string customerAddress;
public string customerState;
public int customerZip;
public int customerAge;
public int totalOrdered;

そして、これは私のコンボボックスが次のように設定されているものです:

public void custIDComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    custIDComboBox.Items.AddRange(Customer.myCustArray);

    custIDComboBox.DataSource = Customer.getAllCustomers();
}
4

2 に答える 2

5

データ バインディングを使用します。

そのように定義されたオブジェクトの既存の配列(あなたの場合は「顧客」)を与える:

public static Customer[] myCustArray = new Customer[100];

次のように配列をデータ ソースとして定義します。

BindingSource theBindingSource = new BindingSource();
theBindingSource.DataSource = myCustArray;
myComboBox.DataSource = bindingSource.DataSource;

次に、次のように各アイテムのラベルと値を設定できます。

//That should be a string represeting the name of the customer object property.
myComboBox.DisplayMember = "customerName";
myComboBox.ValueMember = "customerID";

以上です。

于 2012-12-18T05:23:33.673 に答える
1
Customer.myCustArray[0] = new Customer { customerID = "1", customerName = "Jane" };  
Customer.myCustArray[1] = new Customer { customerID = "2", customerName = "Jack" };

上記の 2 行は必要ありません。出力を表示するために追加しました。次のコードは ComboBox 項目を生成します。

foreach (Customer cus in Customer.myCustArray)
{
    comboBox1.Items.Add("[" + cus.customerID + "] " + cus.customerName);
}

このコードを適切なイベントにコピーできます。たとえばFormLoad、フォームがアクティブになるたびに ComboBox のアイテムを更新する場合は、次のようにします。

private void Form3_Activated(object sender, EventArgs e)
{
    comboBox1.Items.Clear();
    foreach (Customer cus in Customer.myCustArray)
    {
        comboBox1.Items.Add("[" + cus.customerID + "] " + cus.customerName);
    }
}
于 2012-12-18T05:35:20.237 に答える