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 Mainīgie starp 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. Kad esat programmu Windows Forms pieteikumu, jums vienmēr nepieciešams, lai saņemtu mainīgos lielumus no otrās formas logu, piemēram, iespējas formu vai popup meklēšana 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. Lielākā daļa, kas tur gidi teiks, ka Jums ir atvērt otru veidlapu ar ShowDialog (), kas bloķē lietotājam darīt jebko, kamēr viņi ir slēgts otru veidlapu logu. This won't work very well for a find/replace dialog, for instance. Tas nedarbosies ļoti labi atrast / Replace Dialog, piemēram. It also won't work very well for custom drawn popup forms. Tas nestrādās arī ļoti labi pasūtījuma izstrādāts popup veidā.

The quick way to pass variables between the forms is using Delegates. Ātrs veids, lai nodotu mainīgos starp formām izmanto delegātiem. You can set an eventhandler for the Closing event of the second form, and handle the event in the first form. Jūs varat uzstādīt eventhandler par noslēguma pasākumā otrais formu un rokturi notikums pirmajā formā. This allows you to capture variables before the second form window has closed. Tas ļauj jums uztveršanas mainīgos pirms otrā veidā logs ir aizvērts.

For this exercise, we're going to assume that we have two forms: Attiecībā uz šo uzdevumu, mēs spēsim pieņemt, ka mums ir divas formas:

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. Mēs ejam tālāk pieņemam, ka mums esat noklikšķinājis uz kaut kādas pogas, kas atver OptionsForm ar Šovs () metodes izsaukuma. Now let's take a look at the magic: Tagad pieņemsim ņemt pie burvju meklēt:

……. ... .... snip……. drēbnieks ... ....

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) privātā spēkā theform_Closing (objekts sūtītājs, CancelEventArgs e)
{ (

OptionsForm theform = (OptionsForm)sender; OptionsForm theform = (OptionsForm) sūtītāja;

  // Grab the variable from the options form. / / Grab mainīgo no iespējām formu. The options form should set this variable before it closes, and the variable should be marked as public. Iespējas forma jānosaka šo mainīgo pirms to aizver, un mainīgā jāmarķē kā valsts.
string localvar = theform.thestringvariable; string localvar = theform.thestringvariable;

} )

That's all there is to it. Tas viss notiek ar to.

This article was originally written on 09/20/06 Tagged with: Šis pants tika sākotnēji rakstīts uz 09/20/06 tagged with: Programming Programmēšana

Daily Email Updates Daily Email Updates

You can get our how-to articles in your inbox each day for free. Jūs varat saņemt mūsu how-to rakstus savā pastkastītē katru dienu par brīvu. Just enter your name and email below: Vienkārši ievadiet jūsu vārdu un e-pasta zemāk:


Name: Vārds:
Email: E-pasts:

Comments (2) Komentāri (2)

  1. cypher šifrs

    I guess this works better : Es domāju, tas darbojas labāk:
    theform.FormClosing += new FormClosingEventHandler(theform_Closing); theform.FormClosing + = new FormClosingEventHandler (theform_Closing);

    Thanks it helped me a lot to figure it out anyway. Paldies tas palīdzēja man daudz skaitlis it out anyway.

  2. Rama krishna Rama Krishna

    it worked for me as well in great deal tā strādāja man arī ir ļoti daudz

    A big thanks for the person who has given the hint Liels paldies par personu, kas ir mājiens


Our Friends Our Friends
Getting Started Getting Started


About How-To Geek Par How-To Geek
What Is That Process? Kas ir šis process?
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.