We help IT Professionals succeed at work.
Get Started

Drawing OnPaint Custom Control?

tlfeet
tlfeet asked
on
1,038 Views
Last Modified: 2010-05-18
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
           MyControl.Dispose
           'if the graph already exists, clear it, dispose of it, so as to be able to draw the next one/new one
       End If

       MyControl = New MyControlClass(some parameters)

       Me.TabPage5.Controls.Add(MyControl)
    End Private Sub
End Class    'MyMainForm

Imports System.Drawing.Graphics
Public MyControlClass
     Inherits System.Windows.Forms.UserControl

     Protected Overides Sub OnPaint (ByVal e As PaintEventArgs)
        g = e.Graphics
 
       DrawXAxis()
       AddYAxisLabel()
     End Sub

     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)
    End Sub

    Private Sub AddYAxisLabel()
        Me.Controls.Add(yaxislbl)
        yaxislbl.Top = margin
        yaxislbl.Left = margin
        yaxislbl.Text = "Frequency"
        yaxislbl.Height = 16
    End Sub

End Class

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.

Thanks,

Mike.
Comment
Watch Question
Software Architect
Top Expert 2008
Commented:
This problem has been solved!
Unlock 2 Answers and 10 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE