Solved

When does the DrawString method fire?

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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