Hi,
I need the change some controls colors. I have a preview;
http://i44.tinypic.com/28w1hxg.jpg
How to change the blue color to some color?
Thanks.
Printable View
Hi,
I need the change some controls colors. I have a preview;
http://i44.tinypic.com/28w1hxg.jpg
How to change the blue color to some color?
Thanks.
Is some a colour?
As vague as your question is to this forum, perhaps what you mean to say is, that you are using listboxes and you would like the menu highlight to change colour from blue to some other colour. So how would you control the colour of that highlight?
If there isn't a property to change that you can custom draw the controls. Just google it, there's loads of examples available.
You must call: ListBox1.DrawMode = DrawMode.OwnerDrawFixed from somewhere in your application or set in properties.
Code:Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
' Draw the background of the ListBox control for each item.
e.DrawBackground()
' Define the default color of the brush as black.
Dim dfltBrush As Brush = Brushes.Black
' If the item is the selected item, then draw the rectangle.
If (e.State And DrawItemState.Selected = DrawItemState.Selected) Then
' This is the colour for the selectedItem.
e.Graphics.FillRectangle(Brushes.Red, e.Bounds)
Else
' Otherwise, draw the rectangle filled in beige.
e.Graphics.FillRectangle(Brushes.Beige, e.Bounds)
End If
' Draw the current item text based on the current Font and the custom brush settings.
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, dfltBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
' If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle()
End Sub
I have just noticed a problem with my example. If you implement the event in the form's class, then the item drawing will carry over into other controls, which might raise a problem. Can anyone tell me if this is by design?! Or perhaps a suggestion on how to suppress it in the form?
To remove this problem, create an inherited class overriding the OnDrawItem event, and implement the code in that.
vb Code:
Public Class MyListBox Inherits ListBox Public Sub New() MyBase.New() End Sub ''' <summary> ''' Constructor accepting an ArrayList of intended items for the ItemsCollection. ''' </summary> ''' <param name="arr">An ArrayList of type String</param> ''' <remarks></remarks> Public Sub New(ByVal arr As ArrayList) Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed For i As Integer = 0 To arr.Count - 1 Me.Items.Add(arr(i).ToString) Next End Sub Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs) MyBase.OnDrawItem(e) ' Draw the background of the ListBox control for each item. e.DrawBackground() ' Define the default color of the brush as black. Dim dfltBrush As Brush = Brushes.Black ' If the item is the selected item, then draw the rectangle filled in blue. If (e.State And DrawItemState.Selected = DrawItemState.Selected) Then ' This is the colour for the selectedItem. e.Graphics.FillRectangle(Brushes.Red, e.Bounds) Else ' Otherwise, draw the rectangle filled in beige. e.Graphics.FillRectangle(Brushes.Beige, e.Bounds) End If ' Draw the current item text based on the current Font and the custom brush settings. e.Graphics.DrawString(Me.Items(e.Index), e.Font, dfltBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)) ' If the ListBox has focus, draw a focus rectangle around the selected item. e.DrawFocusRectangle() End Sub
@eatmycode thanks dude.
By the way, the same thing can be done without creating a derived listbox class.
Just place the same code (except MyBase.OnDrawItem(e)) in the normal listbox DrawItem event handler.