Calling a method on another form in C#

前端 未结 3 1959
清歌不尽
清歌不尽 2021-01-23 14:15

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.

<
相关标签:
3条回答
  • 2021-01-23 14:49

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

    public int Width { get; set; }
    

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

    0 讨论(0)
  • 2021-01-23 15:00

    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.

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

    or something like that.

    0 讨论(0)
  • 2021-01-23 15:01

    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.
    }
    
    0 讨论(0)
提交回复
热议问题