PC Review


Reply
Thread Tools Rate Thread

Drawing a string

 
 
=?Utf-8?B?Q3liZXJkb3Q=?=
Guest
Posts: n/a
 
      24th Sep 2004
Hello,
I've got a little problem. I want to draw a string to my form with the
follówing code:

Public Sub titleSetTime(ByVal newTime As String)
Dim g As System.Drawing.Graphics
g.DrawString(newTime, New System.Drawing.Font("Microsoft Sans
Serif", 10, System.Drawing.FontStyle.Regular), New
SolidBrush(SystemColors.ControlText), 200, 2)
End Sub

But if I try this, I get a SystemNullReferenceExeption. Does sombody know why?

Thx,
Cyberdot
 
Reply With Quote
 
 
 
 
Russ Gray
Guest
Posts: n/a
 
      24th Sep 2004
Cyberdot wrote:

> Hello,
> I've got a little problem. I want to draw a string to my form with the
> follówing code:
>
> Public Sub titleSetTime(ByVal newTime As String)
> Dim g As System.Drawing.Graphics
> g.DrawString(newTime, New System.Drawing.Font("Microsoft Sans
> Serif", 10, System.Drawing.FontStyle.Regular), New
> SolidBrush(SystemColors.ControlText), 200, 2)
> End Sub
>
> But if I try this, I get a SystemNullReferenceExeption. Does sombody know why?


You haven't actually created a Graphics object, you've just declared
one. I suggest you override the OnPaint method of your form, and use the
Graphics object provided to you in the PaintEventArgs parameter. Check
the MSDN documentation for details.

--
Remove SHOES to reply.
 
Reply With Quote
 
Peter Foot [MVP]
Guest
Posts: n/a
 
      24th Sep 2004
Because you haven't instantiated g, just created a variable to hold a
Graphics object. You can use

g = Me.CreateGraphics()

To get the Graphics instance for your form, which you will then be able to
draw with. You would probably be best to add this drawing code into your
OnPaint override for the form, then invalidate the form when the value
changes.

Peter

--
Peter Foot
Windows Embedded MVP
www.inthehand.com | www.opennetcf.org

Do have an opinion on the effectiveness of Microsoft Windows Mobile and
Embedded newsgroups? Let us know!
https://www.windowsembeddedeval.com/...ity/newsgroups

"Cyberdot" <(E-Mail Removed)> wrote in message
news:44F9F156-0629-4DA3-B0D7-(E-Mail Removed)...
> Hello,
> I've got a little problem. I want to draw a string to my form with the
> follówing code:
>
> Public Sub titleSetTime(ByVal newTime As String)
> Dim g As System.Drawing.Graphics
> g.DrawString(newTime, New System.Drawing.Font("Microsoft Sans
> Serif", 10, System.Drawing.FontStyle.Regular), New
> SolidBrush(SystemColors.ControlText), 200, 2)
> End Sub
>
> But if I try this, I get a SystemNullReferenceExeption. Does sombody know
> why?
>
> Thx,
> Cyberdot



 
Reply With Quote
 
=?Utf-8?B?Q3liZXJkb3Q=?=
Guest
Posts: n/a
 
      24th Sep 2004
Yeah, I tried it and it works. But now I have another problem. I have a
picturebox on the same position where I want to draw the string, and it looks
like the picture box is in front of the string I've drawn. Is there a method
to get the graphics handler for a picturebox?

Thx,
Cyberdot

"Peter Foot [MVP]" wrote:

> Because you haven't instantiated g, just created a variable to hold a
> Graphics object. You can use
>
> g = Me.CreateGraphics()
>
> To get the Graphics instance for your form, which you will then be able to
> draw with. You would probably be best to add this drawing code into your
> OnPaint override for the form, then invalidate the form when the value
> changes.
>
> Peter
>
> --
> Peter Foot
> Windows Embedded MVP
> www.inthehand.com | www.opennetcf.org
>
> Do have an opinion on the effectiveness of Microsoft Windows Mobile and
> Embedded newsgroups? Let us know!
> https://www.windowsembeddedeval.com/...ity/newsgroups
>
> "Cyberdot" <(E-Mail Removed)> wrote in message
> news:44F9F156-0629-4DA3-B0D7-(E-Mail Removed)...
> > Hello,
> > I've got a little problem. I want to draw a string to my form with the
> > follówing code:
> >
> > Public Sub titleSetTime(ByVal newTime As String)
> > Dim g As System.Drawing.Graphics
> > g.DrawString(newTime, New System.Drawing.Font("Microsoft Sans
> > Serif", 10, System.Drawing.FontStyle.Regular), New
> > SolidBrush(SystemColors.ControlText), 200, 2)
> > End Sub
> >
> > But if I try this, I get a SystemNullReferenceExeption. Does sombody know
> > why?
> >
> > Thx,
> > Cyberdot

>
>
>

 
Reply With Quote
 
Peter Foot [MVP]
Guest
Posts: n/a
 
      24th Sep 2004
No, why don't you ditch the picturebox and do all the painting in the
OnPaint method of your form, get the graphics object you are passed, draw
the image then the text

Peter

--
Peter Foot
Windows Embedded MVP
www.inthehand.com | www.opennetcf.org

Do have an opinion on the effectiveness of Microsoft Windows Mobile and
Embedded newsgroups? Let us know!
https://www.windowsembeddedeval.com/...ity/newsgroups

"Cyberdot" <(E-Mail Removed)> wrote in message
news5B6DEA5-B5D2-4B89-991C-(E-Mail Removed)...
> Yeah, I tried it and it works. But now I have another problem. I have a
> picturebox on the same position where I want to draw the string, and it
> looks
> like the picture box is in front of the string I've drawn. Is there a
> method
> to get the graphics handler for a picturebox?
>
> Thx,
> Cyberdot
>
> "Peter Foot [MVP]" wrote:
>
>> Because you haven't instantiated g, just created a variable to hold a
>> Graphics object. You can use
>>
>> g = Me.CreateGraphics()
>>
>> To get the Graphics instance for your form, which you will then be able
>> to
>> draw with. You would probably be best to add this drawing code into your
>> OnPaint override for the form, then invalidate the form when the value
>> changes.
>>
>> Peter
>>
>> --
>> Peter Foot
>> Windows Embedded MVP
>> www.inthehand.com | www.opennetcf.org
>>
>> Do have an opinion on the effectiveness of Microsoft Windows Mobile and
>> Embedded newsgroups? Let us know!
>> https://www.windowsembeddedeval.com/...ity/newsgroups
>>
>> "Cyberdot" <(E-Mail Removed)> wrote in message
>> news:44F9F156-0629-4DA3-B0D7-(E-Mail Removed)...
>> > Hello,
>> > I've got a little problem. I want to draw a string to my form with the
>> > follówing code:
>> >
>> > Public Sub titleSetTime(ByVal newTime As String)
>> > Dim g As System.Drawing.Graphics
>> > g.DrawString(newTime, New System.Drawing.Font("Microsoft Sans
>> > Serif", 10, System.Drawing.FontStyle.Regular), New
>> > SolidBrush(SystemColors.ControlText), 200, 2)
>> > End Sub
>> >
>> > But if I try this, I get a SystemNullReferenceExeption. Does sombody
>> > know
>> > why?
>> >
>> > Thx,
>> > Cyberdot

>>
>>
>>



 
Reply With Quote
 
Steven Licciardi
Guest
Posts: n/a
 
      24th Sep 2004
If all you want to do is draw in teh picturebox then :

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim g As Graphics = e.Graphics
g.DrawString("TEST", New Font("Arial", 12, FontStyle.Bold), New
SolidBrush(Color.Black), 1, 1)
End Sub

will do it

Steven

"Cyberdot" <(E-Mail Removed)> wrote in message
news5B6DEA5-B5D2-4B89-991C-(E-Mail Removed)...
> Yeah, I tried it and it works. But now I have another problem. I have a
> picturebox on the same position where I want to draw the string, and it
> looks
> like the picture box is in front of the string I've drawn. Is there a
> method
> to get the graphics handler for a picturebox?
>
> Thx,
> Cyberdot
>
> "Peter Foot [MVP]" wrote:
>
>> Because you haven't instantiated g, just created a variable to hold a
>> Graphics object. You can use
>>
>> g = Me.CreateGraphics()
>>
>> To get the Graphics instance for your form, which you will then be able
>> to
>> draw with. You would probably be best to add this drawing code into your
>> OnPaint override for the form, then invalidate the form when the value
>> changes.
>>
>> Peter
>>
>> --
>> Peter Foot
>> Windows Embedded MVP
>> www.inthehand.com | www.opennetcf.org
>>
>> Do have an opinion on the effectiveness of Microsoft Windows Mobile and
>> Embedded newsgroups? Let us know!
>> https://www.windowsembeddedeval.com/...ity/newsgroups
>>
>> "Cyberdot" <(E-Mail Removed)> wrote in message
>> news:44F9F156-0629-4DA3-B0D7-(E-Mail Removed)...
>> > Hello,
>> > I've got a little problem. I want to draw a string to my form with the
>> > follówing code:
>> >
>> > Public Sub titleSetTime(ByVal newTime As String)
>> > Dim g As System.Drawing.Graphics
>> > g.DrawString(newTime, New System.Drawing.Font("Microsoft Sans
>> > Serif", 10, System.Drawing.FontStyle.Regular), New
>> > SolidBrush(SystemColors.ControlText), 200, 2)
>> > End Sub
>> >
>> > But if I try this, I get a SystemNullReferenceExeption. Does sombody
>> > know
>> > why?
>> >
>> > Thx,
>> > Cyberdot

>>
>>
>>



 
Reply With Quote
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      24th Sep 2004
It is also worth mentioning that creating a new font to draw a string every
time is a bad practice - create the font once and reuse it. Same applies to
brushes and other GDI objects

--
Alex Feinman
---
Visit http://www.opennetcf.org
"Cyberdot" <(E-Mail Removed)> wrote in message
news:44F9F156-0629-4DA3-B0D7-(E-Mail Removed)...
> Hello,
> I've got a little problem. I want to draw a string to my form with the
> follówing code:
>
> Public Sub titleSetTime(ByVal newTime As String)
> Dim g As System.Drawing.Graphics
> g.DrawString(newTime, New System.Drawing.Font("Microsoft Sans
> Serif", 10, System.Drawing.FontStyle.Regular), New
> SolidBrush(SystemColors.ControlText), 200, 2)
> End Sub
>
> But if I try this, I get a SystemNullReferenceExeption. Does sombody know
> why?
>
> Thx,
> Cyberdot



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String drawing gives me headache... advise very welcome Fre Microsoft C# .NET 5 6th Mar 2008 08:17 PM
System.Drawing.Color to String shapper Microsoft ASP .NET 2 1st Oct 2007 03:07 PM
Drawing an image at the end of a string rpatel4@gmail.com Microsoft Dot NET Compact Framework 3 23rd Sep 2006 04:41 PM
Drawing vertical string Adriano Microsoft VB .NET 3 23rd Jun 2006 01:29 PM
Drawing String Vertically =?Utf-8?B?TmlsZXNoIFJhZGU=?= Microsoft Dot NET Framework Forms 3 9th Mar 2004 10:56 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:19 AM.