Pass Variables between Windows Forms Windows without ShowDialog() Przekazywanie zmiennych między Windows Forms, Windows bez 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. Kiedy jesteś programowania aplikacji Windows Forms, będziesz zawsze potrzebujesz, aby zmienne z drugiego okna formularza, np. formularz wyszukiwania kontekstowego opcji lub polecenia.
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. Większość przewodników tam powie, że trzeba otworzyć drugi formularz z ShowDialog (), który blokuje użytkownikowi robić niczego innego, dopóki nie zamkniesz drugie okno formularza. This won't work very well for a find/replace dialog, for instance. Metoda ta nie działa dobrze na znalezienie / Replace Dialog, na przykład. It also won't work very well for custom drawn popup forms. To również nie będzie działać bardzo dobrze opracowane popup niestandardowe formy.
The quick way to pass variables between the forms is using Delegates. Szybkim sposobem na przekazywanie zmiennych między formy używa delegatów. You can set an eventhandler for the Closing event of the second form, and handle the event in the first form. Można ustawić eventhandler przy zamknięciu drugiego formularza i obsługi zdarzeń w pierwszej formie. This allows you to capture variables before the second form window has closed. To pozwala na przechwytywanie zmiennych przed drugim oknie formularz został zamknięty.
For this exercise, we're going to assume that we have two forms: W tym ćwiczeniu będziemy zakładać, że mamy dwa rodzaje:
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. Jedziemy dalej zakładamy, że mamy kliknął jakiś przycisk, który otwiera OptionsForm z Show () wywołanie metody. Now let's take a look at the magic: Teraz możemy przyjrzeć się magii:
……. ... .... snip……. ciach ... ....
OptionsForm theform = new OptionsForm(); TheForm OptionsForm = new OptionsForm ();
theform.Closing += new CancelEventHandler(theform_Closing); theform.Closing + = new CancelEventHandler (theform_Closing);
theform.Show(); theform.Show ();} )
private void theform_Closing(object sender, CancelEventArgs e) private void theform_Closing (object sender, CancelEventArgs e)
{ (OptionsForm theform = (OptionsForm)sender; TheForm OptionsForm = (OptionsForm) nadawcy;
// Grab the variable from the options form. / / Grab zmiennej z formularza opcji. The options form should set this variable before it closes, and the variable should be marked as public. Formie opcji należy ustawić tę zmienną przed zamknięciem, a zmienna powinny być oznaczone jako publiczne.
string localvar = theform.thestringvariable; localvar string = theform.thestringvariable;} )
That's all there is to it. To wszystko było na tyle.

Daily Email Updates Daily Email Updates
You can get our how-to articles in your inbox each day for free. Możesz pobrać nasze instrukcje postępowania w skrzynce odbiorczej codziennie za darmo. Just enter your name and email below: Wystarczy podać swoje imię i adres e-mail:



I guess this works better : Myślę, że to działa lepiej:
theform.FormClosing += new FormClosingEventHandler(theform_Closing); theform.FormClosing + = new FormClosingEventHandler (theform_Closing);
Thanks it helped me a lot to figure it out anyway. Dzięki to bardzo mi pomogli ustalić to tak.
it worked for me as well in great deal ono pracował dla mnie, jak również w dużym
A big thanks for the person who has given the hint Wielkie podziękowania dla osoby, która dała podpowiedź