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.

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:



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