Home > Ask the Microsoft .Net Development Experts > C# Development Questions & Answers > Passing data between forms
Ask The Win Development Expert: Questions & Answers
EMAIL THIS

Passing data between forms

Mark Belles EXPERT RESPONSE FROM: Mark Belles

Pose a Question
Other Win Development Categories
Meet all Win Development Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 31 January 2005
If I were to write a program, what methods would I use to send information from the main form of the application to a modal popup form and then pass any changes to the data back to the main form when the popup form was closed? This was an interview question that I faced and was not sure how to answer. What would your reply be?


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
C# Development
Top C# expert advice
Using LoadRunner for testing in .NET
Take care with InitializeComponent
GroupBox inheritance
Escape from mailto:
Windows Forms: Overriding the OnClosing method
Can I run a .NET app without installing .NET runtime?
Opening a link with parameters to a new window
Is it possible to open a child form in modal type?
Painting in the non-client area

C# programming language
Mono 2.0 moves .NET apps to Linux - includes migration analyzer
LINQ beyond queries: Strong-typed refection
Assembly versioning in the .NET Framework 2.0
Book excerpt: Creating graphical output using the .NET Compact Framework
Visual Studio 2008 Learning Guide: C# 3.0
Simonyi firm to address divide between domain experts and developers
On Extension Methods in C# and .NET Framework 3.5
Generate RSA public and private keys, export to XML
Book excerpt: Upgrading to Visual Studio 2005
Learning .NET: Tips for getting started with .NET development

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
C#  (SearchWinDevelopment.com)
GLib  (SearchWinDevelopment.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


I would create classes or structures to represent the data being passed, and then create a constructor in the Form class for the Form to be shown that would allow me to pass the information to the class at creation time. I would also expose the information from the class using a property to allow me to retrieve the results after the Form closed. This is a very simple example to help you focus on the ideas at play. How to pass information between various classes and retrieve the information later.

Regards,
Mark

public class Startup
{ 
 [STAThread()]
 public static void Main(string[] args)
 {
  // create some contact info to be edited
  ContactInformation info = new ContactInformation(@"Mark Belles", @"555 C# Drive"); 
  
  // create a form to display and allow the info to be edited
  ContactInformationForm form = new ContactInformationForm(info);
  
  // show the form modally
  if (form.ShowDialog() == DialogResult.OK)
  {
   // retrieve the updated info, and do something with it, perhaps save it somewhere...
   info = form.ContactInformation;
   
   // display the contact information...
   Debug.WriteLine(info);
  }  
 }
}

public class ContactInformation
{
 protected string _name;
 protected string _address;
 
 public ContactInformation(string name, string address)
 {
  _name = name;
  _address = address;
 }
 
 public string Name
 {
  get
  {
   return _name;
  }
  set
  {
   _name = value;
  }
 }
 
 public string Address
 {
  get
  { 
   return _address;
  }
  set
  {
   _address = value;
  }
 }
 
 public override string ToString()
 {
  return string.Format("ContactInformation: '{0}', '{1}'", _name, _address);
 }
}

public class ContactInformationForm : Form
{
 protected ContactInformation _info;
  
 public ContactInformationForm(ContactInformation info)
 {
  _info = info;
  // display the information for editing
 }
 
 public ContactInformation ContactInformation
 {
  get
  {
   return _info;
  }
 }
}




Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Windows Development - White Papers, News and Expert Advice
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts