Click here to Skip to main content
Click here to Skip to main content

Calling parent form functions from a user control

, 22 Apr 2008
Rate this:
Please Sign up or sign in to vote.
The Button inside the User Control raises the Button OnClick event on the form and typing inside the User Control TextBox replicates the text inside the Form’s TextBox.

User control usage usually requires developing communication between parent form and a user control. Accessing user controls can be easily done through their properties and methods. Calling parent form functions from a user control is not so trivial. The sample below shows how to raise parent form event and call parent form function from within the user control.

The Button inside the User Control raises the Button OnClick event on the form and typing inside the User Control TextBox replicates the text inside the Form’s TextBox. The code seems self descriptive.

Parent form code:

public Form1() 
{ 
    InitializeComponent(); 

    formControlPointer += new controlcall(btnHello_Click); 
    ucMyControl.userControlPointer = formControlPointer; 

    formFunctionPointer += new functioncall(Replicate); 

    ucMyControl.userFunctionPointer = formFunctionPointer; 
} 

public delegate void controlcall(object sender, EventArgs e);
public delegate void functioncall(string message); 

private event controlcall formControlPointer;
private event functioncall formFunctionPointer; 

private void btnHello_Click(object sender, EventArgs e) 
{ 
    MessageBox.Show("You typed: " + txtMessage.Text); 
} 

private void Replicate(string message) 
{ 
    txtReplicate.Text = message; 
} 

User control code:

public UserControl1() 
{ 
    InitializeComponent(); 
} 

public Delegate userControlPointer; 
public Delegate userFunctionPointer; 

private void btnHello_Click(object sender, EventArgs e) 
{ 
    object[] arr = { null, null }; 
    userControlPointer.DynamicInvoke(arr); 
} 

private void txtUserControl_TextChanged(object sender, EventArgs e) 
{ 
    userFunctionPointer.DynamicInvoke(txtUserControl.Text); 
} 

License

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

Share

About the Author

AlG

United States United States
No Biography provided

Comments and Discussions

 
QuestionIn VB.Net Calling parent form functions from a user control Pinmembervino123421-Jun-11 23:17 
GeneralThanks Pinmembermatrixology3-May-11 3:42 
General'Delegate' is a 'namespace' but is used like a 'type' Pinmemberveronicam@warnerpacific.com12-Oct-10 7:15 
GeneralMy vote of 5 PinmemberDarrinLynn4-Oct-10 5:56 
GeneralMy vote of 5 PinmemberMember 43205166-Jul-10 6:15 
GeneralMy vote of 5 Pinmemberbinbel4-Jul-10 20:50 
Generalmvc... PinmemberChris Richner22-Apr-08 22:49 
GeneralYeah, but.... PinmvpChristian Graus22-Apr-08 12:19 
GeneralRe: Yeah, but.... Pinmember leppie 22-Apr-08 22:06 
GeneralRe: Yeah, but.... PinmvpChristian Graus22-Apr-08 22:22 
GeneralRe: Yeah, but.... Pinmemberjohannesnestler23-Apr-08 4:22 
AnswerRe: Use FindForm method PinmemberDan Randolph19-Aug-08 9:40 
QuestionRe: Use FindForm method Pinmemberstixoffire2-Jul-09 1:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 22 Apr 2008
Article Copyright 2008 by AlG
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid