Subscribe to How-To Geek Mag-subscribe sa Paano-Upang Geek

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() Pass Variable sa pagitan ng Windows Forms Windows walang 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. Kapag kayo ay isang programa sa aplikasyon ng Windows Forms, ikaw ay walang paltos na kailangan upang makakuha ng mga variable mula sa isang window ng pangalawang form, tulad ng isang opsyon sa form na ito o ang popup paghahanap prompt.

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. Karamihan ng mga gabay sa labas may sasabihin sa iyo na kailangan mong buksan ang ikalawang form kasama ShowDialog (), kung saan ang mga bloke ng user mula sa paggawa ng anumang bagay hanggang sa sila na sarado na ang ikalawang window na form. This won't work very well for a find/replace dialog, for instance. Ito ay hindi gagana tunay mabuti para sa isang mahanap / palitan ng dialog, halimbawa. It also won't work very well for custom drawn popup forms. Ito rin ay hindi gagana lutung para sa pasadyang mga diwa popup anyo.

The quick way to pass variables between the forms is using Delegates. Ang mabilis na paraan upang ipasa ang mga variable sa pagitan ng mga forms na ginagamit ng delegado. You can set an eventhandler for the Closing event of the second form, and handle the event in the first form. Maaari kang magtakda ng isang eventhandler para sa pagsara ng kaganapan ng ikalawang form, at panghawakan ang mga kaganapan sa unang form. This allows you to capture variables before the second form window has closed. Ito ay nagpapahintulot sa iyo na makuha ang mga variable na bago ang ikalawang window ng form ay sarado.

For this exercise, we're going to assume that we have two forms: Para sa mga exercise na ito, kami ay pagpunta sa ipinapalagay na kami ay may dalawang paraan:

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. Kami ay pupunta sa karagdagang ipinapalagay na kami nag-click ang ilang uri ng mga butones na bubukas sa OptionsForm may Ipakita ang mga () na paraan ng tawag. Now let's take a look at the magic: Ngayon natin tingnan ang magic:

……. ... .... snip……. gupitin ... ....

OptionsForm theform = new OptionsForm(); OptionsForm theform = bagong OptionsForm ();
theform.Closing += new CancelEventHandler(theform_Closing); theform.Closing = + bagong CancelEventHandler (theform_Closing);
theform.Show(); theform.Show ();

} )

private void theform_Closing(object sender, CancelEventArgs e) magpawalang-bisa sa pribadong theform_Closing (object sender, CancelEventArgs e)
{ (

OptionsForm theform = (OptionsForm)sender; OptionsForm theform = (OptionsForm) nagpadala;

  // Grab the variable from the options form. / / Grab ng variable mula sa form na pagpipilian. The options form should set this variable before it closes, and the variable should be marked as public. Ang mga opsyon ng form ay dapat na itakda ang variable bago ito magsara, at ang mga variable ay dapat na minarkahan bilang publiko.
string localvar = theform.thestringvariable; string localvar = theform.thestringvariable;

} )

That's all there is to it. Iyan na ang lahat diyan ay sa ito.

This article was originally written on 09/20/06 Tagged with: Ang artikulong ito ay orihinal na isinulat sa 09/20/06 Tagged with: Programming Programming

Daily Email Updates Araw-araw na Updates Email

You can get our how-to articles in your inbox each day for free. Maaari kang makakuha ng aming kung-paano na mga artikulo sa iyong inbox sa bawat araw para sa libre. Just enter your name and email below: Ilagay lamang ang inyong pangalan at email sa ibaba:


Name: Pangalan:
Email: Email:

Comments (2) Comments (2)

  1. cypher kodigong lihim

    I guess this works better : Ako hulaan ito ay gumagawa mas mahusay na:
    theform.FormClosing += new FormClosingEventHandler(theform_Closing); theform.FormClosing = + bagong FormClosingEventHandler (theform_Closing);

    Thanks it helped me a lot to figure it out anyway. Thanks ito nakatulong sa akin ng isang pulutong sa tayahin ito pa rin.

  2. Rama krishna Rama Krisna

    it worked for me as well in great deal ito ay nagtrabaho para sa akin pati na rin sa mahusay na pakikitungo

    A big thanks for the person who has given the hint Isang malaking salamat para sa mga tao na binigyan iparamdam ang


Our Friends Ang aming mga Friends
Getting Started Pagsisimula


About How-To Geek Tungkol sa Paano-Upang Geek
What Is That Process? Ano ba ang Proseso Iyon?
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.