Important! This is an automatic machine translated page. If you can read english, you should Click Here to read the original English version of the article.

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ó.

This article was originally written on 09/20/06 Tagged with: Bài viết này ban đầu được viết ngày 09/20/06 Tagged với: Programming Lập trình

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:


Name: Tên:
Email: Email:

Comments (2) Comment (2)

  1. cypher cypher

    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.

  2. Rama krishna Rama krishna

    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 ý


Leave a Comment Để lại một Thảo luận




Leave your Để lại của bạn friendly thân thiện comment here. bình luận ở đây.

If you have a computer help question, Nếu bạn có một câu hỏi giúp máy tính, click here to leave it on the forums bấm vào đây để lại nó trên các diễn đàn instead. thay vì.

Note: Your comment may not show up immediately on the site. Lưu ý: Ý kiến của bạn có thể không hiển thị ngay lập tức trên trang web.

Our Friends Bạn bè của chúng tôi
Getting Started Bắt đầu


About How-To Geek Giới How-To Geek
What Is That Process? Quá trình đó là gì?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe dwm.exe
ctfmon.exe ctfmon.exe
wmpnetwk.exe wmpnetwk.exe
wmpnscfg.exe wmpnscfg.exe
rundll32.exe rundll32.exe
wfcrun32.exe wfcrun32.exe
Ipoint.exe Ipoint.exe
Itype.exe Itype.exe
Wfica32.exe Wfica32.exe
Mobsync.exe Mobsync.exe
conhost.exe conhost.exe
Dpupdchk.exe Dpupdchk.exe Adobe_Updater.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. Bản quyền © 2006-2009 HowToGeek.com. All Rights Reserved. Tất cả các quyền.