NotSupportedException when CreateGraphics

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to create a picturebox or panel that I can draw a graph on it. I did it by creating a class which inherits Panel (I tried PictureBox as well) and create a graphics in it by

Private g as Graphics = Me.CreateGraphic

Although its syntax is correct, it give me System.NotSupportedException when I run it. How can I soft the problem?
 
"Only the Control and Form classes support Control.CreateGraphics()."
http://wiki.opennetcf.org/ow.asp?CompactFrameworkFAQ/CreateGraphicsOnTextBox

You should try to use the OnPaint method override in your custom control.

--
Tim Wilson
..Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}
Kempton said:
I would like to create a picturebox or panel that I can draw a graph on
it. I did it by creating a class which inherits Panel (I tried PictureBox as
well) and create a graphics in it by:
Private g as Graphics = Me.CreateGraphics

Although its syntax is correct, it give me System.NotSupportedException
when I run it. How can I soft the problem?
 
Thank you for your help? Can you tell me more details? For example, how can I draw a rectangle with the OnPaint method?
 
Code below.

Public Class MyPanel
Inherits System.Windows.Forms.Panel

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim p As New Pen(Color.Red)
e.Graphics.DrawRectangle(p, 10, 10, Me.ClientRectangle.Width - 20, Me.ClientRectangle.Height - 20)
p.Dispose()
End Sub
End Class
 
Back
Top