2017-03-16 67 views
0

我想分配多個字段值組成的成員在組合框中。從代碼中可以看出,分配給value成員的當前字符串是「title」,並且在cboCustomers_SelectionChangeCommited事件中,您可以看到文本框已分配了選定的值。從一個組合框C多個ValueMembers#

我希望實現的是將2個更多的字段賦值給值成員(「firstname」,「lastname」),併爲這兩個值指定兩個更多的文本框。

我希望我已經清楚。如果不是,請說明,我會嘗試重新解釋。

private void Form3_Load(object sender, EventArgs e) 
        { 
         try 
         { 
          dbConn = new OleDbConnection(conString); 
          sql = @"SELECT customer.title, firstname, lastname, product.name, account.balance 
            FROM (account INNER JOIN customer ON account.custid = customer.custid) INNER JOIN product ON account.prodid = product.prodid;"; 

          daItems = new OleDbDataAdapter(sql, dbConn); 
          daItems.Fill(dtAccBal); 


          cboCustomers.DataSource = (dtAccBal); 
          cboCustomers.DisplayMember = "firstname"; 
          cboCustomers.ValueMember = "title"; 
          cboCustomers.SelectedIndex = -1; 

         } 
         catch (Exception ex) 
         { 
          MessageBox.Show(ex.Message, "Error!"); 
         } 
        } 


        private void cboCustomers_SelectionChangeCommitted(object sender, EventArgs e) 
        { 
         if (cboCustomers.SelectedIndex > -1) 
         { 
          try 
          { 
           txtTitle.Text = cboCustomers.SelectedValue.ToString(); 



          } 
          catch (Exception ex) 
          { 
           MessageBox.Show(ex.Message, "Error!"); 
          } 
         } 
        } 
       } 
+0

這是錯的,你不應該使用像這樣的多個值的下拉項。 –

+0

你能提出一種替代方法嗎? –

回答

0

您可以使用實現INotifyPropertyChanged接口對象,並添加數據綁定到你的文本框。

示例:實現INotifyPropertyChanged

public class Person : INotifyPropertyChanged 
{ 
    private string _firstName; 
    public string FirstName 
    { 
     get { return _firstName; } 
     set 
     { 
      if (value == _firstName) return; 
      _firstName = value; 
      OnPropertyChanged(); 
     } 
    } 

    private string _lastName; 
    public string LastName 
    { 
     get { return _lastName; } 
     set 
     { 
      if (value == _lastName) return; 
      _lastName = value; 
      OnPropertyChanged(); 
     } 
    } 

    public override string ToString() => $"{FirstName} {LastName}"; 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

添加上形式ComboBox 「comboBoxPeople」 和兩個TextBoxes 「textBoxFirstName」, 「textBoxLastName」

類Person。形式的

更改構造:

public Form1() 
{ 
    InitializeComponent(); 

    var people = new BindingList<Person> { 
     new Person() { FirstName = "Peter", LastName = "Pan" }, 
     new Person() { FirstName = "Tinker", LastName = "Bell" }, 
     new Person() { FirstName = "James", LastName = "Hook" }, 
     new Person() { FirstName = "Wendy", LastName = "Darling" }, 
    }; 

    var bindingSource = new BindingSource() { DataSource = people }; 

    comboBoxPeople.DataSource = bindingSource; 
    textBoxFirstName.DataBindings.Add(nameof(TextBox.Text), bindingSource, nameof(Person.FirstName), false, DataSourceUpdateMode.OnPropertyChanged); 
    textBoxLastName.DataBindings.Add(nameof(TextBox.Text), bindingSource, nameof(Person.LastName), false, DataSourceUpdateMode.OnPropertyChanged); 
} 

現在你擁有一個充滿人的組合框。選擇其他人後,文本框將自動填入適當的數據。

相關問題