Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have a user control in windows form application i need to raise diffrene methode based on the the parent form called my control have two parent form...........
how can i do pls help me
Posted

Don't. Instead, do it the other way: create an event in your child form, which the parent form subscribes to. That way, the child does not need to know anything about the existence of the parent - it just signals "I have data!" and the parent will process it as required if it is interested.


In the child form:

C#
public partial class frmChild : Form
   {
   // Signal data change happened
   public event EventHandler Changed;

   protected virtual void OnChanged(EventArgs e)
      {
      EventHandler eh = Changed;
      if (eh != null)
         {
         eh(this, e);
         }
      }

   private void DoSomethingToChangeData()
      {
      OnChanged(null);
      }
   }


----- The asign to eh is in case the handler changes between null check
----- and exec.
----- (unlikely, but possible)

----- The null check is to ensure there is a handler. If not, better to
----- ignore it gracefully, than to rely on the thrown exception
----- (NullReferenceException)

In the Parent form:
C#
private void ShowChildForm()
    {
    // Link in his event so if it changes, we detect it.
    frmChild fd = new frmChild();
    fd.Change += new frmChange.ChangeHandler(Changed);
    fd.ShowDialog();
    }

//
// Fired when the data is changed at a lower level.
//
private void Changed(object sender, EventArgs e)
    {
    }
 
Share this answer
 
Keep One Static Variable (ParentName) in Common File like common.cs

Example

Public static string ParentName="";

On Form_Load event of your parent form1

C#
Common.ParentName="Form1";


On Form_Load event of your parent form2
C#
Common.ParentName="Form2";


In Usercontrol Before calling a method check
switch(Common.ParentName)
{
case "Form1":
//Call Method1;
break;
Case "Form2":
Call Method1;
break;
}

Hope this helps!
 
Share this answer
 

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