-1

I am new to C# and I am trying to use OOP and classes. I am trying to draw a simple sinewave and an axis line (X axis). I have gotten similar code to in the "main - Form1" but I cannot get it to draw in the form from within a class. It draws nothing! The code does compile.

What am I missing? What can I do better?

I call the class from a button click -

Drawclick(object sender, EventArgs e)
  {  DrawSine Sine1 = new DrawSine(950);
  }

Here is the class

class DrawSine:Form1
{
        private float sinex=0;//set up variables for sine
        private float countx = 0;
        private float siney = 0;
        private float sinex1=0;
        private float siney1=0;
        public float offset;
        public float scalex;
        public float scaley;

        public DrawSine(int widthG)
        {
            Graphics Graphsine = this.CreateGraphics();//declare sine
            Graphics Graphline = this.CreateGraphics();//declare axis
            Pen Graphpen = new Pen(Brushes.Black, 1.0F);
            Graphline.DrawLine(Graphpen, 0, 200, widthG, 200);//draw line 
                                                0,200 to end of form,200

            int WidthG = widthG; 
            do  //draw sine wave left to right
            {
                sinex += scalex * 6.28f / widthG;
                siney = (float)Math.Sin(sinex);
                Graphsine.DrawLine(Graphpen, sinex1, siney1 + offset, 
                                   countx, siney * 100 + offset);
                sinex1 = sinex;
                sinex1 = siney * 100;
                countx += 1;
            }
            while (countx <= widthG);

        }

}
  • 1
    Never use CreateGraphics, use the Paint event instead. Right now you are splattering pixels in the constructor, before the window is visible. No Sine1.Show() call either, so it never becomes visible. Lots of wrongness, an introductory book on Windows Forms is advisable, you'll be battling bugs for a while without one. – Hans Passant Dec 26 '17 at 10:17

1 Answers1

0

This may be drawing something on the form but you cannot see because soon after the Load event, there will be a PaintBackground event and a PaintEvent as the form is rendered.

These events will effectively erase everything on the form.

2 Remedies:

Encapsulate the above draw code in a method (Let's say DrawSine())

One, Override OnPaint event and write your code there. Your painting will remain intact on form resize or form redraw.

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    DrawSine(e, 950);
}

Two, use form Shown event

private void Form1_Shown(object sender, EventArgs e)
{
    DrawSine(null, 950);
}

Your original method:

public DrawSine(Graphics g, int widthG)
{
    Graphics Graphsine = g??this.CreateGraphics();//declare sine
    Graphics Graphline = g??this.CreateGraphics();//declare axis
    Pen Graphpen = new Pen(Brushes.Black, 1.0F);
    Graphline.DrawLine(Graphpen, 0, 200, widthG, 200);//draw line 
                                            //0,200 to end of form,200

    int WidthG = widthG; 
    do  //draw sine wave left to right
    {
       sinex += scalex * 6.28f / widthG;
       siney = (float)Math.Sin(sinex);
       Graphsine.DrawLine(Graphpen, sinex1, siney1 + offset, 
                               countx, siney * 100 + offset);
       sinex1 = sinex;
       sinex1 = siney * 100;
       countx += 1;
    }
    while (countx <= widthG);
}
Sunil
  • 3,404
  • 10
  • 23
  • 31
  • Thanks for the help! Looks Like I need a book on Windows Forms and using Paint vs CreateGraphics - Any suggestions? Any downloadable versions? – EngRick Dec 26 '17 at 15:40
  • https://stackoverflow.com/questions/15057406/c-sharp-draw-line-onpaint-vs-creategraphics - Link to a similar question. Concise answers are better than big books ;) – Sunil Dec 26 '17 at 16:17