Click here to Skip to main content
Licence CPOL
First Posted 22 Apr 2008
Views 41,356
Downloads 755
Bookmarked 17 times

Calling parent form functions from a user control

By | 22 Apr 2008 | Article
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)

About the Author

AlG



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionIn VB.Net Calling parent form functions from a user control Pinmembervino123423:17 21 Jun '11  
GeneralThanks Pinmembermatrixology3:42 3 May '11  
General'Delegate' is a 'namespace' but is used like a 'type' Pinmemberveronicam@warnerpacific.com7:15 12 Oct '10  
GeneralMy vote of 5 PinmemberDarrinLynn5:56 4 Oct '10  
GeneralMy vote of 5 PinmemberMember 43205166:15 6 Jul '10  
GeneralMy vote of 5 Pinmemberbinbel20:50 4 Jul '10  
Generalmvc... PinmemberChris Richner22:49 22 Apr '08  
GeneralYeah, but.... PinmvpChristian Graus12:19 22 Apr '08  
GeneralRe: Yeah, but.... Pinmember leppie 22:06 22 Apr '08  
GeneralRe: Yeah, but.... PinmvpChristian Graus22:22 22 Apr '08  
GeneralRe: Yeah, but.... Pinmemberjohannesnestler4:22 23 Apr '08  
AnswerRe: Use FindForm method PinmemberDan Randolph9:40 19 Aug '08  
QuestionRe: Use FindForm method Pinmemberstixoffire1:59 2 Jul '09  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 22 Apr 2008
Article Copyright 2008 by AlG
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid