Click here to Skip to main content
Licence CPOL
First Posted 24 Oct 2010
Views 26,838
Bookmarked 16 times

Sharing Data between Windows Forms

By | 17 Dec 2010 | Article
How to share data between Windows Forms in a program without database or files

Introduction

It sometimes comes up that data is entered from a form and it's being used by other forms. For example, you click a button on an "Invoice" form to open "Select customer" form, then you will select a customer and close the form, therefore, you can use customer's name from "Invoice" form or even other forms.

In this article, we will consider:

  • Sharing data between Windows Forms
  • A problem and its solution

Sharing Data

Suppose our program contains two forms, one of them is named "InvoiceForm" and other one is "SelectCustomerForm". We place a button on "InvoiceForm" whose name is "SelectButton", and we place a button on "SelectCustomerForm" its name is "Ok".

We are adding a new class under the title of "Customer" that the fields contain customer description. Example:

public class Customer
{
    private string _FirstName;
    public string FirstName
    {
        get
        {
            return _FirstName;
        }
        set
        {
           _FirstName = value;
        }
    }
    private string _Surname;
    public string Surname
    {
        get
        {
            return _Surname;
        }
        set
        {
            _Surname = value;
        }
    }
}

We're adding another class and name it, "SharedData" with a "Customer" type field in title of "InvoiceCustomer". Example:

public class SharedData
{
    public static Customer InvoiceCoustomer;
}

We are handling the click event of "Select button" from "SelectCustomerForm" and typing necessary codes in it, and initializing the fields of "InvoiceCustomer" with Customer data. Those data can be read from a database or every resource. Example:

public partial class SelectCustomerForm : Form
{
    public SelectCustomerForm()
    {
        InitializeComponent();
    }
    private void Ok_Click(object sender, EventArgs e)
    {
        Customer customer = new Customer();
        customer.FirstName = "John";
        customer.Surname = "Smith";
        SharedData.InvoiceCoustomer = customer;
     
  this.Close();
    }
}

Now, "InvoiceCustomer" has the necessary data and we gain access from every form in our program.

We're handling the click event of "Ok button" from "InvoiceForm" and typing requisite codes in it. It's necessary to read "InvoceCustomer" containing, for example:

public partial class InvoiceForm : Form
{
    public InvoiceForm()
    {
        InitializeComponent();
    }
    private void SelectButton_Click(object sender, EventArgs e)
    {
        SelectCustomerForm selectCustomerForm = new SelectCustomerForm();
        
        selectCustomerForm.ShowDialog();
        CustomerFullNameText.Text = SharedData.InvoiceCoustomer.FirstName + 
        " " + SharedData.InvoiceCoustomer.Surname;
    }
}

You see, data is shared as easily as winking!

A Problem and Its Solution

The problem is that if we come back from "SelectCustomerForm" empty handed, in other words, if we don't select any customer from "SelectCustomerForm", then we confronted a Run-Time Error at first time, because "SharedData.InvoiceForm" is still unassigned.

For resolving this problem, we can use SEH, in "InvoiceForm", Example:

     private void SelectButton_Click(object sender, EventArgs e)
    {
        SelectCustomerForm selectCustomerForm = new SelectCustomerForm();
        
        selectCustomerForm.ShowDialog();
        // SEH  :
        try
        {
            string test = SharedData.InvoiceCoustomer.FirstName;
        }
        catch
        {
            SharedData.InvoiceCoustomer = new Customer();
        }
        //.
        CustomerFullNameText.Text = SharedData.InvoiceCoustomer.FirstName + 
	" " + SharedData.InvoiceCoustomer.Surname; 
    }
}

It tests "InvoiceCustomer" before using. If the object is unassigned, it'll be initialized before using.

Of course, we can use DialogResult instead of SEH.

When a user wants to select a new customer after selecting another one, if the user cancels "SelectCustomerForm" without choosing any new customer, the previous data won't be lost and it is a preference.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shahin Khorshidnia

Software Developer

Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member

Microsoft Certified Technology Specialist (MCTS)
 

"شاهین خورشیدنیا"

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks for the suggestion PinmemberMD9096:27 23 Feb '12  
GeneralRe: Thanks for the suggestion PinmemberShahin Khorshidnia12:54 24 Feb '12  
GeneralMy vote of 1 Pinmembersnortle3:38 3 Mar '11  
GeneralRe: My vote of 1 PinmemberShahin Khorshidnia10:13 4 Feb '12  
GeneralRe: My vote of 1 PinmvpMark Nischalke1:46 13 Feb '12  
GeneralThanks PinmemberShahin Khorshidnia4:24 13 Feb '12  
GeneralMy vote of 1 PinmemberRajesh Kumar Chekuri1:59 10 Feb '11  
GeneralMy vote of 1 PinmemberManfred R. Bihy15:12 13 Jan '11  
GeneralRe: My vote of 1 PinPopularmemberShahin Khorshidnia10:14 4 Feb '12  
GeneralMy vote of 1 PinmemberMarcus Kramer11:38 4 Jan '11  
Generalwell tried PinmemberPranay Rana22:17 19 Dec '10  
GeneralRe: well tried PinmemberFabio Franco3:21 27 Jan '11  
GeneralMy vote of 1 PinmemberRyanEK15:33 19 Dec '10  
GeneralMy vote of 1 PinmemberHristo Bojilov23:54 18 Dec '10  
GeneralMy vote of 4 PinPopularmemberRusselSSC23:10 18 Dec '10  
GeneralRe: My vote of 4 PinmemberShahin Khorshidnia11:55 19 Dec '10  
GeneralRe: My vote of 4 PinPopularmemberRusselSSC23:45 19 Dec '10  
GeneralRe: My vote of 4 PinmemberShahin Khorshidnia8:09 20 Dec '10  
GeneralMy vote of 5 Pinmemberjahan20105:39 18 Dec '10  
GeneralMy vote of 1 PinmemberDaveAuld20:34 17 Dec '10  
GeneralMy vote of 1 PinPopularmvpMark Nischalke17:54 17 Dec '10  
GeneralRe: My vote of 1 PinmemberShahin Khorshidnia2:21 18 Dec '10  
GeneralRe: My vote of 1 PinmvpMark Nischalke3:05 18 Dec '10  
GeneralRe: My vote of 1 PinmemberShahin Khorshidnia11:22 18 Dec '10  
GeneralRe: My vote of 1 PinmvpMark Nischalke11:46 18 Dec '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 17 Dec 2010
Article Copyright 2010 by Shahin Khorshidnia
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid