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 15169
Hi pothik05,
You need to handle the drawing for all items in the ListBox using DrawMode.OwnerD rawnFixed/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_DrawIt em(object sender, System.Windows. Forms.DrawItemE ventArgs e)
{
e.DrawBackgroun d();
Brush textBrush = SystemBrushes.C ontrolText;
Font drawFont = e.Font;
if(listBox1.Ite ms[e.Index].ToString() == "Two")
{
textBrush = Brushes.Red;
if((e.State & DrawItemState.S elected) > 0)
drawFont = new Font(drawFont.F ontFamily, drawFont.Size, FontStyle.Bold) ;
}
else if((e.State & DrawItemState.S elected) > 0)
{
textBrush = SystemBrushes.H ighlightText;
}
e.Graphics.Draw String(listBox1 .Items[e.Index].ToString(), drawFont, textBrush, e.Bounds);
}
Instead of calling e.DrawBackGroun d you could customize your own background drawing code as well. Just remember to test for DrawItemState.S elected and use SystemBrushes.H ighlight 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.OwnerD rawnFixed/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_DrawIt em(object sender, System.Windows. Forms.DrawItemE ventArgs e) { e.DrawBackgroun d(); Brush textBrush = SystemBrushes.C ontrolText; Font drawFont = e.Font;
if(listBox1.Ite ms[e.Index].ToString() == "Two") { textBrush = Brushes.Red; if((e.State & DrawItemState.S elected) > 0) drawFont = new Font(drawFont.F ontFamily, drawFont.Size, FontStyle.Bold) ; } else if((e.State & DrawItemState.S elected) > 0) { textBrush = SystemBrushes.H ighlightText; }
e.Graphics.Draw String(listBox1 .Items[e.Index].ToString(), drawFont, textBrush, e.Bounds); }
Instead of calling e.DrawBackGroun d you could customize your own background drawing code as well. Just remember to test for DrawItemState.S elected and use SystemBrushes.H ighlight 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.OwnerD rawnFixed/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_DrawIt em(object sender, System.Windows. Forms.DrawItemE ventArgs e) { e.DrawBackgroun d(); Brush textBrush = SystemBrushes.C ontrolText; Font drawFont = e.Font;
if(listBox1.Ite ms[e.Index].ToString() == "Two") { textBrush = Brushes.Red; if((e.State & DrawItemState.S elected) > 0) drawFont = new Font(drawFont.F ontFamily, drawFont.Size, FontStyle.Bold) ; } else if((e.State & DrawItemState.S elected) > 0) { textBrush = SystemBrushes.H ighlightText; }
e.Graphics.Draw String(listBox1 .Items[e.Index].ToString(), drawFont, textBrush, e.Bounds); }
Instead of calling e.DrawBackGroun d you could customize your own background drawing code as well. Just remember to test for DrawItemState.S elected and use SystemBrushes.H ighlight 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.OwnerD rawnFixed/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_DrawIt em(object sender, System.Windows. Forms.DrawItemE ventArgs e) { e.DrawBackgroun d(); Brush textBrush = SystemBrushes.C ontrolText; Font drawFont = e.Font;
if(listBox1.Ite ms[e.Index].ToString() == "Two") { textBrush = Brushes.Red; if((e.State & DrawItemState.S elected) > 0) drawFont = new Font(drawFont.F ontFamily, drawFont.Size, FontStyle.Bold) ; } else if((e.State & DrawItemState.S elected) > 0) { textBrush = SystemBrushes.H ighlightText; }
e.Graphics.Draw String(listBox1 .Items[e.Index].ToString(), drawFont, textBrush, e.Bounds); }
Instead of calling e.DrawBackGroun d you could customize your own background drawing code as well. Just remember to test for DrawItemState.S elected and use SystemBrushes.H ighlight as the selected background color.
-- Happy coding! Morten Wennevik [C# MVP]
Well, in that case you can just test for
if(e.Index.ToSt ring() == textBox1.Text)
On Fri, 06 May 2005 17:20:13 +0200, pothik05 <po******@discu ssions.microsof t.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.ToSt ring() == textBox1.Text)
On Fri, 06 May 2005 17:20:13 +0200, pothik05 <po******@discu ssions.microsof t.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 MyCheckedListBo x : CheckedListBox
{
protected override void OnDrawItem(Draw ItemEventArgs 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 MyCheckedListBo x : CheckedListBox { protected override void OnDrawItem(Draw ItemEventArgs 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 text file in a list box so
the user can see the total car fleet, my problem is that I want everything
to line up. So far what I'm getting is something like:
R296RKV Ford Mondeo Large On Hire
TUVWXYZ ...
|
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 it return the
associated records to a listbox. Once the listbox has the records, I
want to select a record, which will open a form associated with the
selected record in the listbox.
|
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 listbox until they have saved
the changes. Is there any way to deny a move from the current item so the
SelectedIndexChanged event never fires no matter move the movement was
attempted? Whether the user uses the cursor keys or mouse to try and leave...
|
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 ListBox.ClearSelection() and
ListBox.SelectedIndex = -1 but they don't work. ClearSelection actually
clears all selections but it doesn't seem to affect visual effects, same
goes for SelectedIndex = -1.
I have also tried Invalidate(true) and...
|
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 displayed in
the Edit control.Exe have a Hunderds of items in the List
controls,which display some text in edit control against an item
selection.
I want to read that data which is displayed in edit control against the
selection of an item in list...
|
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: Patients, and Sessions. There is one record in the Patients table
for each patient. The PatientID is the primary key. The sessions table
contains all of the visits that all of the patients have made to the lab.
There is a one-to-many relation between...
|
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 listbox (or any other control) where I can keep adding the string which I enter in my text box everytime? Since I am new to .NET can someone help me with this? Thanks a lot for the help.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |