Solved

When does the DrawString method fire?

Posted on 2007-03-27
4
280 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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