Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on payroll application,In payroll module Advance Entry form is there in that one button named (Employee List), when user click on Employee list button one form will come, I want to call that form by passing query like "Advance Entry" because this employee list form will be used in other forms, so I don't want to create particular form....Any suggestions will be appreciated...
Posted
Updated 18-Mar-15 22:22pm
v2
Comments
Mohammad Reza Valadkhani 19-Mar-15 3:58am    
do you want to send a variable from one windows form to the other form?
[no name] 19-Mar-15 4:16am    
No I don't want to pass variable, Actually I want to send query from other forms to derived form as named(Employeelist), as per the query record will fetch and fetched record will go back to previous form.

1 solution

One (of many) ways you could use a common Form in the Main Form, and other secondary Forms, in a Windows Forms Apps is:

1. Define a public static property in the Main Form of Type 'CommonForm:

public static CommonForm TheCommonForm { private set; get; }

2. Assign an instance of 'CommonForm to the static public property ... in the Load EventHandler of the Main Form, for example:
C#
private void MainForm_Load(object sender, EventArgs e)
{
    TheCommonForm = new CommonForm();
}
3. When any Form needs to display the 'CommonForm"

MainForm.TheCommonForm.Show();

Then, of course, you will need to consider what elements/data/Controls you need to expose in the instance of 'CommonForm so that any Form which displays it can access what it needs to access.

If you need to pass custom data into the instance of 'CommonForm when you display it, you can define a custom 'Show operator like this:
C#
// in 'CommonForm
private List<string> Data { set; get; }

public void Show(List<string> data = null)
{
    if (data != null) Data = data;

    base.Show();
}
In this brief example, any Form that displays the 'CommonForm can pass a List of string Type data in for internal use in 'CommonForm: if no data is passed in (Show called with 'null, or an empty List), then the internal 'Data list (if any) is left as-is.

If you wished the 'CommonForm to execute a query and then return query results to another Form, or Forms, that can be handled a number of ways, incluiding:

1. have the 'CommonForm define an Event to which any interested Form can subscribe, with that Event returning query results in a custom Class inheriting from EventArgument, or other data structure.

2. pass a data structure by reference to the 'CommonForm, and use the modified data structure after it is handled by 'CommonForm.

Again, there are many ways you might go about this, and without knowing your scenario in more detail, the example here cannot claim to be "best practice."
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900