This could serve you for some more scenarios.
Encapsulate you customer data in one class (eg. MyCustomerData) with all the properties and fields needed for holding the information which is relevant to your application (MyCustomerData.cs).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ComboBoxUpdate
{
public class MyCustomerData
{
private string m_Name;
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
public MyCustomerData(string Name)
{
this.Name = Name;
}
}
}
Create one (let's say) MyCustomerDataEditor form with one constructor that takes one instance from your MyCustomerData class. Of course you need to add all the user input validation logic that ensures data correctness, enables|disables the 'Accept' button after changes validation (etc.).
MyCustomerDataEditorFrm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ComboBoxUpdate
{
public partial class MyCustomerDataEditorFrm : Form
{
private MyCustomerData m_Customer;
public MyCustomerData Customer
{
get { return m_Customer; }
set
{
m_Customer = value;
ValidateEditors();
}
}
public MyCustomerDataEditorFrm( MyCustomerData Customer )
{
m_Customer = Customer;
InitializeComponent();
this.Customer = Customer;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
ValidateEditors();
}
private void ValidateEditors()
{
button1.Enabled = !string.IsNullOrEmpty(textBox1.Text);
}
private void button1_Click(object sender, EventArgs e)
{
m_Customer.Name = textBox1.Text;
}
}
}
Declare some kind of list for holding the MyCustomerData instances list in your main form:
List<mycustomerdata> m_MyCustomers = new List<mycustomerdata>()</mycustomerdata></mycustomerdata>
In your main form populate the list with valid data from your database. Let's suppose you have one method called GetMeTheDataFromTheDataBase()hat retrieves that data.
m_MyCustomers.AddRange( GetMeTheDataFromTheDataBase())
You may configure the ComboBox so the text it displays is the CustomerName property (provided that you declared it in the MyCustomerData class).
The main form could end up like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ComboBoxUpdate
{
public partial class Form1 : Form
{
private List<mycustomerdata> m_Customers;
public Form1()
{
m_Customers = new List<mycustomerdata>();
InitializeComponent();
comboBox1.DisplayMember = "Name";
LoadCustomers();
UpdateCustomersComboBox();
}
private void UpdateCustomersComboBox()
{
foreach (MyCustomerData Customer in m_Customers)
{
if (comboBox1.Items.IndexOf(Customer) < 0)
comboBox1.Items.Add(Customer);
}
UpdateUIControls();
}
private void LoadCustomers()
{
m_Customers.AddRange( new MyCustomerData[] {
new MyCustomerData( "John Doe" ),
new MyCustomerData( "Jane Doe" )
});
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateUIControls();
}
private void UpdateUIControls()
{
button2.Enabled = (comboBox1.SelectedIndex >= 0);
}
private void button1_Click(object sender, EventArgs e)
{
MyCustomerDataEditorFrm Dlg = new MyCustomerDataEditorFrm(new MyCustomerData( "Joe's Brother!" ));
if (Dlg.ShowDialog(this) == DialogResult.OK)
{
m_Customers.Add(Dlg.Customer);
UpdateCustomersComboBox();
}
}
private void button2_Click(object sender, EventArgs e)
{
MyCustomerData Customer = comboBox1.SelectedItem as MyCustomerData;
if (DialogResult.Yes != MessageBox.Show(this, "Sure to remove " + Customer.Name, "Remove Customer", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
return;
m_Customers.Remove(Customer);
comboBox1.Items.Remove(Customer);
UpdateUIControls();
}
}
}
</mycustomerdata></mycustomerdata>
One more thing, the components in the main form are the combobox (combobox1), one "Add New" button (button1) and one "Remove" button (button2).
I didn't see the way to attach the sample project/code, but if you need it i can e-mail it to you.
Hope it helps you some how.