简体   繁体   中英

Form load change another form element properties

Current, Form1 will finish parsing but open up more forms depending on the amount of displays you have connected, so 2 displays will make 2 new instances of Form2.

Form2 is just a Form which has a WebBrowser loaded within it. I code nothing on Form2 whatsoever.

Before I Show() Form2, I set some properties, such as full width and height, so that form being opened takes up the full screen of that monitor. How do I access Form2.WebBrowser1.Url from Form1? I need Form1 to make it load a different URL per screen.

I would either:-

  • pass the URL in on the constructor or
  • have a public property on the form

Depends on whether you just want to change it once the form has loaded.

    public partial class Form2 : Form
{

    //This is the Constructor
    public Form2()
    {
        InitializeComponent();

    }

    //This is an overloaded constructor that takes a url argument
    public Form2(string URL )
    {
        InitializeComponent();

        //Store the URL For Later
        URLToDisplay = URL

    }


    //Property that you can access any where you have a reference to the form instance
    public int URLToDisplay { get; set; }

}

using the constructor above you can then do this....

    Form2 frm = new Form2("www.google.co.uk");
    frm.Show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM