Solved

When does the DrawString method fire?

Posted on 2007-03-27
4
281 Views
Last Modified: 2010-04-23
I am trying to use the DrawString method to display data in a label. Why will it onlt display after I clock ok to my messagebox ? I want it to display on formload. Please check out my code example. I am using VB.NET 2003. Thank you.

 '  A font for the standard size text:
        Dim fnt As Font = New Font("Microsoft Sans Serif", 8, FontStyle.Bold)

        '  A Brush to write the text:
        Dim b As Brush = New SolidBrush(Color.Black)
        '  Graphics objects
        Dim lgNH3 As Graphics = lblNH3.CreateGraphics

        MessageBox.Show("HUH?")
        Dim strNH As String = "What the..?"
        lgNH3.DrawString(strNH, fnt, b, 1, 1)
0
Comment
Question by:IT_Steve
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18803791
Don't use CreateGraphics().

Instead, use the Graphics supplied in the "e" paramter of the Paint() event for whatever control you want to draw on...

For example, with a PictureBox:

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Dim fnt As Font = New Font("Microsoft Sans Serif", 8, FontStyle.Bold)
        Dim b As Brush = New SolidBrush(Color.Black)
        Dim strNH As String = "What the..?"
        e.Graphics.DrawString(strNH, fnt, b, 1, 1)
        b.Dispose()
        fnt.Dispose()
    End Sub
0
 

Author Comment

by:IT_Steve
ID: 18804040
Thank you Idle_Mind . Your solution works for me. Why did my original way behave as it did?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18804176
Because it is rare when CreateGraphics() should actually be used with a Control...since when the control refreshes itself, whatever you drew gets erased.

CreateGraphics() does not give you a persistent drawing surface.
0
 

Author Comment

by:IT_Steve
ID: 18807968
Thank you for the explanation. The points are yours!
Steve
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question