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() פס משתנים בין-Windows Forms Windows ללא 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. כאשר אתה תכנות-Windows Forms Application, אתם תמיד צריכים להגיע המשתנים מחלון הטופס השני, כגון טופס חיפוש נוספות או קופץ הפקודה.

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. רוב המדריכים שם יגידו לך שיש לך לפתוח את הטופס השני עם ShowDialog (), אשר חוסמת את המשתמש לעשות שום דבר אחר עד שהם סגרו את החלון הטופס השני. This won't work very well for a find/replace dialog, for instance. זה לא יעבוד טוב מאוד עבור מצא / להחליף שיח, למשל. It also won't work very well for custom drawn popup forms. זה גם לא יעבוד טוב מאוד עבור המנהג נמשך טפסים קופץ.

The quick way to pass variables between the forms is using Delegates. דרך מהירה להעביר משתנים בין טפסים הוא באמצעות נציגים. You can set an eventhandler for the Closing event of the second form, and handle the event in the first form. ניתן להגדיר eventhandler עבור אירוע הנעילה של הטופס השני, ולטפל האירוע בטופס הראשון. This allows you to capture variables before the second form window has closed. זה מאפשר לך ללכוד משתנים לפני החלון הטופס השני נסגר.

For this exercise, we're going to assume that we have two forms: עבור התרגיל הזה, אנחנו הולכים להניח כי יש לנו שתי צורות:

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. אנחנו הולכים עוד יותר להניח כי אנחנו לוחצים על כפתור כלשהו אשר פותח את OptionsForm עם הצג () שיחת השיטה. Now let's take a look at the magic: עכשיו בוא נסתכל הקסם:

……. ... .... snip……. לגזור ... ....

OptionsForm theform = new OptionsForm(); Theform OptionsForm = OptionsForm חדש ();
theform.Closing += new CancelEventHandler(theform_Closing); theform.Closing + = CancelEventHandler חדש (theform_Closing);
theform.Show(); theform.Show ();

} )

private void theform_Closing(object sender, CancelEventArgs e) החלל הפרטי theform_Closing (השולח אובייקט, CancelEventArgs דואר)
{ (

OptionsForm theform = (OptionsForm)sender; Theform OptionsForm = (OptionsForm) השולח;

  // Grab the variable from the options form. / / תפוס את המשתנה מהטופס נוספות. The options form should set this variable before it closes, and the variable should be marked as public. את הטופס אפשרויות להגדיר את זה לפני שהוא סוגר משתנה, ואת המשתנה צריך להיות מסומן כ הציבור.
string localvar = theform.thestringvariable; localvar מחרוזת = theform.thestringvariable;

} )

That's all there is to it. זה כל מה שיש בו.

This article was originally written on 09/20/06 Tagged with: מאמר זה נכתב במקור ב 09/20/06 עם שתייגת: Programming תכנות

Daily Email Updates שערי עדכונים בדוא"ל

You can get our how-to articles in your inbox each day for free. אתה יכול לקבל כמה שלנו למאמרים לתיבת הדואר שלך בכל יום בחינם. Just enter your name and email below: פשוט להזין את שם ואת הדוא"ל שלך להלן:


Name: שם:
Email: דואר אלקטרוני:

Comments (2) תגובות (2)

  1. cypher הצופן

    I guess this works better : אני מניח שזה עובד טוב יותר:
    theform.FormClosing += new FormClosingEventHandler(theform_Closing); theform.FormClosing + = FormClosingEventHandler חדש (theform_Closing);

    Thanks it helped me a lot to figure it out anyway. תודה זה עזר לי הרבה כדי להבין את זה בכלל.

  2. Rama krishna קרישנה ראמה

    it worked for me as well in great deal זה עבד לי גם ב הרבה

    A big thanks for the person who has given the hint תודה גדולה עבור מי נתן את הרמז


Our Friends חברים שלנו
Getting Started התחלת העבודה


About How-To Geek אודות How-To Geek
What Is That Process? מה זה תהליך?
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. זכויות יוצרים © 2006-2009 HowToGeek.com. All Rights Reserved. כל הזכויות שמורות.