469,306 Members | 1,901 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

text color change in listbox control on winform

Hello

I am trying to change the color of individual entry in the listbox control
based on some custom event. Can anyone please give any idea how to do it?

Thanks
Jul 21 '05 #1
8 14578
Hi pothik05,

You need to handle the drawing for all items in the ListBox using DrawMode.OwnerDrawnFixed/Variable and the DrawItem event.

This sample uses a ListBox where one of the items in the list is the string "Two". This item will be colored Red and if selected highlighted with bold red.

private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
Brush textBrush = SystemBrushes.ControlText;
Font drawFont = e.Font;

if(listBox1.Items[e.Index].ToString() == "Two")
{
textBrush = Brushes.Red;
if((e.State & DrawItemState.Selected) > 0)
drawFont = new Font(drawFont.FontFamily, drawFont.Size, FontStyle.Bold);
}
else if((e.State & DrawItemState.Selected) > 0)
{
textBrush = SystemBrushes.HighlightText;
}

e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), drawFont, textBrush, e.Bounds);
}

Instead of calling e.DrawBackGround you could customize your own background drawing code as well. Just remember to test for DrawItemState.Selected and use SystemBrushes.Highlight as the selected background color.

--
Happy coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #2
Thanks Morten. Actually I could do this. What I need to do is following:

After the Listbox is loaded, I have button and a text box. The text box
takes an input (the listitem index) and based on that input I need to change
the color to green of that listitem of the button's click event.

Thanks for your input.

"Morten Wennevik" wrote:
Hi pothik05,

You need to handle the drawing for all items in the ListBox using DrawMode.OwnerDrawnFixed/Variable and the DrawItem event.

This sample uses a ListBox where one of the items in the list is the string "Two". This item will be colored Red and if selected highlighted with bold red.

private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
Brush textBrush = SystemBrushes.ControlText;
Font drawFont = e.Font;

if(listBox1.Items[e.Index].ToString() == "Two")
{
textBrush = Brushes.Red;
if((e.State & DrawItemState.Selected) > 0)
drawFont = new Font(drawFont.FontFamily, drawFont.Size, FontStyle.Bold);
}
else if((e.State & DrawItemState.Selected) > 0)
{
textBrush = SystemBrushes.HighlightText;
}

e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), drawFont, textBrush, e.Bounds);
}

Instead of calling e.DrawBackGround you could customize your own background drawing code as well. Just remember to test for DrawItemState.Selected and use SystemBrushes.Highlight as the selected background color.

--
Happy coding!
Morten Wennevik [C# MVP]

Jul 21 '05 #3
Thanks Morten. Actually I could do this. What I need to do is following:

After the Listbox is loaded, I have button and a text box. The text box
takes an input (the listitem index) and based on that input I need to change
the color to green of that listitem of the button's click event.

Thanks for your input.

"Morten Wennevik" wrote:
Hi pothik05,

You need to handle the drawing for all items in the ListBox using DrawMode.OwnerDrawnFixed/Variable and the DrawItem event.

This sample uses a ListBox where one of the items in the list is the string "Two". This item will be colored Red and if selected highlighted with bold red.

private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
Brush textBrush = SystemBrushes.ControlText;
Font drawFont = e.Font;

if(listBox1.Items[e.Index].ToString() == "Two")
{
textBrush = Brushes.Red;
if((e.State & DrawItemState.Selected) > 0)
drawFont = new Font(drawFont.FontFamily, drawFont.Size, FontStyle.Bold);
}
else if((e.State & DrawItemState.Selected) > 0)
{
textBrush = SystemBrushes.HighlightText;
}

e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), drawFont, textBrush, e.Bounds);
}

Instead of calling e.DrawBackGround you could customize your own background drawing code as well. Just remember to test for DrawItemState.Selected and use SystemBrushes.Highlight as the selected background color.

--
Happy coding!
Morten Wennevik [C# MVP]

Jul 21 '05 #4
Thanks Morten. Actually I could do this. What I need to do is following:

After the Listbox is loaded, I have button and a text box. The text box
takes an input (the listitem index) and based on that input I need to change
the color to green of that listitem of the button's click event.

Thanks for your input.

"Morten Wennevik" wrote:
Hi pothik05,

You need to handle the drawing for all items in the ListBox using DrawMode.OwnerDrawnFixed/Variable and the DrawItem event.

This sample uses a ListBox where one of the items in the list is the string "Two". This item will be colored Red and if selected highlighted with bold red.

private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
Brush textBrush = SystemBrushes.ControlText;
Font drawFont = e.Font;

if(listBox1.Items[e.Index].ToString() == "Two")
{
textBrush = Brushes.Red;
if((e.State & DrawItemState.Selected) > 0)
drawFont = new Font(drawFont.FontFamily, drawFont.Size, FontStyle.Bold);
}
else if((e.State & DrawItemState.Selected) > 0)
{
textBrush = SystemBrushes.HighlightText;
}

e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), drawFont, textBrush, e.Bounds);
}

Instead of calling e.DrawBackGround you could customize your own background drawing code as well. Just remember to test for DrawItemState.Selected and use SystemBrushes.Highlight as the selected background color.

--
Happy coding!
Morten Wennevik [C# MVP]

Jul 21 '05 #5
Well, in that case you can just test for

if(e.Index.ToString() == textBox1.Text)
On Fri, 06 May 2005 17:20:13 +0200, pothik05 <po******@discussions.microsoft.com> wrote:
Thanks Morten. Actually I could do this. What I need to do is following:

After the Listbox is loaded, I have button and a text box. The text box
takes an input (the listitem index) and based on that input I need to change
the color to green of that listitem of the button's click event.

Thanks for your input.


--
Happy coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #6
Thanks. It works. How do I make the the CheckedListBox work the same way. It
seems no to work.

"Morten Wennevik" wrote:
Well, in that case you can just test for

if(e.Index.ToString() == textBox1.Text)
On Fri, 06 May 2005 17:20:13 +0200, pothik05 <po******@discussions.microsoft.com> wrote:
Thanks Morten. Actually I could do this. What I need to do is following:

After the Listbox is loaded, I have button and a text box. The text box
takes an input (the listitem index) and based on that input I need to change
the color to green of that listitem of the button's click event.

Thanks for your input.


--
Happy coding!
Morten Wennevik [C# MVP]

Jul 21 '05 #7
Unlike what I said in another thread, and like someone else said in the same thread, create your own CheckedListBox version by inheriting from CheckedListBox and override the OnDrawItem event

class MyCheckedListBox : CheckedListBox
{
protected override void OnDrawItem(DrawItemEventArgs e)
{
// this code is called without specifying DrawMode
}
}

--
Happy coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #8
Thanks

"Morten Wennevik" wrote:
Unlike what I said in another thread, and like someone else said in the same thread, create your own CheckedListBox version by inheriting from CheckedListBox and override the OnDrawItem event

class MyCheckedListBox : CheckedListBox
{
protected override void OnDrawItem(DrawItemEventArgs e)
{
// this code is called without specifying DrawMode
}
}

--
Happy coding!
Morten Wennevik [C# MVP]

Jul 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Roy Riddex | last post: by
7 posts views Thread by Byron | last post: by
reply views Thread by Maqsood Ahmed | last post: by
4 posts views Thread by Mike Johnson | last post: by
4 posts views Thread by CrimeMaster | last post: by
reply views Thread by =?Utf-8?B?SmltIFdhbHNo?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.