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() Odovzdávanie premenných medzi 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. Keď ste programovanie Windows Forms aplikácie, budete musieť vždy dostať premenných z druhého okna formulára, ako sú opcie formulár alebo miestne hľadanie napovedať.

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. Väčšina sprievodcov tam vám povedia, že máte otvoriť druhý formulár s ShowDialog (), ktorý blokuje užívateľovi robiť niečo iné, kým som zavrel okno druhej forme. This won't work very well for a find/replace dialog, for instance. To nebude fungovať veľmi dobre nájsť / nahradiť dialóg, napr. It also won't work very well for custom drawn popup forms. To tiež nebude fungovať veľmi dobre vypracované vlastné miestne formy.

The quick way to pass variables between the forms is using Delegates. Rýchly spôsob prenosu premenných medzi formami používa delegátov. You can set an eventhandler for the Closing event of the second form, and handle the event in the first form. Môžete nastaviť EventHandler pre zakončenie druhého formuláre a spracovať udalosti v podobe prvého. This allows you to capture variables before the second form window has closed. To vám umožní zachytiť premenné pred druhú formu okna bola zatvorená.

For this exercise, we're going to assume that we have two forms: K tomuto kroku, budeme predpokladať, že budeme mať dve formy:

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. Chystáme ďalej predpokladať, že sme klikli nejaké tlačidlo, ktoré otvorí OptionsForm s Show () volanie metódy. Now let's take a look at the magic: Teraz sa poďme pozrieť na kúzla:

……. ... .... snip……. snip ... ....

OptionsForm theform = new OptionsForm(); OptionsForm theForm = 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; OptionsForm theForm = (OptionsForm) odosielateľom;

// Grab the variable from the options form. / / Vem premennú z možností formulára. The options form should set this variable before it closes, and the variable should be marked as public. Možnosti forma mala tento ukazovateľ skôr, ako ju zavrie a premenná by mala byť označená ako verejná.
string localvar = theform.thestringvariable; string localvar = theform.thestringvariable;

} )

That's all there is to it. To je všetko, čo sa to.

This article was originally written on 09/20/06 Tagged with: Tento článok bol pôvodne napísaný na 09/20/06 Tagged s: Programming Programovanie

Daily Email Updates Denný Svářeč

You can get our how-to articles in your inbox each day for free. Môžete si naše jak-na články vo vašej schránky každý deň zadarmo. Just enter your name and email below: Stačí zadať svoje meno a e-mail nižšie:


Name: Meno:
Email: E-mail:

Comments (2) Komentáre (2)

  1. cypher nicka

    I guess this works better : Myslím, že to funguje lepšie:
    theform.FormClosing += new FormClosingEventHandler(theform_Closing); theform.FormClosing + = new FormClosingEventHandler (theform_Closing);

    Thanks it helped me a lot to figure it out anyway. Vďaka, že mi veľa pomohli, aby na to prišiel tak ako tak.

  2. Rama krishna Rama Krishna

    it worked for me as well in great deal it worked for me aj vo veľkej

    A big thanks for the person who has given the hint Veľký dík za osobu, ktorá dala pokyn


Leave a Comment Zanechať komentár




Leave your Zanechajte svoj friendly priateľský comment here. komentár tu.

If you have a computer help question, Ak máte počítač pomôže otázku, click here to leave it on the forums kliknite tu nechať to na fórach instead. miesto.

Note: Your comment may not show up immediately on the site. Poznámka: Váš komentár sa nezobrazia okamžite na mieste.

Our Friends Naši priatelia
Getting Started Začíname


About How-To Geek O tom, ako-To Geek
What Is That Process? Čo je to za proces?
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. Copyright © 2006-2009 HowToGeek.com. All Rights Reserved. All Rights Reserved.