Referencing control on one form from another form

后端 未结 8 2011
轻奢々
轻奢々 2020-12-12 01:30

As you can guess, I\'m kind of new to .NET and just want to reference a control on one form from another.

Usually I would just do Form.Control.Property but that does

8条回答
  •  [愿得一人]
    2020-12-12 02:02

    Make a private field. Right click 'Refactor', select 'Encapsulate Field'. This will automatically create a public property for the field.

    Another approach is to overload the public constructor.

    public CustomersForm(string property1, string property2...)
    {
         //Get the properties and do what is necessary.
    }
    
    //Parent Form
    
    CustomersForm frmCustomers = new CustomersForm(property1, property2..);
    

    Also sending the complete control to another form is not a good strategy. Share only the fields that are necessary via public properties/constructors.

提交回复
热议问题