How to access a function / control of a parent class from a child user control loaded in a panel

I have a main form that contains a panel that loads different Usercontrols into the panel. Now I need to access the functions in the main form from UserControl. Below I have given my code;

This is my main Windows form class:

public partial class Form1 : Form { public Form1() { InitializeComponent(); loadlogin(); } private void loadlogin() { login log = new login(); mainPannel.Controls.Clear(); mainPannel.Controls.Add(log); } public void mytest() { } } 

As you can see, I load usercontrol on mainPannel. Now let's look at usercontrol:

 public partial class login : UserControl { string MyConString = ""; public login() { InitializeComponent(); } public void button1_Click_1(object sender, EventArgs e) { //I want to call the parent function mytest(); HOW???? } } 

I want to access mytest () function by pressing button1. I tried many other solutions, but I'm still confused. I used:

  Form my = this.FindForm(); my.Text = "asdasfasf"; 

This gives me a reference to the parent, and I can change the text of the form, but how can I access its functions ???

+4
source share
3 answers

This can help:

 public partial class Form1 : Form { //Other codes private void loadlogin() { login log = new login(this); //CHANGE HERE mainPannel.Controls.Clear(); mainPannel.Controls.Add(log); } //Other codes } 

and

 public partial class login : UserControl { Form1 _parent; //ADD THIS public login(Form1 parent) { InitializeComponent(); this._parent = parent; //ADD THIS } public void button1_Click_1(object sender, EventArgs e) { this._parent.mytest(); //CALL WHAT YOU WANT } } 
+4
source

Well, the above answer will work, but it is not good coding practice to pass a user control to the “parent form”. UserControls must be located in a separate project from your forms project, and your form project must reference your management project in order to get visibility for them. Suppose, for example, that you want this UserControl to be nested in another UserControl at some point. Your code no longer works without overloading the constructor. The best solution would be to use an event. I implemented the implementation using CustomEventArg. By doing this, your UserControl login can sit on anything and still work. If the functionality for changing the text of the parents is not specified, simply do not register in this event. Here is the code, hope this helps someone.

Here is the form code:

 public Form1() { InitializeComponent(); loadlogin(); } private void loadlogin() { login log = new login(); //Registers the UserControl event log.changeParentTextWithCustomEvent += new EventHandler<TextChangedEventArgs>(log_changeParentTextWithCustomEvent); mainPannel.Controls.Clear(); mainPannel.Controls.Add(log); } private void log_changeParentTextWithCustomEvent(object sender, TextChangedEventArgs e) { this.Text = e.Text; } 

Here is the UserControl "login" code (in my test solution, just user control with a button)

 public partial class login : UserControl { //Declare Event with CustomArgs public event EventHandler<TextChangedEventArgs> changeParentTextWithCustomEvent; private String customEventText = "Custom Events FTW!!!"; public login() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //Check to see if the event is registered... //If not then do not fire event if (changeParentTextWithCustomEvent != null) { changeParentTextWithCustomEvent(sender, new TextChangedEventArgs(customEventText)); } } } 

And finally, the CustomEventArgs class:

 public class TextChangedEventArgs : EventArgs { private String text; //Did not implement a "Set" so that the only way to give it the Text value is in the constructor public String Text { get { return text; } } public TextChangedEventArgs(String theText) : base() { text = theText; } } 

Writing your controls in this way, without being visible to the forms, will allow your UserControls to be fully reused and not tied to any types or parents. Just define the behavior that UserControl can initiate and register when necessary. I know this is an old post, but hopefully this helps someone better write UserControls.

+3
source

From your UserControl:

 ((Form1)Parent).mytest(); 
0
source

All Articles