forms - how to change listbox item color c# based on its index -
forms - how to change listbox item color c# based on its index -
i want alter color of listbox item based on it's index. have textbox...and when user enters index number want alter text color of corresponding index in list box
eg: when user clicks button thing happen:
private void button1_click(object sender, eventargs e) { setcolor(int.parse(textbox1.text)); } and want create such setcolor function. listview not alternative me.
you need handle drawitem event of listbox draw items specified color
note : here in below code i'm changing colour of listbox item green
try this:
int itemindex = -1; public form1() { initializecomponent(); this.listbox1.drawitem += new system.windows.forms.drawitemeventhandler(this.listbox1_drawitem); } private void listbox1_drawitem(object sender, drawitemeventargs e) { e.drawbackground(); graphics g = e.graphics; if(e.index == itemindex ) { g.fillrectangle(new solidbrush(color.green), e.bounds); } else { g.fillrectangle(new solidbrush(color.white), e.bounds); } listbox lb = (listbox)sender; g.drawstring(listbox1.items[e.index].tostring(), e.font, new solidbrush(color.black), new pointf(e.bounds.x, e.bounds.y)); e.drawfocusrectangle(); } private void button1_click(object sender, eventargs e) { setcolor(int.parse(textbox1.text)); } void setcolor(int index) { itemindex = index; listbox1.drawmode = drawmode.normal; listbox1.drawmode = drawmode.ownerdrawfixed; } c# forms
Comments
Post a Comment