I am using this code to put a image in listbox but the text does not show . When I click on the list then it shows . What is the problem ?
form_load()
{
listbox1.Items.Add("string");
listbox1.DrawMode = DrawMode.OwnerDrawVariable;
}
private void listbox1_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox lst = sender as ListBox;
e.Graphics.DrawImage(imageList1.Images[0], 0, 0, 10, 10);
e.Graphics.DrawString(lst.Text, this.Font,SystemBrushes.ControlDark, 11, 0);
}
Well, it looks like you're drawing your items incorrectly.
DrawItemevent is being called for each item in the listbox, but you're drawing all the times the same text at the same position. You should usee.Boundsto determine position of each item. Also you can handleMeasureItemevent to set custom bounds for each item if you need some non-standard dimensions.Also
lst.Textdoesn't has much sense here, it should be text of current item to draw, based one.Index.Thus, part of your code drawing string could look something like:
Also you may find useful some example at MSDN.