2012-08-01 30 views
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项目,他们将仍然可见。然后线和图不消失 这里可能有一些基本的东西,但我想我错过了那一个。 需要帮助

回答

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方法中,并记住了我移到这里的代码应该从该方法中移除。

+0

你能举一些例子或者指点我一些有用的链接吗?我正在尝试,但仍然不能正常工作 – 2012-08-01 20:05:05

+0

@jailedabroad编辑了一下 – NominSim 2012-08-01 20:12:48

2

不要在您的菜单中画图点击。取而代之的是Invalidate()并在随后的OnPaint中重绘。