Solved

When does the DrawString method fire?

Posted on 2007-03-27
4
274 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
  • 2
  • 2
4 Comments
 
LVL 85

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 85

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now