Click here to Skip to main content
15,886,258 members
Articles / Desktop Programming / Windows Forms
Article

Calling parent form functions from a user control

Rate me:
Please Sign up or sign in to vote.
4.00/5 (18 votes)
22 Apr 2008CPOL 110.6K   2.6K   26   14
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.

Image 1

Parent form code:

C#
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:

C#
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)


Written By
AlG
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
awaisshabir11-Sep-16 5:20
awaisshabir11-Sep-16 5:20 
QuestionIn VB.Net Calling parent form functions from a user control Pin
vino123421-Jun-11 23:17
vino123421-Jun-11 23:17 
GeneralThanks Pin
matrixology3-May-11 3:42
matrixology3-May-11 3:42 
General'Delegate' is a 'namespace' but is used like a 'type' Pin
WarnerP12-Oct-10 7:15
WarnerP12-Oct-10 7:15 
GeneralMy vote of 5 Pin
DarrinLynn4-Oct-10 5:56
DarrinLynn4-Oct-10 5:56 
GeneralMy vote of 5 Pin
User 43173906-Jul-10 6:15
User 43173906-Jul-10 6:15 
GeneralMy vote of 5 Pin
binbel4-Jul-10 20:50
binbel4-Jul-10 20:50 
Generalmvc... Pin
Chris Richner22-Apr-08 22:49
Chris Richner22-Apr-08 22:49 
GeneralYeah, but.... Pin
Christian Graus22-Apr-08 12:19
protectorChristian Graus22-Apr-08 12:19 
GeneralRe: Yeah, but.... Pin
leppie22-Apr-08 22:06
leppie22-Apr-08 22:06 
GeneralRe: Yeah, but.... Pin
Christian Graus22-Apr-08 22:22
protectorChristian Graus22-Apr-08 22:22 
GeneralRe: Yeah, but.... Pin
johannesnestler23-Apr-08 4:22
johannesnestler23-Apr-08 4:22 
AnswerRe: Use FindForm method Pin
Dan Randolph19-Aug-08 9:40
Dan Randolph19-Aug-08 9:40 
QuestionRe: Use FindForm method Pin
stixoffire2-Jul-09 1:59
stixoffire2-Jul-09 1:59 

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

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