Drawing OnPaint Custom Control?
Posted on 2004-08-09
I am so confused, so if this is confusing, join the club? ;)
I have a VB.NET Windows application, using Visual Studio.NET (2003)
It hassome TabPages on it, and most of it works fine.
On one of the TabPages is wish to draw, optionally 1 to 4 graphs (based upon data crunched process by the app)
So I set up a Custom Control, which I thought would contain the info/data/controls/lines, etc. necessary for the graph(s).
In a sense so far so good. I have very little experience w the System.Drawing.Graphics naemspace in VB.NET (or even VB).
Anyway, program will instantiate mycustomcontrol, and draw it on the TabPage . . . which is good.
I can also get it to draw a line (which would be/is the x-axis) . . . again no problem.
However, I need more than just one line, the y-axis, a bunch of labels, boxes/rectangles for the data (a histogram is the type, if that matters)
So, I thought I owuld go ahead and add one of my Labels, namely the one for the YAxis, which would have text = "Y AXIS DATA" or such, just to move on to another thing I need on the graph(s).
This is where the problem comes up.
If I run what I have so far, just drawing the x-axis, a line, OK fine.
If I have it attempt to draw the YAxisLabel, it goes thru the OnPaint event handler over & over until it bobms out yielding an error message "System.ComponentModel.Win32Exception occurred in system.windows.forms.dll"
I kinda get why it is going thru the OnPaint handler over and over, but have no idea as to how to stop it.
In psuedo code form here is my code:
Public Class MyMainForm
'bunch of code
Private Sub ButtonDrawGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDrawGraph.Click
'this handles the user clicking a button on the MainForm, actually one of the TabPages, which would hopefully draw the Graph
'requested, except it gets stuck in a loop, drawing over & over ;)
If Not (MyControl Is Nothing) Then
'if the graph already exists, clear it, dispose of it, so as to be able to draw the next one/new one
MyControl = New MyControlClass(some parameters)
End Private Sub
End Class 'MyMainForm
Protected Overides Sub OnPaint (ByVal e As PaintEventArgs)
g = e.Graphics
Private Sub DrawXAxis()
Dim BlackPen As New Pen(Color.Black, 1)
Dim startPoint As New Point(5, Me.Height - 20)
Dim endPoint As New Point(Me.Width - 5, Me.Height - 20)
g.DrawLine(BlackPen, startPoint, endPoint)
Private Sub AddYAxisLabel()
yaxislbl.Top = margin
yaxislbl.Left = margin
yaxislbl.Text = "Frequency"
yaxislbl.Height = 16
As I said, if I call the DrawXAxis() alone, it works fine.
If I call the AddYAxisLabel, before or after DrawXAxis, w/w/o SuspenedLayout, it goes into an infinite loop and bombs out.
I really do not get a lot about the System.Drawing namespace, and there may be a much better way to do what I am doing.
Any help is greatly appreciated.