Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在同一窗体上添加新坐标_C#_Winforms - Fatal编程技术网

C# 在同一窗体上添加新坐标

C# 在同一窗体上添加新坐标,c#,winforms,C#,Winforms,当在表格1的文本框中输入圆的坐标并单击按钮时,我的代码在表格2中绘制了一个圆。问题是,每次在表格1中输入坐标时,都会打开一个新的表格2,而不是更新旧的表格 表格1上的代码 private void button1_Click(object sender, EventArgs e) { int r1, r2; setValue = textBox1.Text; setValue1 = textBox2.Text; Int32.TryParse(setValue, o

当在表格1的文本框中输入圆的坐标并单击按钮时,我的代码在表格2中绘制了一个圆。问题是,每次在表格1中输入坐标时,都会打开一个新的表格2,而不是更新旧的表格

表格1上的代码

private void button1_Click(object sender, EventArgs e)
{
    int r1, r2;
    setValue = textBox1.Text;
    setValue1 = textBox2.Text;
    Int32.TryParse(setValue, out r1);
    Int32.TryParse(setValue1, out r2);
    Form2 f2 = new Form2();
    //// f2.Show();
    // f2.addcoordinate(r1,r2);
    // f2.Update();
    Graphics g2;
    g2 = f2.CreateGraphics();
    Class1 add = new Class1();
    add.addcoordinate(r1,r2);
}
1类代码

public void addcoordinate(int r1, int r2)
{
   // MessageBox.Show(r1.ToString());
    Form2 f2 = new Form2();
    f2.addcoordinate(r1, r2);
    f2.Show();
}
表格2上的代码

private List<Point> circleCoordinates = new List<Point>();

public Form1()
{
    InitializeComponent();
}

public void addcoordinate(int r1, int r2)
{
    this.circleCoordinates.Add(new Point(r1, r2));
}

protected override void OnPaint(PaintEventArgs e)
{
    // linedrawing goes here
    foreach (Point point in this.circleCoordinates)
    {
        e.Graphics.DrawEllipse(Pens.Black, new Rectangle(point, new 
        Size(10, 10)));
    }

    base.OnPaint(e);
}
private List circleCoordinates=new List();
公共表格1()
{
初始化组件();
}
公共无效添加坐标(整数r1,整数r2)
{
此.circleCoordinates.Add(新点(r1,r2));
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
//线条画在这里
foreach(本循环坐标中的点)
{
e、 图形.抽屉式(钢笔.黑色,新矩形(点,新
大小(10,10));
}
基础漆(e);
}

请建议。

表格1
中,您对
f2
的定义如下:

Form2 f2 = new Form2();
每次运行这行代码时,它都会创建对象的新实例。这就是为什么每次单击按钮时都会看到一个新表单


Form1
类中定义
Form2
对象,并通过将上述代码行移到类中的方法之外,从所有私有方法中定义该对象。然后在方法内部的代码中使用您声明的
Form2
的特定实例(
f2
)。这样,您正在处理类的同一个实例,而不是每次单击
按钮1

时都创建对象
Form2
的新实例,将对Form2对象的引用存储在某个位置并使用它,而不是每次都创建一个新实例。
Form2 f2=new Form2()每次运行表单时都会创建表单的新副本。而是创建一个副本,并重新使用它。
f2=newform2()仍然使用表单的新实例重新初始化变量。您所做的更改就是声明变量的位置。代码需要在表单的现有实例上操作,而不是在新实例上操作@ADyson感谢您的反馈。我编辑了答案。此外,我想在小时间间隔内,在所选坐标上绘制半径逐渐增大的圆。请建议怎么做。