Home > Enterprise >  Button disable/enable based on user control value from other user control c#
Button disable/enable based on user control value from other user control c#

Time:10-30

I have a user control which has one Button which should enable/disable if i change the state value from other user control.

Can someone help me know how i can achieve this? How to capture the bool for the changes on the enable/disable button?

Usercontrol1 UTILS

    public Utilitaires()
    {
        InitializeComponent();

        PanelSlider_Utils.Controls.Add(new TCP());
    }

    //My button btnUndo created from the Visual interface

    public void btnUndo_Click(object sender, EventArgs e)
    {
      my code...
    }

Usercontrol2 TCP

    //My method which should by the bool canredo change the enabled state of my button btnredo Usercontrol1

    public void Redo_CanRedoChanged(object Sender, bool CanRedo)
    {

     UTILS foo = new UTILS();

        foo.btnRedo.Enabled = CanRedo;// This is my problem to reach the button btnRedo UserControl 1
    }

Sorry for my English...! Thanks

CodePudding user response:

Ok Laurent, this is solved: as it may be complex, I detail all the steps for future readers.

First, I have created a UserControl named UserControl1; it contains only a button named button1. I will use this UserControl for both of yours to stay simple.

To have the UserControl1 ready in the toolbox on the left, I compile the program (F5) and stop it. Now, I drag two UserControls from the toolbox to the empty Form1. So, the instances created automatically are userControl11 and userControl12. I will make the first control the second one.

Add this code at the beginning of the UserControl.cs (before the contructor):

    public bool ButtonEnable
    {
        get => button1.Enabled;
        set { button1.Enabled = value; }
    }

    public UserControl1 InControl { get; set; }

The bool will make the state of the button accessible to any other object. I stated 'public' but 'internal' is enough if you work inside the same solution. Don't use 'private' else it won't be accessible.

The InControl property will set which other instance should be controlled by the current UserControl.

Now, I double click on the button in the UserControl1 to generate the delegate, complete it as follows:

    private void Button1_Click(object sender, EventArgs e)
    {
        if (InControl != null)
        {
            InControl.ButtonEnable = !InControl.ButtonEnable;
        }
    }

This will toggle the state of the controlled button.

Now, let's go back to the Form1.cs and modify the contructor by adding the second line:

    public Form1()
    {
        InitializeComponent();
        userControl11.InControl = userControl12;
    }

It's done, the button of the first UserControl will toggle the state of the second UserControl.

CodePudding user response:

If all you're trying to do is to enable/disable the control then, Try:

// this is usually done by creating a control in
// the Winforms designer, not in directly in your code:
Usercontrol1 myUserControl = new Usercontrol1();

// class variable

bool controlEnabled = true;




public void btnUndo_Click(object sender, EventArgs e)
{
    // As an example, this will toggle the control from
    // enabled to disabled.
    controlEnabled = !controlEnabled;
    myUserControl.Enabled = controlEnabled;             
}
  • Related