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 8 15063
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]
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]
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]
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]
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]
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]
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]
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] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Roy Riddex |
last post by:
I have a text file which holds data for 30 cars in the following way:
CarRegistration CarType CarClass Available
I'm trying to display the full contents of this...
|
by: Megan |
last post by:
Hi-
I'm creating a database of music bands with their cds and songs.
I'm trying to program an SQL statement so that I can enter a string of
text in a textbox, press the 'Enter' key, and have...
|
by: Byron |
last post by:
I'm looking for a way to deny a move from the current listbox item. For
instance, if the user is editing the record associated with the current
listbox item I want to deny a move within the...
|
by: Maqsood Ahmed |
last post by:
Hello!
I am using a ListBox control in a WinForm. ListBox control is populated
through a DataSource. The problem starts when ListBox selects "index 0"
automatically at form load. I have tried...
|
by: pothik05 |
last post by:
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
|
by: Mike Johnson |
last post by:
I'm using VS Basic.net 2003
How can I change the foreground color of the item being added to a listbox?
|
by: CrimeMaster |
last post by:
HI
I have an Exe file when it runs ,there are just two controls on the
window.One is a ListBox and another is a Multi line Rich Edit
control.When i click on an item in the list some text is...
|
by: =?Utf-8?B?SmltIFdhbHNo?= |
last post by:
I am a .NET newbie, specifically WinForms. I have a simple Winform for which
I have a "How To" question:
My WinForm app will display data from a FoxPro database, two tables in
particular:...
|
by: sbandalli |
last post by:
Hi All,
I have one more question in my project, Last time the reply I got really helped me a lot . This is the project in WinForm(C#). I want to copy the text in the text box to a dropdownlist or...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |