R
RSH
I am slowly getting the hang of objects and creating my own. I was given
some help in assigning an object to a ComboBox collection. I have been
looking at it and I get the concept which is very powerful but I'm having a
bit of a problem conceptually. I was hoping someone might be able to shed
some light on creating this object and assigning it in the manner. Also how
would I reference this object by name after it is created in this manner?
Is there a collection of objects that I could iterate through with respect
to this ComboBox?
Thanks for the help,
Ron
// Fill ComboBox with Employees
public void FillEmployees(DataSet DS, ComboBox ComboList)
{
if (bAuthenticated == true)
{
ComboList.Items.Clear();
foreach (DataRow DR in DS.Tables[0].Rows)
{
String sTemp = DR["ID"].ToString().PadLeft(8, '0');
ComboList.Items.Add(new Employee(sTemp, DR));
}
}
}
// Employee Class
class Employee
{
private String iD;
private String firstName;
private String lastName;
private int iselected;
public Employee(String ID, String LastName, String FirstName)
{
iD = ID.PadLeft(8, '0');
lastName = LastName;
firstName = FirstName;
iselected = 0;
}
public Employee(String ID, DataRow DR)
{
iD = DR["ID"].ToString().PadLeft(8, '0');
lastName = DR["LastName"].ToString();
firstName = DR["FirstName"].ToString();
iselected = 0;
}
public string ID
{
get { return iD; }
set { iD = value; }
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public int iSelected
{
get { return iselected++; }
set { iselected = value; }
}
public override string ToString()
{
return lastName + " " + firstName;
}
}
// ComboBox Select Index Changed Event
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
MessageBox.Show(((Employee)comboBox1.SelectedItem).ID.ToString());
richTextBox1.Text += ((Employee)comboBox1.SelectedItem).FirstName.ToString()
+ " " + ((Employee)comboBox1.SelectedItem).LastName.ToString() + "\n";
comboBox1.Items.Remove((Employee)comboBox1.SelectedItem);
}
some help in assigning an object to a ComboBox collection. I have been
looking at it and I get the concept which is very powerful but I'm having a
bit of a problem conceptually. I was hoping someone might be able to shed
some light on creating this object and assigning it in the manner. Also how
would I reference this object by name after it is created in this manner?
Is there a collection of objects that I could iterate through with respect
to this ComboBox?
Thanks for the help,
Ron
// Fill ComboBox with Employees
public void FillEmployees(DataSet DS, ComboBox ComboList)
{
if (bAuthenticated == true)
{
ComboList.Items.Clear();
foreach (DataRow DR in DS.Tables[0].Rows)
{
String sTemp = DR["ID"].ToString().PadLeft(8, '0');
ComboList.Items.Add(new Employee(sTemp, DR));
}
}
}
// Employee Class
class Employee
{
private String iD;
private String firstName;
private String lastName;
private int iselected;
public Employee(String ID, String LastName, String FirstName)
{
iD = ID.PadLeft(8, '0');
lastName = LastName;
firstName = FirstName;
iselected = 0;
}
public Employee(String ID, DataRow DR)
{
iD = DR["ID"].ToString().PadLeft(8, '0');
lastName = DR["LastName"].ToString();
firstName = DR["FirstName"].ToString();
iselected = 0;
}
public string ID
{
get { return iD; }
set { iD = value; }
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public int iSelected
{
get { return iselected++; }
set { iselected = value; }
}
public override string ToString()
{
return lastName + " " + firstName;
}
}
// ComboBox Select Index Changed Event
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
MessageBox.Show(((Employee)comboBox1.SelectedItem).ID.ToString());
richTextBox1.Text += ((Employee)comboBox1.SelectedItem).FirstName.ToString()
+ " " + ((Employee)comboBox1.SelectedItem).LastName.ToString() + "\n";
comboBox1.Items.Remove((Employee)comboBox1.SelectedItem);
}