?
Solved

Colouring a listbox?

Posted on 2006-05-24
17
Medium Priority
?
535 Views
Last Modified: 2008-03-10
Hi,

I am currently using a listbox that will display order numbers

is it possible to set the back colour of each item in the listbox?

I want to have a colour for orders due in the AM and colours for orders due in the PM

0
Comment
Question by:foobarr
  • 8
  • 6
  • 3
17 Comments
 
LVL 5

Expert Comment

by:mydasx
ID: 16755909
overload the paint operation.
0
 
LVL 6

Accepted Solution

by:
ctm5 earned 2000 total points
ID: 16756311
If you use a ListView instead, you can set the fore color or the back color of each item separately. Set HeaderStyle=None, View=Details, and set the column width to match the width of the control. This way you will get the appearance of a ListBox and the entire selected item will have the backcolor.


ctm5
0
 
LVL 6

Expert Comment

by:ctm5
ID: 16756318
To set backcolor:

ListView1.Items(1).BackColor = Color.Red
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Author Comment

by:foobarr
ID: 16760606
i tried to teh listview and i got it working but now I need to get teh index of what I selected normally i would go
tempIndex = Me.lstOrderNotScheduled.SelectedIndex() for my listbox

but it is not the same for a listview...what should I do?
0
 
LVL 7

Author Comment

by:foobarr
ID: 16760943
I found this

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21426163.html?query=color+listbox&topics=866

which is what I wanted initially with the listboxes


but the problem now is that when i select an item it does not highlight that item

0
 
LVL 7

Author Comment

by:foobarr
ID: 16761209
ACtually I got that to work

Dim BrushBG As Brush
        Dim BrushText As Brush
        Dim selected As Boolean
        Dim textFont As Font


        e.DrawBackground()
        textFont = New Font("Arial", 12, FontStyle.Bold)

        If (e.Index Mod 2) = 0 Then
            BrushBG = Brushes.Red
            BrushText = Brushes.DarkBlue
        Else
            BrushBG = Brushes.Yellow
            BrushText = Brushes.DarkBlue
        End If

        If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
            selected = True
        Else
            selected = False

        End If

        If selected = True Then
            BrushBG = Brushes.DarkBlue
            BrushText = Brushes.White
        End If

        ' Draw background in selected color
        e.Graphics.FillRectangle(BrushBG, e.Bounds)
        e.Graphics.DrawRectangle(SystemPens.WindowText, e.Bounds)

        ' Draw the current item text based on the current Font and the custom brush settings.
        If e.Index <> -1 Then
            e.Graphics.DrawString(lstOrderScheduled.Items(e.Index).ToString, textFont, BrushText, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        End If
        ' If the ListBox has focus, draw a focus rectangle around the selected item.
        e.DrawFocusRectangle()
0
 
LVL 6

Expert Comment

by:ctm5
ID: 16761565
If you are still interested in the ListView:

ListView1.FocusedItem.Index

ctm5
0
 
LVL 7

Author Comment

by:foobarr
ID: 16763959
I am definitely going to try the listview way as well because I just ran into some newer problems with the listbox so i will be posting back soon

0
 
LVL 7

Author Comment

by:foobarr
ID: 16764190
Okay I've gotten them to write to the listview properly

but my problem now is handling changing the background color.  

is there when i add the item to teh listview I can immediately add it with a certain background colour?

I initially was going to iterate through the listview after I added it, but then I realized that I would iterate through all items each time so that would be useless...

Maybe if I get the index of before I added to the listview and after I aded to the listview...and then have a for loop that will iterate through the specified indexes and colour accordingly...



dSet = New DataSet

        dSet = connMySQL.returnOverDueOrdersNotScheduled(Me.lblSchedulingDateChosen.Text)
        For Each pRow In dSet.Tables("orders").Rows

            orderStatus = "Overdue"
            lstOrderNotScheduled.Items.Add(pRow("OrderNumber").ToString())

            'add the item background color here??
            'for each item in the listview
            'listview.item(index#).backcolor = blah



        Next

        'secondly get the orders that are due in teh AM and then mark them
        dSet = connMySQL.returnDueInAMOrdersNotScheduled(Me.lblSchedulingDateChosen.Text, "AM")
        For Each pRow In dSet.Tables("orders").Rows

            orderStatus = "DueInAM"
            lstOrderNotScheduled.Items.Add(pRow("OrderNumber").ToString())
        Next

        'thirdly get the orders that are due in the PM and then mark them
        dSet = connMySQL.returnDueInPMOrdersNotScheduled(Me.lblSchedulingDateChosen.Text, "PM")
        For Each pRow In dSet.Tables("orders").Rows

            orderStatus = "DueInPM"
            lstOrderNotScheduled.Items.Add(pRow("OrderNumber").ToString())
        Next
0
 
LVL 7

Author Comment

by:foobarr
ID: 16764381
Actually I would rather be able to draw the backcolor in the Event Handler DrawItem of the ListView


but I need to then figure out how to tell the Event Handler Draw Item that I only want certain items to be coloured a certain way
0
 
LVL 6

Expert Comment

by:ctm5
ID: 16764482
Yes, you can set the background color as you add them. Try this:

        Dim lvitem As ListViewItem = ListView1.Items.Add(pRow("OrderNumber").ToString())
        ListView1.Items(lvitem.Index).BackColor = Color.Purple


ctm5
0
 
LVL 5

Expert Comment

by:mydasx
ID: 16764505
Actually I would rather be able to draw the backcolor in the Event Handler DrawItem of the ListView


but I need to then figure out how to tell the Event Handler Draw Item that I only want certain items to be coloured a certain way

I accomplish this with an invisible column that i call "colorType" you can then in your drawitem event reference the invisible row and put a case statement for drawing the colors.  If you want to change these things on the fly, do an invalidate when you make a change of the invisible row.
0
 
LVL 5

Expert Comment

by:mydasx
ID: 16764545
my guts usually look something like this:
in this case i'm marking things w/ an invisible deleted row as pink
and i'm coloring things w/ a trimrule w/ a different color depending on the trim rule
(this is for datagridview but the same idea applies)

Dim deleted As Boolean = CBool(Me.DataGridView1.Rows(e.RowIndex).Cells("Deleted").Value)
        Dim trimRule As String = CStr(Me.DataGridView1.Rows(e.RowIndex).Cells("TrimRule").Value)
        If (deleted) Then
            e.CellStyle.BackColor = Color.Pink
        End If
        Select Case trimRule
            Case "HNLInterIslandNonStopOnly"
                e.CellStyle.BackColor = Color.Coral
            Case "MainlandToMainlandCleanup"
                e.CellStyle.BackColor = Color.RosyBrown
            Case "OGGJHMCleanup"
                e.CellStyle.BackColor = Color.SteelBlue
            Case "IslandAirCleanup"
                e.CellStyle.BackColor = Color.SeaGreen
            Case "ShortestRoute"
                e.CellStyle.BackColor = Color.Orchid
        End Select
0
 
LVL 7

Author Comment

by:foobarr
ID: 16769482
last question

with the listview

I want to clear the listview before I populate it...

I used listview.clear() but that delete all the rows and columns...

is there any way to just clear the rows from that column?


0
 
LVL 6

Expert Comment

by:ctm5
ID: 16769924
Sure:

        ListView1.Items(1).Remove()

ctm5
0
 
LVL 7

Author Comment

by:foobarr
ID: 16770302
there is no way to remove them all?

I know I can put that statement in a loop...but no clear type command?
0
 
LVL 6

Expert Comment

by:ctm5
ID: 16770344
Sorry -- I misread your question.

So you want to remove, say Column 2 from a three-column listview. Is that correct?

If that is what you want, then:

        ListView1.Columns(1).Dispose()

ctm5

A hint: Lots of times you can figure out this sort of stuff by using Intellisense. If you are unfamiliar with that, check it out.


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question