简体   繁体   中英

How to get textboxt's text from another class c#

I have a form main and it include a textbox companyname and other form name is Form1 and a class ListViewPrinter.cs under Form1.cs .I just want to get companyname value from ListViewPrinter.cs . I tried to do

TextBox com = Application.OpenForms["main"].Controls["companyname"] as TextBox;

but it gives me NullReferenceException was unhandled by user code .How can I get companyname 在此处输入图片说明

You need to either make the class ListViewPrinter static or create an instance of it. Google for these two terms to get an idea.

You may declare main form as static and get the value as you will get it from main.

public static main main_ = new main();

....

string txtValue = main_.companyname;

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