DrawLine on panel on dynamic TabPage not working...help?

I

intrepid_dw

All

I am writing a C# WinForms application which is giving me some
problems.

The application consists of a form containing an empty Tab control to
which TabPages are added dynamically (at runtime). Each TabPage
contains a single Panel control. My intent is to draw a graph on that
Panel control dynamically, with the code capturing a point at each
update interval, then calling the Graphics.DrawLine method of the panel
to draw the line between the previous and "new" points.

At the moment, the application is written to add a single TabPage. The
form appears with the TabPage and its proper Panel control, but the
line does not appear. Every indication is that the code to draw the
line fires, but no line appears in the panel.

A debug-step through the code indicates each new point is computed
correctly; no exceptions are thrown, and the Pen used to draw the line
is of a contrasting color (Green against a white background). I've
tried invalidating the Panel, invalidating the parent TabControl,
refreshing the Panel, and a few other items, but nothing causes the
line to draw.

May I please ask for suggestions on what I am possibly overlooking? The
code that performs the line draw is as follows (plotHost is a reference
to a Control object used for the drawing surface); lastTranslatedPoint
is the "starting point" of the new line to be drawn, newPoint is the
"ending point." (The X and Y elements of lastTranslatedPoint are
initialized to -1 for the first pass because there is no "previous
point" from which to draw.) The variable xTick is simply the ticker for
the variable along the X axis.


public void AddNewValue(decimal newValue)
{
try
{
int newYPlotValue = Convert.ToInt32(
Decimal.Divide(newValue,maxPlotValue*plotHost.Height);
Point newPoint = new Point(xTick,newYPlotValue);
if (lastTranslatedPoint.X!=-1 && lastTranslatedPoint.Y!=-1)
{
plotHost.CreateGraphics().DrawLine(new Pen(new
SolidBrush(Color.Green),2),lastTranslatedPoint,newPoint);
plotHost.Parent.Invalidate();
}
xTick+=4;
lastTranslatedPoint = newPoint;
}
catch(Exception e)
{
Console.WriteLine("Help!");
}
}

Many thanks in advance,
-intrepid
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top