Results 1 to 7 of 7

Thread: [RESOLVED] Windows Form Controls Colors

  1. #1

    Thread Starter
    Lively Member cevem's Avatar
    Join Date
    Feb 2010
    Location
    Mars
    Posts
    78

    Resolved [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.

  2. #2
    Lively Member eatmycode's Avatar
    Join Date
    Mar 2010
    Location
    Kingston upon Hull
    Posts
    74

    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

  3. #3

  4. #4
    Lively Member eatmycode's Avatar
    Join Date
    Mar 2010
    Location
    Kingston upon Hull
    Posts
    74

    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

  5. #5
    Lively Member eatmycode's Avatar
    Join Date
    Mar 2010
    Location
    Kingston upon Hull
    Posts
    74

    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:
    1. Public Class MyListBox
    2.     Inherits ListBox
    3.  
    4.     Public Sub New()
    5.         MyBase.New()
    6.     End Sub
    7.  
    8.     ''' <summary>
    9.     ''' Constructor accepting an ArrayList of intended items for the ItemsCollection.
    10.     ''' </summary>
    11.     ''' <param name="arr">An ArrayList of type String</param>
    12.     ''' <remarks></remarks>
    13.     Public Sub New(ByVal arr As ArrayList)
    14.         Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    15.         For i As Integer = 0 To arr.Count - 1
    16.             Me.Items.Add(arr(i).ToString)
    17.         Next
    18.     End Sub
    19.  
    20.     Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
    21.         MyBase.OnDrawItem(e)
    22.         ' Draw the background of the ListBox control for each item.
    23.         e.DrawBackground()
    24.         ' Define the default color of the brush as black.
    25.         Dim dfltBrush As Brush = Brushes.Black
    26.  
    27.         ' If the item is the selected item, then draw the rectangle filled in blue.
    28.         If (e.State And DrawItemState.Selected = DrawItemState.Selected) Then
    29.             ' This is the colour for the selectedItem.
    30.             e.Graphics.FillRectangle(Brushes.Red, e.Bounds)
    31.         Else
    32.             ' Otherwise, draw the rectangle filled in beige.
    33.             e.Graphics.FillRectangle(Brushes.Beige, e.Bounds)
    34.         End If
    35.  
    36.         ' Draw the current item text based on the current Font and the custom brush settings.
    37.         e.Graphics.DrawString(Me.Items(e.Index), e.Font, dfltBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
    38.         ' If the ListBox has focus, draw a focus rectangle around the selected item.
    39.         e.DrawFocusRectangle()
    40.     End Sub
    Technik ... Kniff, die Welt so einzurichten, dass wir sie nicht erleben mussen

    Max Frisch

  6. #6

    Thread Starter
    Lively Member cevem's Avatar
    Join Date
    Feb 2010
    Location
    Mars
    Posts
    78

    Re: Windows Form Controls Colors

    @eatmycode thanks dude.

  7. #7
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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
  •  



Click Here to Expand Forum to Full Width