• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1187
  • Last Modified:

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

0
si2030
Asked:
si2030
  • 7
  • 5
1 Solution
 
abelCommented:
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

0
 
abelCommented:
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

0
 
si2030Author Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
abelCommented:
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
 
abelCommented:
> 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
 
si2030Author Commented:
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
 
abelCommented:
What about something like:

If radioButton1.Checked Then    DrawSquareWave(e.Graphics)Else    DrawSineWave(e.Graphics)End If
0
 
si2030Author Commented:
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
 
si2030Author Commented:
Worked perfectly.
0
 
abelCommented:
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
 
si2030Author Commented:
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
 
abelCommented:
Nice layout! Looks real smooth :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now