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();
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.