Problem Drawing Strings

G

Guest

vb.net 2k3:

I have this code that doesn't work unless I take out the g.DrawString
statements. If anyone can look over the code and tell me why I'd appreciate
it.

Dim g As Graphics
Dim LabelText As String
LabelText = "--"
x = x + 20
Dim varSlash As String
varSlash = "/"


Dim brushColor As System.Drawing.Color
Dim blackBrush = New SolidBrush(SystemColors.WindowText)
Dim blueBrush = New SolidBrush(brushColor.Blue)
Dim greenBrush = New SolidBrush(brushColor.Green)
Dim redBrush = New SolidBrush(brushColor.Red)
Dim yellowBrush = New SolidBrush(brushColor.Yellow)
Dim grayBrush = New SolidBrush(brushColor.Gray)
Dim labelFont As New Font("Arial", 40)
Dim format As New StringFormat
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center

y = 200
g.DrawString(varSection.ToString, Font, blackBrush, x, y)
y = y + 15
g.DrawString(LabelText.ToString, Font, blackBrush, x, y)
y = y + 5
g.DrawString(varCheckList.ToString, Font, blackBrush, x, y)
y = y + 50
g.DrawString(varValidated.ToString, Font, blueBrush, x, y)
y = y + 10
g.DrawString(varSlash.ToString, Font, blackBrush, x, y)
y = y + 10
g.DrawString(varAwaiting.ToString, Font, greenBrush, x, y)
y = y + 10
g.DrawString(varSlash.ToString, Font, blackBrush, x, y)
y = y + 10
g.DrawString(varNot.ToString, Font, redBrush, x, y)
y = y + 10
g.DrawString(varSlash.ToString, Font, blackBrush, x, y)
y = y + 10
g.DrawString(varFollow.ToString, Font, yellowBrush, x, y)
y = y + 10
g.DrawString(varSlash.ToString, Font, blackBrush, x, y)
y = y + 10
g.DrawString(varNA.ToString, Font, grayBrush, x, y)
 
G

Guest

You have only set "g" to a type of Graphics. You don't have a graphics
object to draw on. You need to create a graphics object from either a
control or a bitmap;

dim g as graphics = mycontrol.CreateGraphics

or

dim mybitmap As Bitmap = New Bitmap(Width, Height)
dim g as Graphics = Graphics.FromImage(v_BackImage)
 
G

Guest

Sorry, I forgot to change one of my variables...here is the corrected code:

You have only set "g" to a type of Graphics. You don't have a graphics
object to draw on. You need to create a graphics object from either a
control or a bitmap;

dim g as graphics = mycontrol.CreateGraphics

or

dim mybitmap As Bitmap = New Bitmap(Width, Height)
dim g as Graphics = Graphics.FromImage(mybitmap)
 
H

Herfried K. Wagner [MVP]

brix_zx2 said:
I have this code that doesn't work unless I take out the g.DrawString
statements. If anyone can look over the code and tell me why I'd
appreciate
it.

Dim g As Graphics

If you are using the code inside a form's or control's 'OnPaint' method or
'Paint' event handler, add this line:

\\\
g = e.Graphics
///
 
B

Bob Powell [MVP]

You have to get a Graphics object. The one you have is declared but is just
a placeholder.

Perhaps you might use

dim g as Graphics = e.Graphics

if you were responding to a Paint event o perhaps...

dim e as Graphics=Graphics.FromImage(...)

if you were drawing on an in-memory bitmap.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

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