Pass Variables between Windows Forms Windows without ShowDialog() Pass biến giữa Windows Forms Windows mà không ShowDialog ()
When you are programming a Windows Forms application, you will invariably need to get variables from a second form window, such as an options form or popup search prompt. Khi bạn được lập trình một ứng dụng Windows Forms, bạn lúc nào sẽ cần phải nhận được biến từ một cửa sổ hình thức thứ hai, như là một hình thức lựa chọn hoặc tìm kiếm popup nhắc.
Most of the guides out there will tell you that you have to open the second form with ShowDialog(), which blocks the user from doing anything else until they've closed the second form window. Hầu hết các hướng dẫn ra khỏi đó sẽ cho bạn biết rằng bạn đã để mở hình thức thứ hai với ShowDialog (), trong đó khối người sử dụng từ làm bất cứ điều gì khác cho đến khi họ đã đóng cửa sổ mẫu thứ hai. This won't work very well for a find/replace dialog, for instance. Điều này sẽ không hoạt động rất tốt cho một tìm / thay thế các hộp thoại, ví dụ. It also won't work very well for custom drawn popup forms. Nó cũng sẽ không làm việc rất tốt để tùy chỉnh rút ra các hình thức popup.
The quick way to pass variables between the forms is using Delegates. Cách nhanh chóng vượt qua các biến giữa các hình thức sử dụng đại biểu. You can set an eventhandler for the Closing event of the second form, and handle the event in the first form. Bạn có thể thiết lập một EventHandler cho sự kiện đóng cửa của mẫu thứ hai, và xử lý các sự kiện trong những hình thức đầu tiên. This allows you to capture variables before the second form window has closed. Điều này cho phép bạn chụp các biến trước khi cửa sổ thứ hai có hình thức đóng cửa.
For this exercise, we're going to assume that we have two forms: Để thực hiện điều này, chúng ta sẽ giả định rằng chúng tôi có hai hình thức:
MainForm MainForm
OptionsForm OptionsForm
We're going to further assume that we've clicked some sort of button that opens the OptionsForm with a Show() method call. Chúng ta sẽ tiếp tục giả định rằng chúng tôi đã nhấp vào một số loại nút mở ra các OptionsForm với một phương pháp gọi Show (). Now let's take a look at the magic: Bây giờ chúng ta hãy nhìn vào những phép thuật:
……. ... .... snip……. snip ... ....
OptionsForm theform = new OptionsForm(); OptionsForm theform = mới OptionsForm ();
theform.Closing += new CancelEventHandler(theform_Closing); theform.Closing + = mới CancelEventHandler (theform_Closing);
theform.Show(); theform.Show ();} )
private void theform_Closing(object sender, CancelEventArgs e) private void theform_Closing người gửi đối tượng (, CancelEventArgs e)
{ (OptionsForm theform = (OptionsForm)sender; OptionsForm theform = (OptionsForm) người gửi;
// Grab the variable from the options form. / / Grab biến từ các hình thức lựa chọn. The options form should set this variable before it closes, and the variable should be marked as public. Các hình thức lựa chọn nên đặt biến này trước khi nó đóng cửa, và biến cần được đánh dấu là công cộng.
string localvar = theform.thestringvariable; string localvar = theform.thestringvariable;} )
That's all there is to it. Đó là tất cả để có nó.

Daily Email Updates Email cập nhật hàng ngày
You can get our how-to articles in your inbox each day for free. Bạn có thể làm thế nào chúng tôi vào các bài viết trong hộp thư của bạn mỗi ngày miễn phí. Just enter your name and email below: Chỉ cần nhập tên và email của bạn dưới đây:



I guess this works better : Tôi đoán này làm việc tốt hơn:
theform.FormClosing += new FormClosingEventHandler(theform_Closing); theform.FormClosing + = mới FormClosingEventHandler (theform_Closing);
Thanks it helped me a lot to figure it out anyway. Nhờ đó đã giúp tôi rất nhiều để con nó ra anyway.
it worked for me as well in great deal nó làm việc cho tôi cũng như trong thỏa thuận tuyệt vời
A big thanks for the person who has given the hint Một lớn nhờ những người đã đưa ra những gợi ý