Using and instance of a class on two forms

前端 未结 3 1209
野的像风
野的像风 2021-01-23 21:24

I am struggling to get my head around the following. I current have three forms, my main and one main class.

public partial class frmMain : Form
{
    public frm         


        
3条回答
  •  星月不相逢
    2021-01-23 22:01

    You should add a field of type StockControl to each of your forms, and make it public, or add getter/setter to it. This means adding the following lines to each one of your forms:

    private StockControl _stockCtrl;
    public StockControl StockCtrl
    {
       get { return _stockCtrl; }
       set { _stockCtrl = value; }
    }
    

    The in the cod of each form you can access your StockControl. But it will be empty (i.e. null) if you don't assign it something. This is something I'd do before opening the form. If you are in your main method:

    frmSuppliers frmToOpen = new frmSuppliers();
    frmSuppliers.StockCtrl = StockSystem;
    frmSuppliers.Show();
    

提交回复
热议问题