Click here to Skip to main content
15,887,421 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I have a Windows form which has a UserControl and two buttons.
On user Control i have a button.I want to disable those two buttons on form when i click on Button on UserControl.

Thanks
Posted
Comments
sashje- 12-Jun-15 8:54am    
Expose an OnButtonClicked event in your usercontrol, and subscripte to the event in the main form.

F.ex in the usercontrol
define "public event EventHandler OnButtonClicked;"
and within the OnClick event of the button do
if (OnButtonClicked!= null)
{
OnButtonClicked(sender, e);
}

In your main form, set an eventhandler to the event
this.MyUserControl.OnButtonClicked += new EventHandler(Method_To_Disable_My_Button);

and

protected void Method_To_Disable_My_Button(object sender, EventArgs e)
{
this.MyButton.Enabled = false;
}
Sergey Alexandrovich Kryukov 12-Jun-15 9:39am    
You could post it as an answer, but you would need to provide better explanation and perhaps fix the code. So far, it does not solve the problem, because you are disabling one "this.*" button, but it should be one on the parent form.

The method name Method_To_Disable_My_Button with fixed implementation makes the messing with events just pointless. You need to accept the handler from the user's code.

Please see Solution 1.

—SA

C#
public partial class xyz_Usercontrol : UserControl
{

    public xyz_Usercontrol()
    {
        InitializeComponent();


    }
    public Button mybt
    {
   //button1 is the button on your user control
        get { return this.button1; }

    }

}

   //code inside your parent form

        private void Form1_Load(object sender, EventArgs e)
        {

            Button tt = xyz_Usercontrol1.mybt;
            tt.Click += tt_Click;
        }

        void tt_Click(object sender, EventArgs e)
        {
        //button1 is the button on your parent form
            button1.Visible = false;
        }
 
Share this answer
 
It does not sound like a good design idea, but there are real cases when you need to do something similar. The problem is: the user control is not supposed to "know" anything about the form using it.

But the problem is simple: you could use some delegate method passed by the parent form to the control, so the control could call it. Better yet, it should be encapsulated using some even of the user control. This is how, for example:
C#
public class MyUserControl : UserControl {

    public MyUserControl() {
        //...
        myButton.Click += () => {
            if (MyButtonClicked != null)
                MyButtonClicked.Invoke(myButton, new System.EventArgs);
        }
    }

    public event System.EventHandler MyButtonClicked;

    Button myButton;

}
The parent form using this control can initialize it and add some event handler to the invocation list of you MyUserControl.MyButtonClicked event instance. This way, the instance of MyButtonClicked will invoke this event not "knowing" what the delegates put in the invocation list really do; they belong to the parent control. This way, the behavior detail of the two control (parent form and your user control) are properly encapsulated and controls' internals are isolated.

By the way, this is a simple example pf the inversion of control concept: http://en.wikipedia.org/wiki/Inversion_of_control[^].

—SA
 
Share this answer
 

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