Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

Tags: drawstring

comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Today I had to draw a text on a panel that I had created at runtime. This is actually pretty easy but there are some pitfalls that can take a few minutes to figure out. This is all still in the good old windows forms, none of that WPF magic for me ;-).

So first you create a form.

And then in the load event of that form we create this.

  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         _Panel = New Panel
  3.         _Panel.Dock = DockStyle.Fill
  4.         _Panel.BackColor = Color.LightCyan
  5.         Dim _graphics As Graphics = _Panel.CreateGraphics
  6.         _graphics.DrawString("The brown fox thing.", New Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point, Nothing), New System.Drawing.SolidBrush(Color.Gray), Convert.ToInt32(Me.Width / 2), Convert.ToInt32(Me.Height / 2))
  7.         _Panel.Visible = True
  8.         Me.Controls.Add(_Panel)
  9.     End Sub

Result

Weird no text. Well, I can explain that. If you look very carefully and change your code to this:

  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         _Panel = New Panel
  3.         _Panel.Dock = DockStyle.Fill
  4.         _Panel.BackColor = Color.LightCyan
  5.         _Panel.Visible = True
  6.         Me.Controls.Add(_Panel)
  7. Dim _graphics As Graphics = _Panel.CreateGraphics
  8.         _graphics.DrawString("The brown fox thing.", New Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point, Nothing), New System.Drawing.SolidBrush(Color.Gray), Convert.ToInt32(Me.Width / 2), Convert.ToInt32(Me.Height / 2))
  9.     End Sub

Then you will see whether the text to be drawn quickly disappears or not ;-). What happens is that after you draw the text on the graphics object, the panel is redrawn. The paint event of this panel doesn’t know about the drawstring and doesn’t, therefore, draw it again.

The solution is to draw the string in the paint event of the panel.

Which looks like this:

  1. Private _Panel As Panel
  2.  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         _Panel = New Panel
  5.         _Panel.Dock = DockStyle.Fill
  6.         _Panel.BackColor = Color.LightCyan
  7.         _Panel.Visible = True
  8.         Me.Controls.Add(_Panel)
  9.         AddHandler _Panel.Paint, AddressOf DrawText
  10.     End Sub
  11.  
  12.     Private Sub DrawText(ByVal obj As Object, ByVal e As PaintEventArgs)
  13.         e.Graphics.DrawString("Rightclick to add or Delete an image.", New Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point, Nothing), New System.Drawing.SolidBrush(Color.Gray), Convert.ToInt32(Me.Width / 2), Convert.ToInt32(Me.Height / 2), string_format)
  14.     End Sub

And this gives this result.

The text isn’t all that beautifully placed but we can do something about that.

Change your code to this:

  1. Public Class Form1
  2.     Private _Panel As Panel
  3.  
  4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  5.         _Panel = New Panel
  6.         _Panel.Dock = DockStyle.Fill
  7.         _Panel.BackColor = Color.LightCyan
  8.         _Panel.Visible = True
  9.         Me.Controls.Add(_Panel)
  10.         AddHandler _Panel.Paint, AddressOf DrawText
  11.     End Sub
  12.  
  13.     Private Sub DrawText(ByVal obj As Object, ByVal e As PaintEventArgs)
  14.         Using string_format As New StringFormat()
  15.             string_format.Alignment = StringAlignment.Center
  16.             string_format.LineAlignment = StringAlignment.Center
  17.            e.Graphics.DrawString("Rightclick to add or Delete an image.", New Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point, Nothing), New System.Drawing.SolidBrush(Color.Gray), Convert.ToInt32(Me.ClientSize.Width / 2), Convert.ToInt32(Me.ClientSize.Height / 2), string_format)
  18.         End Using
  19.     End Sub
  20. End Class

This is the result:

Now the text is nicely centered without having to do to much tedious calculation. The stringformat does most of the hard work for us.

You can find more on this site.

Master .NET’s Text Tricks

About the Author

User bio imageChristiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
Social SitingsTwitterLinkedInHomePageLTD RSS Feed
2561 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback