|
-
Mar 11th, 2010, 03:49 PM
#1
Thread Starter
Lively Member
[RESOLVED] Windows Form Controls Colors
Hi,
I need the change some controls colors. I have a preview;

How to change the blue color to some color?
Thanks.
-
Mar 11th, 2010, 03:53 PM
#2
Lively Member
Re: Windows Form Controls Colors
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?
Last edited by eatmycode; Mar 11th, 2010 at 04:00 PM.
Technik ... Kniff, die Welt so einzurichten, dass wir sie nicht erleben mussen
Max Frisch
-
Mar 11th, 2010, 04:11 PM
#3
Re: Windows Form Controls Colors
If there isn't a property to change that you can custom draw the controls. Just google it, there's loads of examples available.
-
Mar 11th, 2010, 04:33 PM
#4
Lively Member
Re: Windows Form Controls Colors
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
Technik ... Kniff, die Welt so einzurichten, dass wir sie nicht erleben mussen
Max Frisch
-
Mar 11th, 2010, 05:21 PM
#5
Lively Member
Re: Windows Form Controls Colors
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
Technik ... Kniff, die Welt so einzurichten, dass wir sie nicht erleben mussen
Max Frisch
-
Mar 11th, 2010, 05:23 PM
#6
Thread Starter
Lively Member
Re: Windows Form Controls Colors
-
Mar 11th, 2010, 05:33 PM
#7
Re: [RESOLVED] Windows Form Controls Colors
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|