Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a winform in that form i am calling user controls if i click button from winfrom how can i access the instance of user control and its button.and how i hide the user control button

What I have tried:

I have no idea how i solve this
Posted
Updated 27-Aug-22 13:40pm
v2

If I understand your question correctly, you want to access the controls inside a user control from outside.

If this is the case I would strongly advice not to do so. If you need to affect the control behaviour from outside or read information from it, define properties and methods in the control. If you need to receive notifications, define events in the user control.

One good principle when creating user controls is that from outside you should not need to know what the control actually contains or how it works. You just need to have meanings to communicate with it as described above.

Have a look at for example Creating a Windows Form User Control[^]. Even though it's VB and rather old it explains the first steps.
 
Share this answer
 
Comments
Maciej Los 8-Sep-16 2:23am    
5ed!
Ralf Meier 8-Sep-16 15:23pm    
I would give the same answer - +5 from me
if you have a userControl and you want to access one of his controls from a form that contains your usercontrol.
there is a solution for that . .. what you can do is you can define a function that can return a specific control .
For example if you have a text box inside you usercontrol:
public Control getTextBox (){
return (Control)this.TextBox1;
// this : represent the UserControl and the TextBox1 is a TextBox inside the userConrol.
// do the same for the other controls (define a function for each control you need to use it after ..)..

}
 
Share this answer
 
Comments
Dave Kreskowiak 28-Aug-22 0:06am    
The outside world should never get a direct reference to a constituent control of a UserControl. You are breaking encapsulation and separation of concerns.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900