0

I wanted to create a basic usercontrol that has a buffered graphics. But something is not right, it stops at the line Using gr As Graphics = Graphics.FromImage(m_Buffer)

I even managed to crash VS2010. That is why I suspect that I introduced some errors. I copied most parts from example code, and I guess that at least the OnPaint is never called because it was not one of the Events that I could select in the Designer.

Thank you very much for the help!

Public Class UserControl1

Private m_Buffer As Bitmap

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

    e.Graphics.DrawImage(m_Buffer, Point.Empty)
    'MyBase.OnPaint(e)'what is this for?

End Sub
Public Sub PaintSomething()

    Using gr As Graphics = Graphics.FromImage(m_Buffer)
        gr.FillEllipse(New SolidBrush(Color.Blue), 0, 0, 100, 100)
    End Using

    Me.Invalidate()

End Sub
Public Sub New()

    InitializeComponent()

    Me.SetStyle(ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer, True)
    Me.UpdateStyles()

End Sub

Private Sub UserControl1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

    m_Buffer = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)

End Sub

End Class

tmighty
  • 10,734
  • 21
  • 104
  • 218
  • Maybe m_Buffer is empty? Where is PaintSomethign called? – UNeverNo Oct 31 '12 at 09:12
  • Yes, that is true. But I don't know why because I instantiate it at UserControl1_Resize. – tmighty Oct 31 '12 at 09:19
  • I guess that PaintSomething gets called before initialising it in UserControl1_Resize – UNeverNo Oct 31 '12 at 09:22
  • In my code, UserControl1_Resize is not called. Is the name of the sub ("UserControl1_Resize") wrong, or where else should I instantiate the m_Buffer? – tmighty Oct 31 '12 at 09:31
  • I'd advice to use a function and (re-)set it in UserControl1_Resize plus Form_Load. As far as I remember sizing should be finished there. – UNeverNo Oct 31 '12 at 09:45
  • You really need to read a book about Winforms programming. Focus on the chapter that talks about event handlers. And don't stop reading until "what is this for?" is crystal clear. – Hans Passant Oct 31 '12 at 09:49

0 Answers0