Click here to Skip to main content
16,020,249 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have two user controls Usercontrol1 and Usercontrol2

Usercontrol1 load into Usercontrol2, i want a situation when i press a button in Usercontrol1 an action should be perform

let me say when i press a button in Usercontrol1 a button that is enable in Usercontrol2 should be disable

Thank you

What I have tried:

i can only transfer data from user controls but not able to perform action
Posted
Updated 9-Mar-23 10:36am
v2
Comments
Member 8923021 1-Dec-23 14:22pm    
I am trying to do what you are doing so could you show me the code you used to load user control1 into user control2. Plus the xmal.

Simple. You don't.

The UserControls should know absolutely nothing about each other. They shouldn't even know another instance exists.

If you want to tell another control or form something, you do it through events. If the user clicks a button in in your UserControl1 (or whatever you named it), it raises an event that subscribers to the event would handle. In your case, that would be the container you put your UserControl1 instance in.

The container decides what to do when the event is raised, like tell your other UserControl2 instance to do something. That UserControl2 would expose public methods and properties to take in that information and do whatever is needed, like enable or disable a button UserControl2 has.
 
Share this answer
 
Comments
[no name] 23-Jun-22 5:32am    
Thank you for all your suggestions, it really gives a clue. i will appreciate if i can be a given a clue on how to do about it..

UserControl2
public bool DisableButton
{
set { btn1.Enabled = value; }
}

UserControl1
UserControl1.DisableButton = false;
Dave Kreskowiak 23-Jun-22 9:58am    
Again, any control should not know anything at all about another control. In your case, UserControl2 should NEVER mention UserControl1.
[no name] 23-Jun-22 11:57am    
You have any possible solution please??
Dave Kreskowiak 23-Jun-22 11:58am    
I already told you what the solution is. YOU have to write the code to do it.

Nobody else can write that code because there isn't enough detail in your question to do that, and nobody else is getting paid to write that code, except you.
[no name] 23-Jun-22 13:39pm    
Assuming UserControl2 (UC2) is a "singleton", add a static reference to UC2 in UC2: e.g.

public static UC2 Current = null;

Then in the constructor: Current = this;

Later on you can reference UC2 as: UC2.Current.DisableButton();
Additional to the Suggestion from Dave :

Another way could be to work with Properties. You could give your Control_2 a Property which tells the Control, if necessary, how to act in a special way.
 
Share this answer
 
Comments
[no name] 23-Jun-22 5:32am    
Thank you for all your suggestions, it really gives a clue. i will appreciate if i can be a given a clue on how to do about it.
some sample

UserControl2
public bool DisableButton
{
set { btn1.Enabled = value; }
}

UserControl1
UserControl1.DisableButton = false;

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