C# Access Method of Form1 on Form2

后端 未结 4 974
生来不讨喜
生来不讨喜 2021-01-16 21:42

I have 2 Forms in my project. Form1 is the main Form. There I have a button to open Form2, a ListView and a method to call a url and feed the ListView with data it gets from

4条回答
  •  Happy的楠姐
    2021-01-16 21:49

    Define a property on Form2 to allow the access of the result

    // In Form2
    public string Url { get { return urlTextBox.Text; } }
    

    In Form1

    var form2 = new Form2();
    form2.ShowDialog(this);
    string url = form2.Url;
    

    Note: unless Show() the ShowDialog() method waits until form2 is closed.

    The ShowDialog argument this is the owner of the form to be opened. It binds form2 to form1 and makes it always appear on top of form1.

提交回复
热议问题