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() 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.

This article was originally written on 09/20/06 Tagged with: Ten artykuł został napisany na 09/20/06 Tagged with: Programming Programowanie

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:


Name: Imię i nazwisko:
Email: E-mail:

Comments (2) Komentarze (2)

  1. cypher Cypher

    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.

  2. Rama krishna Rama Krishna

    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ź


Leave a Comment Zostaw komentarz




Leave your Zostaw swój friendly przyjazny comment here. komentarz.

If you have a computer help question, Jeśli masz pytanie pomocy komputera, click here to leave it on the forums Kliknij tutaj, aby pozostawić ją na forum instead. zamiast.

Note: Your comment may not show up immediately on the site. Uwaga: Twój komentarz nie pojawi się od razu na stronie.

Our Friends Nasi Przyjaciele
Getting Started Pierwsze kroki


About How-To Geek O How-To Geek
What Is That Process? Co to jest proces?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe dwm.exe
ctfmon.exe Gadu-Gadu
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. Copyright © 2006-2009 HowToGeek.com. All Rights Reserved. All Rights Reserved.