Click here to Skip to main content
15,886,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one function in Form1 .
C#
public void aggregation_get()
{
}


how can i call function to form2

What I have tried:

C#
private void button6_Click(object sender, EventArgs e)
        {
         Form1.aggregation_get()
        }

But Its not working . In Form1, should i declare form2 variable at class level
Posted
Updated 31-May-21 8:30am
v2
Comments
Daniele Rota Nodari 31-May-21 10:28am    
What is the relation between the two forms? do them exist independently or one is open as consequence of an action on the other form?
PIEBALDconsult 31-May-21 12:07pm    
Design it correctly from the start. Don't put such functionality in the forms at all; put it in a separate common class. Always think in layers rather than monolithic classes.

You shouldn't be doing that at all. You're tying the two forms together so that one cannot exist without the other.

If what you're trying to do is manipulate your data model, this is a sign that you need to rethink your data model.

If you don't have a data model, you need to rethink you're approach to it.

If what you need to happen is UI related on Form1, Form2 can expose an event Form1 can subscribe to to receive the notification that something needs to happen. It's then up to Form1 to decide how to respond to that notification. This completely separates Form1 from Form2 so they can be used independently.
 
Share this answer
 
Comments
BillWoodruff 1-Jun-21 13:54pm    
+5
This reply is meant to complement the excellent advice given you by Dave K. Be sure and up-vote his solution.

Consider two scenarios:

1) notification: an event on one Form notifies another Form.

2) notification and transfer of information or data: one Form requests information from another Form; or, one Form sends information to another Form.

The technique I think achieves the maximum de-coupling (isolation, separation of concerns) between Forms is to define a publicly exposed Func or Action (both are convenient ways to define a Delegate/EventHandler).

Do study Action [^] and Func [^] now. For a deeper overview: [^]

When the instance of the Form with that Func or Action invokes it (runs it), it will broadcast to all of its subscribers. The Action can transfer whatever, but its invocation returns no result. The Func returns a result tom the calling code.

Assume Form1 here is the Main Form of a WinForm app; Form 2 is a Form that Form1 will create and use.

1) Notification
C#
 // in Form1

private Form2 form2;

private void Form1_Load(object sender, System.EventArgs e)
{
     form2 = new Form2();

     form2.SendData += 
}

private void Form2NotificationReceiver(DateTime dt, string msg)
{
     // do something with information   
}

 // in Form 2

public Action<DateTime, string> SendData { set; get;}

private void NotifyButton_Click(object sender, System.EventArgs e)
{
    string msg = "NotifyButton Clicked";

    // always check for null
    if (SendData != null)
    {
        SendData(DateTime.UtcNow, msg );
    }

    // alternative using c# 6 null propagation
    // SendData?.Invoke(DateTime.UtcNow, msg);
}
Summary: an Action (delegate) is inserted into Form2 by Form1' at run-time, a Button click in Form2 executes the Action which calls/invokes a handler in Form1.

Form2 "knows nothing" about Form1, it can't access Formq in any way. Form1's access to Form2 is a one-way transmission link.

Will round this out with a Func example tomorrow ... energy allowing.
 
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