这是我的代码
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
项时,它们将保持可见状态。然后线条和图形不会消失
这里可能有一些基本但我想我错过了那个。
需要帮助
答案 0 :(得分:2)
不要在菜单中点击。相反,Invalidate()
并在随后的OnPaint
覆盖中绘制。
答案 1 :(得分: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
,并请记住我移动的代码应该从该方法中移除。