473,472 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Changing colours of certain items in a listbox

Hi,

I need to change the colour of certain items in a listbox depending on
criteria I receive from my dataset? How would this be accomplished?

Thanks in advance...

BM
Nov 20 '05 #1
6 1527
You can't change the individual back and for colors of a listbox item. You
can change the back or the for color for the control as a whole.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Brett Miller" <br***@NOSPAMpsion.co.za> wrote in message
news:Oc**************@tk2msftngp13.phx.gbl...
Hi,

I need to change the colour of certain items in a listbox depending on
criteria I receive from my dataset? How would this be accomplished?

Thanks in advance...

BM

Nov 20 '05 #2
On Mon, 5 Jul 2004 10:36:59 +0200, "Brett Miller"
<br***@NOSPAMpsion.co.za> wrote:
I need to change the colour of certain items in a listbox depending on
criteria I receive from my dataset? How would this be accomplished?


Add a listbox to a form and try this:

<Code>

Public Sub New()
MyBase.New()
InitializeComponent()

ListBox1.DrawMode = DrawMode.OwnerDrawFixed

ListBox1.Items.Add("Black")
ListBox1.Items.Add("Red")
ListBox1.Items.Add("Green")
ListBox1.Items.Add("Blue")
ListBox1.Items.Add("Pink")

End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
DrawItemEventArgs) Handles ListBox1.DrawItem

Dim s As String = ListBox1.Items(e.Index).ToString
Dim b As New SolidBrush(Color.FromName(s))

e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawString(s, Font, b, e.Bounds.X, e.Bounds.Y)

b.Dispose

End Sub

</Code>

Hope this helps

Blu
Nov 20 '05 #3
I didnt think of that, well done !

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"BluDog" <ne**@nospam.bludog.net> wrote in message
news:j9********************************@4ax.com...
On Mon, 5 Jul 2004 10:36:59 +0200, "Brett Miller"
<br***@NOSPAMpsion.co.za> wrote:
I need to change the colour of certain items in a listbox depending on
criteria I receive from my dataset? How would this be accomplished?


Add a listbox to a form and try this:

<Code>

Public Sub New()
MyBase.New()
InitializeComponent()

ListBox1.DrawMode = DrawMode.OwnerDrawFixed

ListBox1.Items.Add("Black")
ListBox1.Items.Add("Red")
ListBox1.Items.Add("Green")
ListBox1.Items.Add("Blue")
ListBox1.Items.Add("Pink")

End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
DrawItemEventArgs) Handles ListBox1.DrawItem

Dim s As String = ListBox1.Items(e.Index).ToString
Dim b As New SolidBrush(Color.FromName(s))

e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawString(s, Font, b, e.Bounds.X, e.Bounds.Y)

b.Dispose

End Sub

</Code>

Hope this helps

Blu

Nov 20 '05 #4
* "Brett Miller" <br***@NOSPAMpsion.co.za> scripsit:
I need to change the colour of certain items in a listbox depending on
criteria I receive from my dataset? How would this be accomplished?


I suggest using a listview control instead because it natively supports
setting the color of the items. For the listbox control, you would have
to draw the items in the specific color yourself.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
hi guys,

Thanks for the response, I can seem to the .Drawmode property or the
..DrawItem event??? I'm coding for the compact framework, mybe thats the
issue?

regards,
bm

"Brett Miller" <br***@NOSPAMpsion.co.za> wrote in message
news:Oc**************@tk2msftngp13.phx.gbl...
Hi,

I need to change the colour of certain items in a listbox depending on
criteria I receive from my dataset? How would this be accomplished?

Thanks in advance...

BM

Nov 20 '05 #6
* "Brett Miller" <br***@NOSPAMpsion.co.za> scripsit:
Thanks for the response, I can seem to the .Drawmode property or the
.DrawItem event??? I'm coding for the compact framework, mybe thats the
issue?


For Compact Framework-related questions, consider posting to this group:

<URL:news://news.microsoft.com/microsoft.public.dotnet.framework.compactframework >

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7

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

Similar topics

2
by: Jim M | last post by:
I am having trouble changing any properties of the web form listbox from client side javascript. As an example, all of the lines below seem to cause an error... temp =...
2
by: Luca | last post by:
Hello, I'm using a windows form in which there is a standard ListBox control. I want to add/remove objects from the ArrayList associated to the ListBox, and I want the ListBox immediately shows...
2
by: Alpha | last post by:
I have a window application. On one of the form, there is a listbox and a few combox. The lstSchItem has a dataview as a datasource. The comboxes are bind to its selected value but through the...
1
by: Firewalker | last post by:
I am attempting to change the backColor property on the previously instantiated buttons FROM a listbox_doubleClick event. I thought it would be something like this: If...
0
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing...
6
by: segue | last post by:
Hi; I need to change the order of a listbox array from a form app where I select order up or down. What's a good way to do that? Regards; Segue
2
by: Dom | last post by:
I often see a listbox in which certain items are grayed, indicating that the item is "there" but disabled. It's a very helpful feature. Can you do that in cSharp? I don't find...
6
by: Dom | last post by:
In some programs, I see that certain items in a listbox have been grayed, to indicate that they are "there" but can not be chosen. This is very helpful. Can you do this in CSharp? Is there a...
3
by: Bruno Silva | last post by:
Is there anyway to put the items on a listbox with several colours on its items? For example Blue Red Red Blue .... and so on
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
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.