Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Drawing OnPaint Custom Control?

Posted on 2004-08-09
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
           '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)

    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
     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()
        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.


Question by:tlfeet
  • 5
  • 3
  • 2
LVL 55

Accepted Solution

Jaime Olivares earned 300 total points
ID: 11755972
OnPaint() calls DrawXAxis() and AddYAxisLabel()
Inside AddYAxisLabel you call:


I think there is a problem. It is not recommended to modify an object inside a painting function, because it can ocurrs in unpredictable moments and quantity.

LVL 15

Assisted Solution

SRigney earned 200 total points
ID: 11756036
You are actually adding a label control to your control, which may be what the problem is.  Get the Graphics handle and try painting the text instead of using a label control.

Author Comment

ID: 11756133
Hi SRigney,

Humm, I understand what you are saying, and I think I have "fixed" the problem.

Yes, I was adding a Control from within the OnPaint event handler.

I moved the addition of the control to outside the OnPaint event handler.  And it works fine.

I could in fact, except for one small detail, ;-) put this control in at design time (in fact I just might do that).
The one small exception is that (possibly) it's location and size, would vary from time to time, so figured I might as well make the whole thing dynamic.

So, in short, have moved the adding control code to outside of OnPaint handler, and seems to work fine, OK.

This is related I guess, I need to, well actually would prefer to make the orientation of the text w/i the control to be vertical as opposed to default/standard horizontal.

I assume I would do that with the Graphics features (if it is possible)?
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 11756176
Hi Jaime,

I am sorry I missed your response.

Essentially, yes, as you state, as as did SRigney, having the add control functiion w/i the the OnPaint handler is/was causing the problem.

Sorry, I missed your response.

Which leads to: what do you guys think is fair in awarding/splitting the points?

Jaime was 6 minutes faster, both anwered the q, solved the problem.

Thanks, to both.
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11756304
Well, it is 500 pts question, so could be enough points for all, although I deserve some extra points for typing faster ;-)
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11756324
btw. There is an specific VB.NET forum, you can post your VB.net questions there, because vb.net is totally different from Visual Basic (just similar names)
LVL 15

Expert Comment

ID: 11756352
I think there's enough point for everyone, but my goal is to make sure you have something working, the points are an added bonus.

This is a sub chunk of code stolen from a C# Verticle label control, but it should get you close enough to what you need.

Graphics g = e.Graphics;

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment =
stringFormat.Trimming = StringTrimming.None;
stringFormat.FormatFlags =

Brush textBrush = new

Matrix storedState = g.Transform;

g.TranslateTransform(-ClientRectangle.Width, -ClientRectangle.Height);

      "Y label",

g.Transform = storedState;

Author Comment

ID: 11756357
Hi Jaime,

Re: "btw. There is an specific VB.NET forum, you can post your VB.net questions there, because vb.net is totally different from Visual Basic (just similar names)"

I thought that is/was where I was done at.  Now that you point it out, I am in fact in VB - sorry for any confusion.
I did intend to post in VB.NET

Author Comment

ID: 11756382

Author Comment

ID: 11756791

He's back.

Accepted answer too soon.

In  trying to incorporate Vertical Text, back to the infinite loop thing.

BTW, found code to draw vertical text in VB.NET help (SRigney's post, led to more searches, which turned up exactly what I was looking for, but which did not appear in my first searches . . . but that is a digression)

The code for drawing Vertical Text, per help is:

   Public Sub DrawVerticalString()
        Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
        Dim drawString As String = "Sample Text"
        Dim drawFont As New System.Drawing.Font("Arial", 16)
        Dim drawBrush As New _
        Dim x As Single = 150.0
        Dim y As Single = 50.0
        Dim drawFormat As New System.Drawing.StringFormat
        drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
        g.DrawString(drawString, drawFont, drawBrush, _
        x, y, drawFormat)
    End Sub

Problem, is, that if I place reference/call to this sub outside OnPaint, it does not draw.

If I do place it within the OnPaint, it goes back into an infnite loop, near as I can tell, to draw the X-Axis, then DrawVertical text, then this cause OnPaint to call X-Axis & DrawVertical, and so it goes, ad-infinitum

...again, I have no idea what I am doing, but it seems one can only draw one thing, as subsequent drawings of things/objects, calls the OnPaint agrument to be called yet again, which then causes the whole loop, set of drawings to repeat.

How, do I draw more than one thing/object, w/o getting into an infinite loop where the drawing of the second, subsequent objects cause the OnPaint handler to be called recursively?


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question