1

这是我的代码

private void graphToolStripMenuItem_Click(object sender, EventArgs e)
        {

            button1.Visible = false;
            button2.Visible = false;
            button3.Visible = false;
            button4.Visible = false;
            label1.Visible = false;
            textBox1.Visible = false;
            textBox2.Visible = false;
            textBox3.Visible = false;
            textBox4.Visible = false;
            textBox5.Visible = false;
            textBox6.Visible = false;
            textBox7.Visible = false;
            textBox8.Visible = false;
            textBox9.Visible = false;
            textBox10.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = true;
            gg = this.CreateGraphics();
            p3 = new Pen(Color.Blue,5);
            b1 = new SolidBrush(Color.Red);
            p2 = new Pen(Color.Red);
            Font f=new Font("Arial",16);
            float ox = this.ClientSize.Width / 2;
            float oy = this.ClientSize.Height / 2;
            gg.DrawLine(p3, ox - 500, oy, ox + 500, oy);
            gg.DrawLine(p3, ox, oy + 300, ox, oy - 300);
            gg.DrawString("Argument", f, b1, ox - 100, oy - 200);
            gg.DrawString("<----f(Argument)---->", f, b1, ox + 100, oy + 100);
            for (int i = 0; i < 1000; i++)
            {
                double tem1 = graphValuesCal();
                double temp2 = functionCal();
                gg.FillEllipse(b1, ox + (float)tem1/2,oy-20*(float)temp2, (float)5, (float)5);
                Thread.Sleep(10);
            }

        }

当我运行此代码时,会绘制图表,但是当循环完成图表以及字符串和线条(轴)消失时。如果我注释掉这段代码

                button1.Visible = false;
                button2.Visible = false;
                button3.Visible = false;
                button4.Visible = false;
                label1.Visible = false;
                textBox1.Visible = false;
                textBox2.Visible = false;
                textBox3.Visible = false;
                textBox4.Visible = false;
                textBox5.Visible = false;
                textBox6.Visible = false;
                textBox7.Visible = false;
                textBox8.Visible = false;
                textBox9.Visible = false;
                textBox10.Visible = false;
                label2.Visible = false;
                label3.Visible = false;

意味着如果所有这些控件之前都是可见的,那么当我单击该Graph menu项目时它们将保持可见。然后线条和图表不会消失这里可能有一些基本的东西,但我想我错过了那个。需要帮助

4

2 回答 2

2

不要在你的菜单中点击。相反,Invalidate()并在随后的OnPaint覆盖中绘制。

于 2012-08-01T19:32:42.440 回答
2

您的绘图应该在OnPaint事件方法中完成。

在您的表单上遵守OnPaint事件并将您的绘画添加到该方法中,如下所示:

// Put this in your constructor, or use VisualStudio to create the method for you
this.Paint += new System.Windows.Forms.PaintEventHandler(this.paint_Method);

private void paint_Method(object sender, PaintEventArgs e)
{ 
     gg = e.Graphics;
     p3 = new Pen(Color.Blue,5);
     b1 = new SolidBrush(Color.Red);
     p2 = new Pen(Color.Red);
     Font f=new Font("Arial",16);
     float ox = this.ClientSize.Width / 2;
     float oy = this.ClientSize.Height / 2;
     gg.DrawLine(p3, ox - 500, oy, ox + 500, oy);
     gg.DrawLine(p3, ox, oy + 300, ox, oy - 300);
     gg.DrawString("Argument", f, b1, ox - 100, oy - 200);
     gg.DrawString("<----f(Argument)---->", f, b1, ox + 100, oy + 100);
     for (int i = 0; i < 1000; i++)
     {
         double tem1 = graphValuesCal();
         double temp2 = functionCal();
         gg.FillEllipse(b1, ox + (float)tem1/2,oy-20*(float)temp2, 5f, 5f);
         // Thread.Sleep(10);
     }
}

您可能也想this.Invalidate()在您的graphToolStripMenuItem_Click方法中调用,并记住我在此处移动的代码应该从该方法中删除。

于 2012-08-01T19:33:31.193 回答