473,396 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3219
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(){return 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(string s, Color c)
{
Name = s;
ItemColor = c;
}
}

public TestForm()
{
b1 = new Button();
b1.Click += new EventHandler(b1_Click);
this.Controls.Add(b1);

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

lb = new ListBox();
lb.DrawMode = DrawMode.OwnerDrawFixed;
lb.DrawItem += new DrawItemEventHandler(lb_DrawItem);
lb.Location = new Point(b1.Left, b1.Top + b1.Height + 5);
this.Controls.Add(lb);
}
private void b1_Click (object sender, EventArgs e)
{
lb.Items.Add(new ListBoxItem("Clicked", Color.YellowGreen));
}

private void b2_Click (object sender, EventArgs e)
{
lb.Items.Add(new ListBoxItem("Clicked", Color.Salmon));
}

private void lb_DrawItem(object sender, DrawItemEventArgs 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.FillRectangle(b, e.Bounds);

e.DrawBackground();
e.Graphics.DrawString(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(){return 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(string s, Color c)
{
Name = s;
ItemColor = c;
}
}

public TestForm()
{
b1 = new Button();
b1.Click += new EventHandler(b1_Click);
this.Controls.Add(b1);

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

lb = new ListBox();
lb.DrawMode = DrawMode.OwnerDrawFixed;
lb.DrawItem += new DrawItemEventHandler(lb_DrawItem);
lb.Location = new Point(b1.Left, b1.Top + b1.Height + 5);
this.Controls.Add(lb);
}
private void b1_Click (object sender, EventArgs e)
{
lb.Items.Add(new ListBoxItem("Clicked", Color.YellowGreen));
}

private void b2_Click (object sender, EventArgs e)
{
lb.Items.Add(new ListBoxItem("Clicked", Color.Salmon));
}

private void lb_DrawItem(object sender, DrawItemEventArgs 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.FillRectangle(b, e.Bounds);

e.DrawBackground();
e.Graphics.DrawString(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
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...
2
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...
1
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...
0
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...
2
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...
0
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...
0
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...
0
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...

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.