我想从Form2(文本框,图片框)控制Form1 [复制]

时间:2021-09-09 04:22:43

This question already has an answer here:

这个问题在这里已有答案:

I want to resize my picturebox in Form1 (picturebox1) using textbox1,textbox2 and button in Form2.

我想使用text2,textbox2和Form2中的按钮调整Form1(picturebox1)中的图片框大小。

First I made this in Form1.Designer.cs:

首先我在Form1.Designer.cs中做了这个:

 public System.Windows.Forms.PictureBox picturebox1;

After this in Form2.cs (submit button):

在Form2.cs之后(提交按钮):

private void button1_Click(object sender, EventArgs e)
    {
Form1 frm1 = new Form1();

        int height = int.Parse(textbox1.Text);
        frm1.picturebox1.Height = height;
        int width = int.Parse(textbox2.Text);
        frm1.picturebox1.Width = width; 
}

But this not change the size of the picturebox1...

但这并没有改变picturebox1的大小......

1 个解决方案

#1


0  

This is your problem:

这是你的问题:

Form1 frm1 = new Form1();

You're creating a new instance of Form1. So you're successfully modifying the values on that new instance, but doing nothing to the instance you already have.

您正在创建Form1的新实例。因此,您已成功修改该新实例上的值,但对您已有的实例不执行任何操作。

Form2 needs a reference to the existing instance, not create a new one. You can supply that instance to Form2 on its constructor for example. Something like this:

Form2需要对现有实例的引用,而不是创建新实例。例如,您可以在其构造函数上将该实例提供给Form2。像这样的东西:

private Form1 Form1Instance { get; set; }

public Form2(Form1 form1Instance)
{
    this.Form1Instance = form1Instance;
}

Then when you create an instance of Form2 from Form1, you'd supply it the reference it needs:

然后,当您从Form1创建Form2的实例时,您将为它提供所需的引用:

var form2 = new Form2(this);
form2.Show();

Then any code in Form2 can act upon that instance of Form1. Something like this:

然后,Form2中的任何代码都可以作用于Form1的该实例。像这样的东西:

private void button1_Click(object sender, EventArgs e)
{
    int height = int.Parse(textbox1.Text);
    this.Form1Instance.picturebox1.Height = height;
    int width = int.Parse(textbox2.Text);
    this.Form1Instance.picturebox1.Width = width; 
}

#1


0  

This is your problem:

这是你的问题:

Form1 frm1 = new Form1();

You're creating a new instance of Form1. So you're successfully modifying the values on that new instance, but doing nothing to the instance you already have.

您正在创建Form1的新实例。因此,您已成功修改该新实例上的值,但对您已有的实例不执行任何操作。

Form2 needs a reference to the existing instance, not create a new one. You can supply that instance to Form2 on its constructor for example. Something like this:

Form2需要对现有实例的引用,而不是创建新实例。例如,您可以在其构造函数上将该实例提供给Form2。像这样的东西:

private Form1 Form1Instance { get; set; }

public Form2(Form1 form1Instance)
{
    this.Form1Instance = form1Instance;
}

Then when you create an instance of Form2 from Form1, you'd supply it the reference it needs:

然后,当您从Form1创建Form2的实例时,您将为它提供所需的引用:

var form2 = new Form2(this);
form2.Show();

Then any code in Form2 can act upon that instance of Form1. Something like this:

然后,Form2中的任何代码都可以作用于Form1的该实例。像这样的东西:

private void button1_Click(object sender, EventArgs e)
{
    int height = int.Parse(textbox1.Text);
    this.Form1Instance.picturebox1.Height = height;
    int width = int.Parse(textbox2.Text);
    this.Form1Instance.picturebox1.Width = width; 
}