473,721 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

owner drawn listbox

Hi,

I can't seem to figure out how to dynamically add new items with different
font colors to a listbox. I have followed the ColorListBox example at
http://www.codeproject.com/cs/combobox/colorlistbox.asp which i found very
informative. But in the example all the items that should be added to the
listbox is known when the form is loaded.

What I want to do is add lines (items) to the listbox dynamically trough the
application - for instance when a button is clicked I want to add the text
"CLICKED" with red font color to the LISTBOX. I have no idea how to
accomplish this, so if there are anyone who can help me I would really
apreciate it.

Thanks!
Nov 16 '05 #1
2 3236
Hi Petrus,

This piece of code will do what you want.
You don't necessarily need a struct or class containing a color, but it
provides some added possibilities. If you also add a 'public override
ToString(){retu rn Name;} to the struct it will show the items correctly
when there is no OwnerDrawn mode (although there will be no color).

Button b1;
Button b2;
ListBox lb;

private struct ListBoxItem
{
public string Name;
public Color ItemColor;
public ListBoxItem(str ing s, Color c)
{
Name = s;
ItemColor = c;
}
}

public TestForm()
{
b1 = new Button();
b1.Click += new EventHandler(b1 _Click);
this.Controls.A dd(b1);

b2 = new Button();
b2.Click += new EventHandler(b2 _Click);
b2.Location = new Point(b1.Left + b1.Width + 5, b1.Top);
this.Controls.A dd(b2);

lb = new ListBox();
lb.DrawMode = DrawMode.OwnerD rawFixed;
lb.DrawItem += new DrawItemEventHa ndler(lb_DrawIt em);
lb.Location = new Point(b1.Left, b1.Top + b1.Height + 5);
this.Controls.A dd(lb);
}
private void b1_Click (object sender, EventArgs e)
{
lb.Items.Add(ne w ListBoxItem("Cl icked", Color.YellowGre en));
}

private void b2_Click (object sender, EventArgs e)
{
lb.Items.Add(ne w ListBoxItem("Cl icked", Color.Salmon));
}

private void lb_DrawItem(obj ect sender, DrawItemEventAr gs e)
{
ListBoxItem lbi = (ListBoxItem)lb .Items[e.Index];
SolidBrush b = new SolidBrush(lbi. ItemColor);
// you could also do something like
// if(lb.Items[e.Index].ToString() == "Clicked")
// b = Brushes.Red;
// else
// b = Brushes.Black;

// you can also specify background color, although then you will also
// be responsible for highlighting the background when needed
// e.Graphics.Fill Rectangle(b, e.Bounds);

e.DrawBackgroun d();
e.Graphics.Draw String(lbi.Name , lb.Font, b, e.Bounds);
b.Dispose();
}

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Thanks!

-Petrus

"Morten Wennevik" wrote:
Hi Petrus,

This piece of code will do what you want.
You don't necessarily need a struct or class containing a color, but it
provides some added possibilities. If you also add a 'public override
ToString(){retu rn Name;} to the struct it will show the items correctly
when there is no OwnerDrawn mode (although there will be no color).

Button b1;
Button b2;
ListBox lb;

private struct ListBoxItem
{
public string Name;
public Color ItemColor;
public ListBoxItem(str ing s, Color c)
{
Name = s;
ItemColor = c;
}
}

public TestForm()
{
b1 = new Button();
b1.Click += new EventHandler(b1 _Click);
this.Controls.A dd(b1);

b2 = new Button();
b2.Click += new EventHandler(b2 _Click);
b2.Location = new Point(b1.Left + b1.Width + 5, b1.Top);
this.Controls.A dd(b2);

lb = new ListBox();
lb.DrawMode = DrawMode.OwnerD rawFixed;
lb.DrawItem += new DrawItemEventHa ndler(lb_DrawIt em);
lb.Location = new Point(b1.Left, b1.Top + b1.Height + 5);
this.Controls.A dd(lb);
}
private void b1_Click (object sender, EventArgs e)
{
lb.Items.Add(ne w ListBoxItem("Cl icked", Color.YellowGre en));
}

private void b2_Click (object sender, EventArgs e)
{
lb.Items.Add(ne w ListBoxItem("Cl icked", Color.Salmon));
}

private void lb_DrawItem(obj ect sender, DrawItemEventAr gs e)
{
ListBoxItem lbi = (ListBoxItem)lb .Items[e.Index];
SolidBrush b = new SolidBrush(lbi. ItemColor);
// you could also do something like
// if(lb.Items[e.Index].ToString() == "Clicked")
// b = Brushes.Red;
// else
// b = Brushes.Black;

// you can also specify background color, although then you will also
// be responsible for highlighting the background when needed
// e.Graphics.Fill Rectangle(b, e.Bounds);

e.DrawBackgroun d();
e.Graphics.Draw String(lbi.Name , lb.Font, b, e.Bounds);
b.Dispose();
}

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

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1762
by: Bernie Yaeger | last post by:
I now know how to gather the file type icons and I'm able to use them in a listview. But a listbox does not have a .smallimagelist or .largeimagelist member, so I don't know how to translate that so that I have the icon + the string in a listbox. Is this possible, and if so, can you give me some idea how to go about it. Thanks for any help. Bernie Yaeger
2
2519
by: Eric | last post by:
I implemented owner drawing on the main and context menus of my main form. It works fine. There is also a notify icon control that has a context menu. When the icon is in the status bar, it works perfectly fine when it is double-clicked. However, when I try to initialize the notify icon's context menu as an owner-drawn menu, all the text in that menu disappears. Is owner drawing a notify icon's context menu possible? I saw someone...
1
2889
by: Patty O'Dors | last post by:
Hi I have some code to create an ownerdrawn listbox (derived), and when I add an item to it, the bold text of the first item (the title, 'Collections and Maturities') mysteriously seems to get bunched up at the right, i.e. squashed up! any idea why? The main bit of the code is as such // (in progressReporter.cs) protected struct LBRow //a row of the listbox, whether it be the title or a
0
1813
by: John R. | last post by:
I want to add a textbox next to each item in a Listbox on a Windows Form. The items need to be scrollable so I thought that maybe this is possible with an Owner Draw listbox. Searching the web and newsgroups I don't find an example anywhere how I could add a textbox or other control to an Owner Draw listbox. Can someone show me how I'd add a textbox to each listbox item using an Owner Draw listbox?
2
3135
by: dan heskett | last post by:
I am owner-drawing a listbox, in an attempt to create a nice list with some custom "fields" and text layout. Essentially it works, but I must be missing something big, conceptually, because I get all kinds of screen artifacts and weirdness. My general goal is: list item with a few areas for text, every other item shaded a light color for readability, font color changes with selection. The listbox is populated with custom structurs...
0
1635
by: Brian Henry | last post by:
Ok whats going on here... got a couple problems... this is relevant now to this group since .NET 2.0 is finally RTM 1) draws really slow when making it a large form (resizing speed is jerky) to a res from 100x100 to 1280x1024 once it gets above 500x500 it starts jerking you can see the screen refreshing as it paints 2) sometimes the background selection when you select multiple items then move the mouse around on the screen dissapear...
0
955
by: Steve Marshall | last post by:
Hi All, I'm working on an owner-drawn combo box which will display multiple columns in the list, either from a table in an associated DataSourse or from delimited strings in the Items collection. It's all going quite well, and was actually easier to achieve than I had expected. But I am seeing one peculiar anomaly when I make a selection from the list. Eveything works perfectly if the list is populated from a DataSource. But if the...
0
2684
by: LostInMd | last post by:
Hi All, I've got an owner drawn listBox where I draw and measure the items that I add to the listBox. For example, I have a listBox that can only display 10 characters on each horizontal line. The majority of my items contain much more than 10 characters and thus the reason for my use of owner drawn listBoxes - I do not want to use a horizontal scrollbar, instead I want the text of each item to wrap onto multiple lines. I do use a...
0
8730
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,...
1
9131
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9064
tracyyun
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...
0
8007
agi2029
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...
1
6669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5981
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();...
0
4484
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...
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.