Welcome, Guest: Join Kenyans247 Log In!

Stats: 296 Members, 14,138 topics and 65 posts Date:  Tuesday 01 December 2020 at 08:33 AM
Popular & Trending NewestFeatured Links ClassifiedDirectoryLivescoresLive Kenyan TVLive Kenyan Radio Follow Us Like Us

About Us Terms & Conditions Privacy Policy FAQ's Disclaimer Copyright Contact Us Advertise With Us Download K247 APP Marketplace Moderator Policy We Are Hiring Kenya Radio and TV Stations Listen and Watch Live Letter From the C.E.O Kenya Radio Stations Live Kenya TV Stations Live Kenya - Newspapers and News Sites and popular blog Forum Rules

Pass Variables between Windows Forms Windows without ShowDialog()

Kenyans247 / Science/Technology / Computers / Pass Variables between Windows Forms Windows without ShowDialog()

72 people viewed

Pass Variables Between Windows Forms Windows Without Showdialog()
(Go Down) (» Reply topic)
Pass Variables between Windows Forms Windows without ShowDialog() by Kenyans247(1): Sun 22, March, 2020 10:56am
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.

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. This won’t work very well for a find/replace dialog, for instance. It also won’t work very well for custom drawn popup forms.

The quick way to pass variables between the forms is using Delegates. You can set an eventhandler for the Closing event of the second form, and handle the event in the first form. This allows you to capture variables before the second form window has closed.

For this exercise, we’re going to assume that we have two forms:

MainForm

OptionsForm

We’re going to further assume that we’ve clicked some sort of button that opens the OptionsForm with a Show() method call. Now let’s take a look at the magic:

……. snip…….

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

}

private void theform_Closing(object sender, CancelEventArgs e)
{

OptionsForm theform = (OptionsForm)sender;

// Grab the variable from the options form. The options form should set this variable before it closes, and the variable should be marked as public.
string localvar = theform.thestringvariable;

}



That’s all there is to it.

0 Shares 0 Like •

(» Go Up)

You must Sign in or Sign Up to post content

For you to post content on this website you have to log in or register, it only take few minute to create account

Go Up

Recommended for you

Pass Variables Between Windows Forms Windows Without Showdialog()

Currently: 1 guest(s) reading this article

Sections: Counties, Naija Forums, Africa, Environment, Huduma Centre, Innovation, Education, Technology Market, Phones, Programing, Music/Radio, Lifestyle, Literature, TV/Movies, Events, Ugandan Forum, South Sudan Forum, Tanzania Forum, Burundi Forum, Rwanda Forum,

About Us | Terms & Conditions | Privacy Policy | FAQ's | Disclaimer | Copyright | Contact Us | Advertise With Us | Download K247 APP | Marketplace | Moderator Policy | We Are Hiring | Kenya Radio and TV Stations Listen and Watch Live | Letter From the C.E.O | Kenya Radio Stations Live | Kenya TV Stations Live | Kenya - Newspapers and News Sites and popular blog | Forum Rules | Featured LinksFollow Us on TwitterLike Us on Facebook

Kenyans247 - Copyright © 2019 - 2020 Sande Kennedy. All rights reserved. See How To Advertise.
Disclaimer: Every Kenyans247 member is solely responsible for anything that he/she posts or uploads on Kenyans247.