简体   繁体   中英

How to use an object in more than one form in Visual C#?

I'm new in c#. I have read some book about c# for beginners. Now i can code simple applications. I have 2 forms and 1 class in a project. I want to use a object from that class in 2 forms. I want first form to set a property of the object and secont to display this property. How can i do it. Please help me. Thanks

You need to change the constructor of the second form to take the class instance as a parameter.

For example:

public partial class PopupForm : Form {
    public PopupForm(MyClass instance) {
        InitializeComponent();
        //Do things with instance
    }
    ...
}

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