Results 1 to 3 of 3

Thread: Custom Color Help? (Solved)

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Custom Color Help? (Solved)

    Hi! I'm having trouble setting a custom color for a rectangle. I used a solidbrush to use the color, and brush1 to = solidbrush. This code does not work with e.Graphics.FillRectangle(LBrush, 0, 0, 50, 50). Both ways just give me the form with a huge red x over everything. What is wrong? Thanks!

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim LColor As Color = New Color
    4.     Dim LBrush As SolidBrush
    5.  
    6.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    7.  
    8.         NewColor()
    9.  
    10.         Me.Invalidate()
    11.  
    12.     End Sub
    13.  
    14.     Private Sub NewColor()
    15.  
    16.         Randomize()
    17.         LColor = Color.FromArgb(Math.Round(Rnd() * 150) + 100, Math.Round(Rnd() * 150) + 100, Math.Round(Rnd() * 150) + 100)
    18.         LBrush.Color = LColor
    19.  
    20.     End Sub
    21.  
    22.     Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    23.  
    24.         Dim Brush1 As Brush = LBrush
    25.  
    26.         e.Graphics.FillRectangle(Brush1, 0, 0, 50, 50)
    27.  
    28.     End Sub
    29.  
    30. End Class
    Last edited by NinjaNic; Dec 6th, 2014 at 04:40 PM.

  2. #2

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Custom Color Help?

    Found my mistake: Solidbrush works but it must be created and set in the same block (Form1_Paint).

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim LColor As Color = New Color
    4.  
    5.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         NewColor()
    8.  
    9.         Me.Invalidate()
    10.  
    11.     End Sub
    12.  
    13.     Private Sub NewColor()
    14.  
    15.         Randomize()
    16.         LColor = Color.FromArgb(Math.Round(Rnd() * 150) + 100, Math.Round(Rnd() * 150) + 100, Math.Round(Rnd() * 150) + 100)
    17.  
    18.     End Sub
    19.  
    20.     Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    21.  
    22.         Dim LBrush As SolidBrush = New SolidBrush(LColor)
    23.  
    24.         e.Graphics.FillRectangle(LBrush, 0, 0, 50, 50)
    25.  
    26.     End Sub
    27.  
    28. End Class

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Custom Color Help? (Solved)

    When you create any disposable object, make sure that you dispose it when you're done with it. This is usually done with a Using block if possible, e.g.
    Code:
    Using LBrush As SolidBrush = New SolidBrush(LColor)
        e.Graphics.FillRectangle(LBrush, 0, 0, 50, 50)
    End Using

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