graphics question

B

billsahiker

I am using the Microsoft Press training kit for the Framework 2.0
exam. It has the following example for putting text into a font and
drawing it. I created a new Windows Application. But where do I put
the code? The book does not say -it assumes the reader knows. I tried
the form.load event and the form.paint event, but I get an empty form
when the program runs.

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

I also tried putting the two declarations at the beginning of the
Form1 class and changed them to Private properties.

Bill
 
R

RobinS

Seems to me you could put that in the form_load event. Stick it there, then
show all of the code. Maybe your event handler isn't set up right.

Robin S.
 
M

Mythran

I am using the Microsoft Press training kit for the Framework 2.0
exam. It has the following example for putting text into a font and
drawing it. I created a new Windows Application. But where do I put
the code? The book does not say -it assumes the reader knows. I tried
the form.load event and the form.paint event, but I get an empty form
when the program runs.

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

I also tried putting the two declarations at the beginning of the
Form1 class and changed them to Private properties.

Bill

That code can be put anywhere, but I would suggest changing it a little and
put it in the OnPaint overrides (or Paint event).

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)

Dim f As Font = New Font("Arial", 12, FontStyle.Bold)
e.Graphics.DrawString("something", f, Brushes.Blue, 10, 10)
End Sub


' NOTE: Above code is off top of my head, along with the OP's 'modified'
code above.
' NOTE: OP's code looks like it's the same as mine....off top of head or
typed out from book, since there is a mis-spelling.

Anywho, the sample I give doesn't require you to create a Graphics object.
The OnPaint method's PaintEventArgs parameter contains a Graphics property
so you can use that instead.

HTH,
Mythran
 
B

billsahiker

I am using vs2005 to generate the code for the event signature so it
should be correct. Being new to .net, I must be missing something
very basic, but what? Could I trouble you to run this code to see if
it works for you?

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("something", f, Brushes.Blue, 10, 10)
End Sub

End Class
 
B

billsahiker

Thanks. my post indicated I am working with the 70-536 exam training
kit and the code is from the book, so I really need to find out if it
works as is, or if there is an error in the book. I am thinking that
something is missing, but what?
 
B

billsahiker

I do not need a workaround or alternative. Here is my complete class.
The code in the paint event is from the 536 training kit, which does
not indicate what Sub to put it in and I need to know if it works as
is, or if there is something missing, or if it should go in another
Sub.

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("something", f, Brushes.Blue, 10, 10)
End Sub


End Class
 
R

RobinS

Someone answered this when you reposted the question.

Robin S.
---------------------------------
 
M

Mythran

I do not need a workaround or alternative. Here is my complete class.
The code in the paint event is from the 536 training kit, which does
not indicate what Sub to put it in and I need to know if it works as
is, or if there is something missing, or if it should go in another
Sub.

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("something", f, Brushes.Blue, 10, 10)
End Sub


End Class

It should work as is, in the Form Paint event as shown.. The problem I see,
though, is you are creating graphics IN the paint event handler. This is
not needed and is redundant because you have the graphics object for the
form as a property in the PaintEventArgs parameter passed into the same
event handler. To access this property, use e.Graphics. Trust me on this,
it is better to use it than to create a new Graphics instance every call to
the paint handler.

HTH,
Mythran
 

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