简体   繁体   English

表单加载更改另一个表单元素属性

[英]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. 当前,Form1将完成解析,但根据您已连接的显示数量,打开更多表单,因此2个显示将创建2个新的Form2实例。

Form2 is just a Form which has a WebBrowser loaded within it. Form2只是一个其中装有WebBrowser的Form。 I code nothing on Form2 whatsoever. 我什么都不在Form2上编码。

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. 在Show()Form2之前,我先设置了一些属性,例如全宽和全高,以便打开的窗体占据该监视器的全屏。 How do I access Form2.WebBrowser1.Url from Form1? 如何从Form1访问Form2.WebBrowser1.Url? I need Form1 to make it load a different URL per screen. 我需要Form1使其在每个屏幕上加载不同的URL。

I would either:- 我会:-

  • pass the URL in on the constructor or 在构造函数上传递URL或
  • 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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