graphics.drawstring

B

billsahiker

There is a code example in the MS Training Kit for the Framework 2.0
test that does not seem to work. The example has just three lines of
code, but does not indicate where to put the code, so I put it in the
form.load event and it generates a blank screen, but is supposed to
display "Hello World" on the form. Then I put it in the paint event
as shown below and still I get a blank form when it runs. Any ideas
what is wrong? I previously posted here and was given another way of
accomplishing the task -that is not the point as I am studying for the
exam and need to know how to make this code work, or find out if it
has an error or something missing.

Public Class Form1


Private Sub Form1_Paint(ByVal sender as Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint

Dim g as Graphics = me.CreateGraphics
Dim f as Font = New Font("Arial", 12, FonstStyle.Bold)
g.Drawstring("Hello World", f, Brushes.Blue, 10, 10)

End Sub


End Class
 
H

Herfried K. Wagner [MVP]

Private Sub Form1_Paint(ByVal sender as Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint

Dim g as Graphics = me.CreateGraphics

=> 'Dim g As Graphics = e.Graphics'.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

There is a code example in the MS Training Kit for the Framework 2.0
test that does not seem to work. The example has just three lines of
code, but does not indicate where to put the code, so I put it in the
form.load event and it generates a blank screen, but is supposed to
display "Hello World" on the form. Then I put it in the paint event
as shown below and still I get a blank form when it runs. Any ideas
what is wrong? I previously posted here and was given another way of
accomplishing the task -that is not the point as I am studying for the
exam and need to know how to make this code work, or find out if it
has an error or something missing.

Public Class Form1


Private Sub Form1_Paint(ByVal sender as Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint

Dim g as Graphics = me.CreateGraphics
Dim f as Font = New Font("Arial", 12, FonstStyle.Bold)
g.Drawstring("Hello World", f, Brushes.Blue, 10, 10)

End Sub


End Class

The code example for Control.CreateGraphics only uses the Graphics
object to measure a string, it doesn't draw anything, so I'm not sure
that it can even be used to draw anything on the screen.

Anyhow, if you create a Graphics object this way you should call it's
Dispose method when you are done with it.

The PaintEventArgs object contains a Graphics object that you are
supposed to use in the Paint event, you shouldn't create another one.
Especially as the one provided is already clipped down to the size of
the control (or the part of the control that needs to be redrawn) so
that you won't accidentally draw outside the control.

The supplied Graphics object should not be disposed. That is done by the
code that created it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top