hi all..I am new on C# .My problem is I have a 1 combo box, list box and button . When I choose value on combobox it search on database nd shows on listbox. and I used OwnerDravFixed method..It works and when i change combobox value it show that some line with another colour.But I need to take this value from button click.But second time i cant call this lisbox_DrawItem method.. Any idea? here my codes
public void groupComboBox()
{
connection.Open();
MySqlCommand cmd = new MySqlCommand("select Group_NO from sinif", connection);
reader = cmd.ExecuteReader();
if (reader.HasRows)
while (reader.Read())
{
groupcomboBox.Items.Add(reader["Group_NO"]);
}
connection.Close();
}
private void search_B_Click(object sender, EventArgs e)
{
listbox.items.Add("car");
listbox.items.Add("plane");
}
private void groupcomboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.Items.Count != 0)
listBox1.Items.Clear();
connection.Open();
MySqlCommand cmd = new MySqlCommand("select Student_ID,Student_Name,Group_NO,Student_MacAdress from student INNER JOIN sinif ON student.Group_ID = sinif.Group_ID where Group_NO=(?GroupNO)", connection);
MySqlParameter param = new MySqlParameter();
param.ParameterName = "?GroupNO";
param.Value = groupcomboBox.SelectedItem.ToString();
cmd.Parameters.Add(param);
reader = cmd.ExecuteReader();
if (reader.HasRows)
while (reader.Read())
{
listBox1.Items.Add("" + reader["Student_Name"] + " " + reader["Group_NO"] + " " + reader["Student_MacAdress"]);
}
reader.Close();
connection.Close();
}
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
int delta = 5;
Rectangle rc = new Rectangle(e.Bounds.X + delta, e.Bounds.Y + delta, e.Bounds.Width - 10, e.Bounds.Height - delta);
// Console.WriteLine(e.State.ToString());
// Setup the stringformatting object
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
// Get the item text
listBox1 = (ListBox)sender;
string str = (string)listBox1.Items[e.Index];
// Check if the item is selected
if(listBox1.Items[e.Index].ToString().Contains("car"))
{
// Paint the item that if not selected
e.Graphics.FillRectangle(new SolidBrush(Color.BlueViolet), rc);
e.Graphics.DrawString(str, new Font("Ariel", 9), new SolidBrush(Color.Black), rc, sf);
e.DrawFocusRectangle();
}
else
{
// Paint the item accordingly if it is selected
e.DrawFocusRectangle();
e.Graphics.FillRectangle(new SolidBrush(Color.Pink), rc);
e.Graphics.DrawString(str, new Font("Ariel", 9), new SolidBrush(Color.Black), rc, sf);
}
}
Problem It show only data from Combobox bu nto from search_B_Click action