Pass Variables between Windows Forms Windows without ShowDialog() Pass Kintamieji tarp Windows Forms Windows be 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. Jei esate programavimo Windows Forms taikymo, jums visada reikia gauti kintamuosius antra forma langą, pavyzdžiui, galimybės formą arba iššokantį paieškos eilutę.
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. Dauguma ten gidas jums pasakys, kad jūs turite atidaryti antrą formą ShowDialog (), kuri blokuoja vartotojui atlikti kažką dar, kol jie baigė antrąjį formos langas. This won't work very well for a find/replace dialog, for instance. Tai nėra labai gerai dirba už Find / Replace Dialog, pvz. It also won't work very well for custom drawn popup forms. Ji taip pat nėra labai gerai dirba pagal užsakymą parengtos iškylančių langų formos.
The quick way to pass variables between the forms is using Delegates. Greitai perduoti tarp kintamųjų formas naudoja delegatų. You can set an eventhandler for the Closing event of the second form, and handle the event in the first form. Galite nustatyti, kad už baigiamąjį renginį antra formą eventhandler ir tvarkyti visų pirma forma įvykis. This allows you to capture variables before the second form window has closed. Tai leidžia užfiksuoti kintamieji iki antros forma langas buvo uždarytas.
For this exercise, we're going to assume that we have two forms: Už šį darbą, mes ketiname daryti prielaidą, kad turime dviem būdais:
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. Mes ketiname ir toliau manyti, kad mes kai paspaudėte mygtuką, rūšiuoti, kad atidaro su Parodyti OptionsForm () metodas skambinti. Now let's take a look at the magic: Dabar tegul imasi ne magija ieškoti:
……. ... .... snip……. Snip ... ....
OptionsForm theform = new OptionsForm(); OptionsForm theForm = nauji OptionsForm ();
theform.Closing += new CancelEventHandler(theform_Closing); theform.Closing + = nauji 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) sender;
// Grab the variable from the options form. / / Grab iš variantų forma kintamasis. The options form should set this variable before it closes, and the variable should be marked as public. Galimybes formą turėtų nustatyti šį kintamąjį prieš jį uždaro, o kintamasis turėtų būti pažymėta kaip vieša.
string localvar = theform.thestringvariable; string localvar = theform.thestringvariable;} )
That's all there is to it. Tai viskas, nėra prie jo.

Daily Email Updates Dienos paštas Atnaujinimai
You can get our how-to articles in your inbox each day for free. Galite gauti mūsų kaip prie straipsnių į Jūsų pašto dėžutę kasdien nemokamai. Just enter your name and email below: Tiesiog įveskite vardą ir elektroninio pašto adresą žemiau:



I guess this works better : Manau, tai veikia geriau:
theform.FormClosing += new FormClosingEventHandler(theform_Closing); theform.FormClosing + = nauji FormClosingEventHandler (theform_Closing);
Thanks it helped me a lot to figure it out anyway. Dėkojame, kad padėjo man daug pav it out anyway.
it worked for me as well in great deal ji dirbo už mane, taip pat ir daug
A big thanks for the person who has given the hint Didelis ačiū už asmenį, kuris davė užuominą