Link to home
Start Free TrialLog in
Avatar of si2030
si2030

asked on

Automatically draw in picturebox upon load...

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

Open in new window

Avatar of abel
abel
Flag of Netherlands image

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

Open in new window

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);
}

Open in new window

Avatar of si2030
si2030

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> 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.
Avatar of si2030

ASKER

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

What about something like:

If radioButton1.Checked Then    DrawSquareWave(e.Graphics)Else    DrawSineWave(e.Graphics)End If
Avatar of si2030

ASKER

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...
Avatar of si2030

ASKER

Worked perfectly.
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.
Avatar of si2030

ASKER

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
Nice layout! Looks real smooth :)