|
-
Dec 6th, 2014, 04:01 PM
#1
Thread Starter
Addicted Member
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:
Public Class Form1
Dim LColor As Color = New Color
Dim LBrush As SolidBrush
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
NewColor()
Me.Invalidate()
End Sub
Private Sub NewColor()
Randomize()
LColor = Color.FromArgb(Math.Round(Rnd() * 150) + 100, Math.Round(Rnd() * 150) + 100, Math.Round(Rnd() * 150) + 100)
LBrush.Color = LColor
End Sub
Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim Brush1 As Brush = LBrush
e.Graphics.FillRectangle(Brush1, 0, 0, 50, 50)
End Sub
End Class
Last edited by NinjaNic; Dec 6th, 2014 at 04:40 PM.
-
Dec 6th, 2014, 04:33 PM
#2
Thread Starter
Addicted Member
Re: Custom Color Help?
Found my mistake: Solidbrush works but it must be created and set in the same block (Form1_Paint).
vb Code:
Public Class Form1 Dim LColor As Color = New Color Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load NewColor() Me.Invalidate() End Sub Private Sub NewColor() Randomize() LColor = Color.FromArgb(Math.Round(Rnd() * 150) + 100, Math.Round(Rnd() * 150) + 100, Math.Round(Rnd() * 150) + 100) End Sub Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim LBrush As SolidBrush = New SolidBrush(LColor) e.Graphics.FillRectangle(LBrush, 0, 0, 50, 50) End Sub End Class
-
Dec 7th, 2014, 09:12 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|