Skip to main content
 首页 » 编程设计

c#之如果窗体上没有其他控件,图表就会消失

2024年02月20日14itcoder

这是我的代码

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 项时它们将保持可见。然后线条和图形不会消失 这里可能有一些基本的东西,但我想我错过了那个。 需要帮助

请您参考如下方法:

您的绘图应该在 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); 
     } 
} 

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