Solved

# Automatically draw in picturebox upon load...

Posted on 2009-04-22
1,141 Views
Hi Experts,

I have a picturebox that I use to draw a sine wave in.  I have the sinewave drawing in a subroutine (see below).

My problem is, I want to draw this sinewave when I load the form. I have tried to call this routine in the form load handle and it doesnt show. I then tried to set the radio button checked to true and have a checkedchanged handler call the subroutine. Again its not displaying. Yet if start the app and then click on the radio button the picture box has the sine wave draw in it....

How to I get the form to draw the sine wave on load.. or in otherwords, automatically?

Kind Regards

Simon
``````Private Sub drawSineWave()

'I just draw a Sin graphics between 0-2p for example

Dim x0 As Single = 80.0F

Dim y0 As Single = 50.0F

'Assume the graphics width is 200pixels

'so there're 200 points

Dim points As PointF() = New PointF(199) {}

For j As Integer = 0 To 199

points(j) = New PointF()

points(j).X = x0 + j

points(j).Y = y0 - CSng((Math.Sin((2 * Math.PI * j) / 200) * (200 / (2 * Math.PI))))

Next

Using p As New Pen(Color.Blue)

p.EndCap = LineCap.ArrowAnchor

'Draw X-coordinate

pctWaveDisplay.CreateGraphics.DrawLine(p, x0, y0, x0 + 220, y0)

'Draw(Y - coordinate)

p.StartCap = LineCap.ArrowAnchor

pctWaveDisplay.CreateGraphics.DrawLine(p, x0, y0 + 47, x0, y0 - 47)

p.DashStyle = DashStyle.Dash

p.Color = Color.Red

p.StartCap = LineCap.NoAnchor

pctWaveDisplay.CreateGraphics.DrawLine(p, x0, y0 - 32, x0 + 50, y0 - 32)

End Using

pctWaveDisplay.CreateGraphics.DrawString("0", SystemFonts.DefaultFont, Brushes.Blue, x0, y0)

pctWaveDisplay.CreateGraphics.DrawString("p", SystemFonts.DefaultFont, Brushes.Blue, x0 + 95, y0)

pctWaveDisplay.CreateGraphics.DrawString("2p", SystemFonts.DefaultFont, Brushes.Blue, x0 + 200, y0)

pctWaveDisplay.CreateGraphics.DrawString("VOLTAGE", SystemFonts.DefaultFont, Brushes.Blue, x0 - 60, y0 - 43)

pctWaveDisplay.CreateGraphics.SmoothingMode = SmoothingMode.AntiAlias

pctWaveDisplay.CreateGraphics.DrawLines(Pens.Red, points)

End Sub
``````
0
Question by:si2030

LVL 39

Expert Comment

Try the OnShown handler, which is fired right after the first time the form is shown:

``````Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown

drawSineWave()

End Sub
``````
0

LVL 39

Expert Comment

Hmm, that won't work. I took your code and manipulated it a bit. Here's the C# version (sorry, didn't have time to try it in VB). The main point is: use the pctWaveDisplay OnPaint event, AND change the CreateGraphics into the e.Graphics from the event:

``````private void pctWaveDisplay_Paint(object sender, PaintEventArgs e)

{

drawSineWave(e.Graphics);

}

private void drawSineWave(Graphics graphics)

{

//I just draw a Sin graphics between 0-2p for example

float x0 = 80f;

float y0 = 50f;

//Assume the graphics width is 200pixels

//so there're 200 points

PointF[] points = new PointF[200];

for (int j = 0; j <= 199; j++)

{

points[j] = new PointF();

points[j].X = x0 + j;

points[j].Y = y0 - (float)(Math.Sin((2 * Math.PI * j) / 200) * (200 / (2 * Math.PI)));

}

using (Pen p = new Pen(Color.Blue))

{

p.EndCap = LineCap.ArrowAnchor;

//Draw X-coordinate

graphics.DrawLine(p, x0, y0, x0 + 220, y0);

//Draw(Y - coordinate)

p.StartCap = LineCap.ArrowAnchor;

graphics.DrawLine(p, x0, y0 + 47, x0, y0 - 47);

p.DashStyle = DashStyle.Dash;

p.Color = Color.Red;

p.StartCap = LineCap.NoAnchor;

graphics.DrawLine(p, x0, y0 - 32, x0 + 50, y0 - 32);

}

graphics.DrawString("0", SystemFonts.DefaultFont, Brushes.Blue, x0, y0);

graphics.DrawString("p", SystemFonts.DefaultFont, Brushes.Blue, x0 + 95, y0);

graphics.DrawString("2p", SystemFonts.DefaultFont, Brushes.Blue, x0 + 200, y0);

graphics.DrawString("VOLTAGE", SystemFonts.DefaultFont, Brushes.Blue, x0 - 60, y0 - 43);

graphics.SmoothingMode = SmoothingMode.AntiAlias;

graphics.DrawLines(Pens.Red, points);

}
``````
0

Author Comment

I tried that abel however it displayed the graph very briefly and then it disapeared.

The picturebox is in a groupbox. Its like its being drawn and then wiped as the grpbox displays.
0

LVL 39

Accepted Solution

Now you do not even have to put anything in the OnLoad event of the form. And, when you move your form around (over the edge of the screen), the sinus will not disappear anymore.

In VB, just do the following:

1. remove every pctWaveDisplay.CreateGraphics and replace it with "picGraphics"
2. add a parameter to your function that you call "picGraphics As Graphics"
3. add the paint event (select it from the properties or the dropdown) for the pctWaveDisplay
4. inside that event, add a drawSineWave(e.Graphics)
that should do it.
0

LVL 39

Expert Comment

> I tried that abel however it displayed the graph very briefly and then it disapeared.
I assume that was on the first post of me? See the second and, most importantly, the last, on how you can tackle this.
0

Author Comment

Hi abel,

Sorry I didnt catch the second post. Works perfectly. I have one other question though.

I have two radio buttons - one for a sinewave and one for a squarewave (almost identical to the sinewave sub).

I now have the display defaulting to a sinewave and it displays automatically.

If the user now clicks on square wave how do I call the squarewave function given that they would both have a parameter "ByVal graphics As Graphics" now.

I was calling it simply as "drawsinewave()" however now the sub needs to be called from within a handle that has the above param...

0

LVL 39

Expert Comment

If radioButton1.Checked Then    DrawSquareWave(e.Graphics)Else    DrawSineWave(e.Graphics)End If
0

Author Comment

This is what I would have in the paint event. However I want to be able to redraw the sine/square wave by calling the subroutine.  I cant put "e.graphics" in as the parameter.

So if a user clicks on a new radio button how to I repaint the picbox and thus call the above " if then" sollution...
0

Author Closing Comment

Worked perfectly.
0

LVL 39

Expert Comment

Thanks for the grade and the pts. About that last question (which I only see now, sorry for the late follow-up), have you managed? In general, I would put that (the if-statement) in the Paint event, and use pctWaveDisplay.Invalidate(..) with a Rect the size of the picture control, to force a Paint event on the radioButton1_Click event.
0

Author Comment

Hi Abel, it should be me that appologies... I should have signed off on this ages ago. I did get it working and its a nice little personal transformer design application that shows a squarewave or sine wave depending on what type of signal you are putting through the tranformer.

http://www.screencast.com/users/si2030/folders/Jing/media/d49329ca-6a35-41f1-bf84-443333dabba4
0

LVL 39

Expert Comment

Nice layout! Looks real smooth :)
0

## Featured Post

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…