Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

c# - find form instance from other class

I have Main form with list of data inside listBox. On button click I'm opening new form to create new data object (Main form is inactive in background), when new data is submitted listobox inside main form should be populated with that new object.

I was thinking following:

  1. When Form2 is submitted I was thinking to find MainForm instance and kill that instance and after that it should be easy, load again list of data from the db and display in the listbox.

Question is:

If Form1 is created and on some event Form2 is instantiated with showDialog so Form1 is inactive until data is submitted how to find Form1 instance before Form2 is closed?

So again, how to find instance of Form1 class from Form2 class?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get a reference to any of the application's currently open forms by using the Application.OpenForms property. Forms in this FormCollection can be accessed by index like so:

var form1 = Application.OpenForms[0];

or by the form's Name property like so:

Form form1 = Application.OpenForms["Form1"];

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...