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. זה כל מה שיש בו.

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



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. תודה זה עזר לי הרבה כדי להבין את זה בכלל.
it worked for me as well in great deal זה עבד לי גם ב הרבה
A big thanks for the person who has given the hint תודה גדולה עבור מי נתן את הרמז