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

Calling parent form functions from a user control

By , 22 Apr 2008
Rate this:
Please Sign up or sign in to vote.

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() 

    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() 

public Delegate userControlPointer; 
public Delegate userFunctionPointer; 

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

private void txtUserControl_TextChanged(object sender, EventArgs e) 


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

About the Author


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
Web04 | 2.8.140415.2 | Last Updated 22 Apr 2008
Article Copyright 2008 by AlG
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid