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 15000
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: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |