I can't see my Drawing on a Button

mechwarrior3

Well-known member
Joined
Dec 19, 2005
Messages
67
Programming Experience
Beginner
Hello,

So, I am trying to use graphics to draw a black triangle on Button1. I am trying to draw this at the time the form loads. However, when I run my code, there is no triangle drawn on my button. :( Why not? Here is the code I'm using in the Form1_Load function:

My Code said:

Dim
g As Graphics
Dim myBrush AsNew SolidBrush(Color.Black)
Dim myPoints(2) As System.Drawing.Point
myPoints(0).X() = 0
myPoints(0).Y() = 0
myPoints(1).X() = 57
myPoints(1).Y() = 21
myPoints(2).X() = 114
myPoints(2).Y() = 0
g =
Me.Button1.CreateGraphics
' g.FillPolygon(myBrush, New Point() {New Point(0, 0), New Point(57, 21), New Point(114, 0)})
g.FillPolygon(myBrush, myPoints)


All help is greatly appreciated. :)
 
After a form loads, the form (and all of the controls contained within) are refreshed and therefore repainted.
To make the triangle "permanent" use the button's Paint EventHandler to do the drawing. The Paint event for a control is called whenever the control needs to be repainted.
 
Good call. :) Thank you very much, Paszt. This is how I'm taking care of it now.

In the Form1_Load code, I've added this line:
VB.NET:
 AddHandler Button1.Paint, AddressOf Me.DownButton_Paint

And the DownButton_Paint function looks like this:
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] g [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics = e.Graphics
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myBrush [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SolidBrush(Color.Black)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myPoints(2) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Drawing.Point
myPoints(0).X() = 0
myPoints(0).Y() = 10
myPoints(1).X() = 57
myPoints(1).Y() = 35
myPoints(2).X() = 114
myPoints(2).Y() = 10
g.FillPolygon(myBrush, myPoints)[/SIZE]



Again, thank you for your help. :)
 
Back
Top