VB .NET does not have an AutoRedraw property for forms

This is a problem if you wish to display text and graphics directly on a form. This brief project should help to provide you with AutoRedraw capability.

The theory is as follows

Some explanation

If you try to print directly to the form first and then save each printing from the form to the bitmap you will run into problems since the system may not display your graphics in time to be copied. You could try the 'System.Windows.Forms.Application.DoEvents()' statement, but an intervening Paint event might spoil everything.

Here is the code

  1. '=======================================================
  2. ' AutoRedraw in VB .NET
  3. ' by: Rick Meyer December 2002
  4. ' http://trixar.com/~makai/index.htm
  5. '=======================================================
  6. ' Purpose and Explanation:
  7. '
  8. ' This very brief project provides a replacement for
  9. ' the VB6 Form.AutoRedraw missing in VB .NET
  10. '=======================================================
  11. ' Instructions and Operation:
  12. '
  13. ' 1. Start a new VB.NET Windows Application
  14. ' 2. On Form1 put a Button named Button1
  15. ' 3. Copy the following code into Form1 class
  16. ' 4. Press F5 to run
  17. ' 5. Minimize and restore to verify the repaint
  18. ' 6. Click Button1
  19. ' 7. Minimize and restore to verify the repaint
  20. '=======================================================
  21. 'Bitmap holds picture of the form
  22. Private b1 As Bitmap
  23. 'Graphics object (printing buffer)
  24. Private g1 As Graphics
  25. Private Sub Form1_Activated(ByVal sender As Object, _ByVal e As System.EventArgs) Handles MyBase.Activated
  26. Static done As Boolean = False
  27. If Not done Then
  28. 'Size and describe the form
  29. Size = New Size(200, 150)
  30. Text = "AutoRedraw"
  31. 'Size, Locate, & Describe the button
  32. With Button1
  33. .Text = "Print Something"
  34. .Size = New Size(100, 20)
  35. .Location = New Point(30, 80)
  36. End With
  37. 'AUTOREDRAW INITIALIZATION
  38. 'Create the initial bitmap from Form
  39. b1 = New Bitmap(Width, Height, Me.CreateGraphics())
  40. 'Create the Graphics Object buffer
  41. ' which ties the bitmap to it so that
  42. ' when you draw something on the object
  43. ' the bitmap is updated
  44. g1 = Graphics.FromImage(b1)
  45. 'Prevent reentry to initialization
  46. done = True
  47. 'END AUTOREDRAW INITIALIZATION
  48. 'Print beginning text
  49. FormPrint("Beginning Text", 25, 10)
  50. End If
  51. End Sub
  52. 'Print something
  53. Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click
  54. FormPrint("Something", 35, 46)
  55. End Sub
  56. Private Sub FormPrint(ByVal t$, ByVal x1%, ByVal y1%)
  57. 'Printing to the graphics buffer (not form)
  58. ' updates the associated bitmap b1
  59. g1.DrawString(t, New Font("Verdana", 12), _Brushes.Black, x1, y1)
  60. 'Copy the bitmap to the form
  61. Me.CreateGraphics.DrawImage(b1, 0, 0)
  62. End Sub
  63. Private Sub Form1_Paint( _ByVal sender As Object, _ByVal e As System.Windows.Forms.PaintEventArgs) _Handles MyBase.Paint
  64. 'Copy the bitmap to the form
  65. e.Graphics.DrawImage(b1, 0, 0)
  66. End Sub