Windows Develop Bookmark and Share   
 index > Windows Forms General > Problems with a form showing mathematical plots
 

Problems with a form showing mathematical plots

Hello everyone!

I'm creating a program (in VC++ Express 2008) that plots a curve in a form and dynamically refreshes it whenever a parameter in a HScrollBar is changed. In order to visualize the graph, I use a Panel object and then Igeta handle to its graphics using this->panel1->CreateGraphics(); (this is referred to the form itself). But I have two problems:

1) if I enlarge the window, the panel is not enlarged as well

2) if I move another window on the gui, the plot is deleted and if I remove the window on it, no plot is shown unless I move again the scrollbar, and then a new plot is created (using the method DrawLine of the Graphics class. The same happens if I minimize the gui

How can I solve these problems?

Thanks!

Claudio

Ares982  Friday, March 07, 2008 12:36 PM

Hi Ares982,

You can set your Panel.Dock property to DockStyle.Fill, so that your panel automatically fills your main form when the main form is resizing.

In order to solve your second problem, you need to create an event handler for Panel.Paint() event and put your painting logic there. So that whenever another Form covers your application and is moved away later, the Panel can be redrawn correctly. Besides, invoke Panel.Invalidate() explicitly to redraw your panel when you scroll your horizontal scroll bar.

Code Snippet

partial class Form1 : Form

private Panel canvas;

private HScrollBar hScrollBar1;

private void Form1_Load(object sender, EventArgs e)

{

canvas = new Panel();

canvas.Dock = DockStyle.Fill;

canvas.BorderStyle = BorderStyle.Fixed3D;

this.Controls.Add(canvas);

canvas.Paint += new PaintEventHandler(canvas_Paint);

this.Paint += new PaintEventHandler(Form1_Paint);

new HScrollBar();

hScrollBar1.Parent = this;

hScrollBar1.Dock = DockStyle.Bottom;

.Controls.Add(hScrollBar1);

hScrollBar1.Scroll += new ScrollEventHandler(hScrollBar1_Scroll);

}

void hScrollBar1_Scroll(object sender, ScrollEventArgs e)

{

canvas.Invalidate();

}

void canvas_Paint(object sender, PaintEventArgs e)

{

Graphics g = canvas.CreateGraphics();

g.DrawLine(Pens.Black,

new Point(0, 0),

new Point(canvas.Width, canvas.Height));

g.DrawLine(Pens.Black,

new Point(canvas.Width, 0),

new Point(0, canvas.Height));

}

void Form1_Paint(object sender, PaintEventArgs e)

{

canvas.Invalidate();

}

private void Form1_ResizeEnd(object sender, EventArgs e)

{

canvas.Invalidate();

}

Best regards,

Jacob

Jacob Sui - MSFT  Wednesday, March 12, 2008 4:57 AM

Hi Ares982,

You can set your Panel.Dock property to DockStyle.Fill, so that your panel automatically fills your main form when the main form is resizing.

In order to solve your second problem, you need to create an event handler for Panel.Paint() event and put your painting logic there. So that whenever another Form covers your application and is moved away later, the Panel can be redrawn correctly. Besides, invoke Panel.Invalidate() explicitly to redraw your panel when you scroll your horizontal scroll bar.

Code Snippet

partial class Form1 : Form

private Panel canvas;

private HScrollBar hScrollBar1;

private void Form1_Load(object sender, EventArgs e)

{

canvas = new Panel();

canvas.Dock = DockStyle.Fill;

canvas.BorderStyle = BorderStyle.Fixed3D;

this.Controls.Add(canvas);

canvas.Paint += new PaintEventHandler(canvas_Paint);

this.Paint += new PaintEventHandler(Form1_Paint);

new HScrollBar();

hScrollBar1.Parent = this;

hScrollBar1.Dock = DockStyle.Bottom;

.Controls.Add(hScrollBar1);

hScrollBar1.Scroll += new ScrollEventHandler(hScrollBar1_Scroll);

}

void hScrollBar1_Scroll(object sender, ScrollEventArgs e)

{

canvas.Invalidate();

}

void canvas_Paint(object sender, PaintEventArgs e)

{

Graphics g = canvas.CreateGraphics();

g.DrawLine(Pens.Black,

new Point(0, 0),

new Point(canvas.Width, canvas.Height));

g.DrawLine(Pens.Black,

new Point(canvas.Width, 0),

new Point(0, canvas.Height));

}

void Form1_Paint(object sender, PaintEventArgs e)

{

canvas.Invalidate();

}

private void Form1_ResizeEnd(object sender, EventArgs e)

{

canvas.Invalidate();

}

Best regards,

Jacob

Jacob Sui - MSFT  Wednesday, March 12, 2008 4:57 AM

Thank you very much, Jacob, and most of all thank you for the code snippet! :-)

Regards,

Claudio

Ares982  Wednesday, March 12, 2008 1:29 PM

You can use google to search for other answers

Custom Search

More Threads

• DivideByZeroException
• Passing object from one form to another form , C# winform application.
• Flyout Menu
• Menu makes application non-responsive
• AutoComplete TextBox
• need to determine desktop top/left-bottom/right co-ordinates
• Delay when load crystal report
• Code for painting
• Simple C# Dice Golf
• datagridviewcomboboxcolumn problem