13 Replies - 37340 Views - Last Post: 08 May 2011 - 10:41 AM Rate Topic: -----

#1 zippp  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 22-June 09

Listbox - change item's color

Posted 26 June 2009 - 03:24 AM

Ok, i almost finished my music player, but i need one more thing.

I have a ListBox as PlayList. It is possible, when i double click a Item to change he`s text color or background color?

Tnx!

This post has been edited by zippp: 26 June 2009 - 03:28 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Listbox - change item's color

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8390
  • View blog
  • Posts: 31,200
  • Joined: 12-June 08

Re: Listbox - change item's color

Posted 26 June 2009 - 08:23 AM

No there is no particular way of doing that since the listbox is a glorified text box.

If you used a listVIEW then yes. You can call the back/front color for the list view's items.
Was This Post Helpful? 0
  • +
  • -

#3 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: Listbox - change item's color

Posted 26 June 2009 - 09:18 AM

View Postzippp, on 26 Jun, 2009 - 02:24 AM, said:

Ok, i almost finished my music player, but i need one more thing.

I have a ListBox as PlayList. It is possible, when i double click a Item to change he`s text color or background color?

Tnx!



It is possible, but you would have to handle the drawing of the listbox items yourself. The Listbox has a property named "DrawMode" that you would have to set to OwnerDrawFixed. You would then have to handle the ItemDraw event for the listbox.
Here is a simple example that draws selected items in red text with a yellow background:
	Dim clrSelectedText As Color = Color.Red	'Our color for selected text 
	Dim clrHighlight As Color = Color.Yellow	'Our background for selected items

	Private Sub lBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lBox.DrawItem
		If e.State = DrawItemState.Selected Then
			'This item is selected
			'e.DrawBackground()
			e.Graphics.FillRectangle(New SolidBrush(clrHighlight), e.Bounds)	'Fill the item's rectangle with our highlight
			e.Graphics.DrawString(lBox.Items.Item(e.Index), e.Font, New SolidBrush(clrSelectedText), e.Bounds)  'Draw the text for the item
		ElseIf e.State = DrawItemState.None Then
			'This item has no state
			e.DrawBackground()  'Draw our regular background
			e.Graphics.DrawString(lBox.Items.Item(e.Index), e.Font, Brushes.Black, e.Bounds)	'Draw the item text in its regular color
		End If
		'Draws a focus rectangle around the item if it has focus
		e.DrawFocusRectangle()
	End Sub

More detailed info can be found on MSDN.

But, depending on exactly what you're trying to accomplish it might be easier\more practical to use a ListView as modi123_1 suggested.
Was This Post Helpful? 1
  • +
  • -

#4 zippp  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 22-June 09

Re: Listbox - change item's color

Posted 26 June 2009 - 01:14 PM

Tnx dude!

Edited: i`m using it as a playlist for a music player....so i have to change so many if i`ll add a listview instead of listbox...

This post has been edited by zippp: 26 June 2009 - 01:18 PM

Was This Post Helpful? 0
  • +
  • -

#5 swehunter2000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 04-April 11

Re: Listbox - change item's color

Posted 07 May 2011 - 08:00 AM

View Postzippp, on 26 June 2009 - 01:14 PM, said:

Tnx dude!

Edited: i`m using it as a playlist for a music player....so i have to change so many if i`ll add a listview instead of listbox...


Did you make it? i have no clue on how to set the "lBox_DrawItem(LstSong2, e as what?)"
Was This Post Helpful? 0
  • +
  • -

#6 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Listbox - change item's color

Posted 07 May 2011 - 08:10 AM

Hi,

You have to draw the items yourself, this is easy to do. Set DrawMode=OwnerDrawFixed and handle the DrawItem event. Example:

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    e.DrawBackground()
    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
        e.Graphics.FillRectangle(Brushes.LightGreen, e.Bounds)
    End If
    Using b As New SolidBrush(e.ForeColor)
        e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items(e.Index)), e.Font, b, e.Bounds)
    End Using
    e.DrawFocusRectangle()
End Sub

Was This Post Helpful? 0
  • +
  • -

#7 swehunter2000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 04-April 11

Re: Listbox - change item's color

Posted 07 May 2011 - 08:40 AM

View PostLuc001, on 07 May 2011 - 08:10 AM, said:

Hi,

You have to draw the items yourself, this is easy to do. Set DrawMode=OwnerDrawFixed and handle the DrawItem event. Example:

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    e.DrawBackground()
    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
        e.Graphics.FillRectangle(Brushes.LightGreen, e.Bounds)
    End If
    Using b As New SolidBrush(e.ForeColor)
        e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items(e.Index)), e.Font, b, e.Bounds)
    End Using
    e.DrawFocusRectangle()
End Sub


if im right this onlye changes the item colors as they are beeing drawed and can not change the items txt color efter it has been drawed?
Was This Post Helpful? 0
  • +
  • -

#8 juunas  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 87
  • Joined: 25-March 09

Re: Listbox - change item's color

Posted 07 May 2011 - 11:15 PM

Yes, this style definitely draws them as they are being drawn. However, you can use this in your form's code whenever you need to draw the items again.

ListBox.Invalidate()


This will make it draw the items again.

E: OP, you can then just use a variable to store the index of the item you wish to color.

This post has been edited by juunas: 07 May 2011 - 11:18 PM

Was This Post Helpful? 0
  • +
  • -

#9 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Listbox - change item's color

Posted 07 May 2011 - 11:40 PM

Hi,

The code I provided does what PO asked for to do.
That means that when you created a Listbox set Drawmode in the properties of your Listbox into OwnerDrawFixed and add some items.

Then debug and everytime you select an item it will change the backcolor of your item.

@juunas you doesn't need to Invalidate the listbox.
Was This Post Helpful? 0
  • +
  • -

#10 juunas  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 87
  • Joined: 25-March 09

Re: Listbox - change item's color

Posted 08 May 2011 - 12:58 AM

@Luc001:

You won't need to Invalidate even if I don't change the SelectedIndex? I don't like changing it to color the item, since if I was selecting some songs to remove for example, it would remove all my selections, making me start selecting them all over again.
Was This Post Helpful? 0
  • +
  • -

#11 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Listbox - change item's color

Posted 08 May 2011 - 08:06 AM

Hi juunas,

I don't understand what do you mean with:

Quote

I don't like changing it to color the item, since if I was selecting some songs to remove for example, it would remove all my selections, making me start selecting them all over again.


Here's an example where you can add 1 item to and remove 1 item at a time in your listbox and still can select the others with the new created itemcolor.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        e.DrawBackground()
        If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
            e.Graphics.FillRectangle(Brushes.LightSeaGreen, e.Bounds)
        End If
        Using b As New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items(e.Index)), e.Font, b, e.Bounds)
        End Using
        e.DrawFocusRectangle()
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        ListBox1.Items.Remove(ListBox1.SelectedItem)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ListBox1.Items.Add(TextBox1.Text)
      End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#12 juunas  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 87
  • Joined: 25-March 09

Re: Listbox - change item's color

Posted 08 May 2011 - 08:19 AM

I guess I didn't explain well enough. In my player, when the song changes, that new song is colored red, but it doesn't affect selections. I mean, it doesn't change the ListBox's SelectedIndex to the new song.
Was This Post Helpful? 0
  • +
  • -

#13 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Listbox - change item's color

Posted 08 May 2011 - 08:47 AM

Hi,

Can you show us your player code?
Was This Post Helpful? 0
  • +
  • -

#14 juunas  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 87
  • Joined: 25-March 09

Re: Listbox - change item's color

Posted 08 May 2011 - 10:41 AM

I use a custom ListBox class on my form.

Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Public Class ColoredListBox
    Inherits Windows.Forms.ListBox

    Private cIndex As Integer = -1

    Public Sub New()
        Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
    End Sub

    Public Property ColoredIndex() As Integer
        Get
            Return cIndex
        End Get
        Set(ByVal value As Integer)
            cIndex = value
        End Set
    End Property

    Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
        MyBase.OnDrawItem(e)
        Try
            e.DrawBackground()
            Dim curSong As Integer = Form1.currentPlaying
            Dim myBrush As SolidBrush = Nothing
            Dim itemColor As SolidBrush = Nothing
            Dim myPen As Pen = Nothing
            If e.Index = curSong Then
                myBrush = New SolidBrush(e.BackColor)
                myPen = New Pen(System.Drawing.SystemColors.ControlDark)
                itemColor = New SolidBrush(Color.Red)
                cIndex = curSong
            Else
                myBrush = New SolidBrush(e.BackColor)
                myPen = New Pen(System.Drawing.SystemColors.ControlDark)
                itemColor = New SolidBrush(e.ForeColor)
            End If
            e.Graphics.FillRectangle(myBrush, e.Bounds)
            e.Graphics.DrawRectangle(myPen, e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height)
            e.Graphics.DrawString(Me.GetItemText(Me.Items(e.Index)), e.Font, itemColor, e.Bounds.X + 2, e.Bounds.Y, StringFormat.GenericDefault)
            myBrush.Dispose()
            itemColor.Dispose()
            myPen.Dispose()
        Catch ex As Exception
            Console.WriteLine(ex.StackTrace())
        End Try
    End Sub

End Class

The draw event checks which is the currently playing song, and makes that one's forecolor red.

In my form, I just use this whenever I need to update the colors. The currentPlaying variable is -1 when no song is playing, which means that no song will be colored red at that time.
playlist.Invalidate()


Also, I use a cIndex (colored index) property to track which one is colored.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1