473,386 Members | 1,705 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,386 software developers and data experts.

listbox item forecolor change

Does anyone know why the Listbox1.Refresh() command may not trigger the ListBox1_DrawItem sub every time?

In Microsoft Visual Basic 2010, a listbox has a forcolor and backcolor property. These properties change the forcolour and backcolor for all the items in the listbox. By default there is no property for the forecolor and backcolor of an individual item on a listbox, I am aware there is on a list view but I would still wish to use a listbox. I am trying to have the ability to change the forecolor and backcolor properties of individual items in the listbox. To do this the listbox's draw item sub must be used with the listbox's drawmode property set to OwnerDrawFixed. Then using a brush colour along with the e.graphics the forecolor or backcolor can be changed. I have seen and followed examples of how to do this for the currently selected item. Such as the one from ehow's website. What I am tiring to do however is change the colour of the litsbox item as it is added depending on a variable.

Here is my code:
Expand|Select|Wrap|Line Numbers
  1.  
  2.     Private Sub listbox_add()
  3.  
  4.         Me.ListBox1.Items.Add(listbox_text(list_num)) ' adds the line to the list box
  5.         add_item_colour = True
  6.         ListBox1.Refresh()
  7.  
  8. End Sub
  9.  
  10.  
  11.  
  12.     Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
  13.     Dim myBrush As Brush = Brushes.Black
  14.  
  15.     e.DrawBackground()
  16.     If add_item_colour = True Then
  17.         If blue_message = True Then
  18.             myBrush = Brushes.Blue
  19.         Else
  20.             myBrush = Brushes.Black
  21.         End If
  22.         e.Graphics.DrawString(ListBox1.Items.Item(list_num), ListBox1.Font, myBrush, _
  23.         New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
  24.         add_item_colour = False
  25.     End If
  26.     e.DrawFocusRectangle()
  27. End Sub
  28.  
The listbox_text is a string array that stores the string being added, the list_num is a integer that increments when new items are added to the listbox and the blue_message is a Boolean that it true when I want a blue message and false when I don't.

The problem I seem to be having is that Listbox1.Refresh() command does not seem to be triggering the ListBox1_DrawItem sub every time it is called. I found this by using brake points. Does anyone know why this might be the case and how I could fix it?

Thanks, any help on this would be much appreciated.
Sep 3 '13 #1
0 1107

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name...
0
by: E Miller | last post by:
Visual Studio .NET 2002 I've got an application in which I need to be able to click on a listbox item to initiate a dialog pertaining to that item. I'd like to be able to keep the dialog...
1
by: Claire | last post by:
As a Listbox.item, rather than using a string I'm using a ListItems object, overriding the ToString method When I add items to listbox.items, my strings are shown correctly. On the otherhand, when...
0
by: Robert Karlsson | last post by:
Hi, Is there a way of generating a tab within a ListBox item to create the illusion of columns within the item? Example: Header 1 Header 2 <- labels...
3
by: RockNRoll | last post by:
Greetings, Can I select an item in a listbox based on user textbox entry and a submit button? Can you provide any code examples for what needs to occur when the user clicks submit? Thank you,...
21
by: Bilal Abbasi | last post by:
I realize that you can add items to a list box as objects so you can have access to more than just one property like the itemindex in vb6. Question I have is how do I cause the listbox to show a...
3
by: Dean Slindee | last post by:
In a checked listbox, I am allowing drag/drop of the items within (resequencing). Problem is, when dropping a checked item, the checked state always reverts to unchecked (unwanted). Anyone know...
2
by: baret bonden | last post by:
Trying to return a selected listbox item to another form .tried lots of ways; defining public variables and passing those as well as textboxes ..I' m able to display the chosen item on it's form...
8
by: Dan | last post by:
Is there any way to assign a tag value to each item in a listbox?
3
by: Kevin Walzer | last post by:
I'm trying to set the active item in a Tkinter listbox to my application's currently-defined default font. Here's how I get the fonts loaded into the listbox: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.