在C#中调用另一个表单上的方法

时间:2022-11-17 15:55:30

I am building a basic Image editor. In my app, if the user wants to resize the image a new form pops up and asks the user to input an new width and height for the image.

我正在构建一个基本的图像编辑器。在我的应用程序中,如果用户想要调整图像大小,则弹出一个新表单并要求用户输入图像的新宽度和高度。

public partial class Form1 : Form
{
...
    private void resizeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        resize resizeForm = new resize();
        resizeForm.ShowDialog();
    }
...
}

I am wondering how I can get the values from the resizeForm and use them to alter the image on the parent form (the Form1 instance).

我想知道如何从resizeForm获取值并使用它们来更改父窗体(Form1实例)上的图像。

If this question needs clarification please let me know.

如果这个问题需要澄清,请告诉我。

Thanks!

3 个解决方案

#1


I assume there are a number of ways to do this. I'd probably use public properties on the resizeForm and then get those when the resizeForm.ShowDialog() returns.

我假设有很多方法可以做到这一点。我可能在resizeForm上使用公共属性,然后在resizeForm.ShowDialog()返回时获取它们。

if (resizeForm.ShowDialog() == DialogResult.OK) // or whatever
{
   myVal = resizeForm.Val;
   ...
}

or something like that.

或类似的东西。

#2


Setup properties in your "resize" class for the values you want to retrieve. For example, if you add a width property:

在“resize”类中为要检索的值设置属性。例如,如果添加width属性:

public int Width { get; set; }

you will be able to get the width from your Form1 class.

您将能够从Form1类中获取宽度。

#3


Add properties to your resize form that your main form can interrogate after the resize form is closed, like ...

在调整大小窗体关闭后,您的主窗体可以查询调整大小窗体的属性,例如...

DialogResult dr = resizeForm.ShowDialog();

if( dr != DialogResult.Cancel )
{
  var newH = resizeForm.Height;
  var newW = resizeForm.Width;

  // do something with new vals.
}

#1


I assume there are a number of ways to do this. I'd probably use public properties on the resizeForm and then get those when the resizeForm.ShowDialog() returns.

我假设有很多方法可以做到这一点。我可能在resizeForm上使用公共属性,然后在resizeForm.ShowDialog()返回时获取它们。

if (resizeForm.ShowDialog() == DialogResult.OK) // or whatever
{
   myVal = resizeForm.Val;
   ...
}

or something like that.

或类似的东西。

#2


Setup properties in your "resize" class for the values you want to retrieve. For example, if you add a width property:

在“resize”类中为要检索的值设置属性。例如,如果添加width属性:

public int Width { get; set; }

you will be able to get the width from your Form1 class.

您将能够从Form1类中获取宽度。

#3


Add properties to your resize form that your main form can interrogate after the resize form is closed, like ...

在调整大小窗体关闭后,您的主窗体可以查询调整大小窗体的属性,例如...

DialogResult dr = resizeForm.ShowDialog();

if( dr != DialogResult.Cancel )
{
  var newH = resizeForm.Height;
  var newW = resizeForm.Width;

  // do something with new vals.
}