Reputation: 101
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
Upvotes: 0
Views: 491
Reputation: 101
Thanks @ken and @fredy for showing me a way. I found another solution that works in my case :
Usercontrol1 Utils
public static Utils Current = null;
public Utilitaires()
{
InitializeComponent();
PanelSlider_Utils.Controls.Add(new Tcp());
Current = this;
}
//My button btnUndo created from the Visual interface
public void btnUndo_Click(object sender, EventArgs e)
{
my code...
}
Usercontrol2 Tcp
public Utils UndoRedo = Utils.Current;
public void Redo_CanRedoChanged(object Sender, bool CanRedo)
{
UndoRedo.btnRedo.Enabled = CanRedo;
}
Thanks again for your help !
Upvotes: 0
Reputation: 536
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;
}
// Additional info: if you want to disable only a button in your
// user control then create a public method to disable the button
// ex:
public class MyUserControl
{
// other stuff
public void EnableMyButton(bool enable)
{
MyButton.Enable = enable;
}
}
public void btnUndo_Click(object sender, EventArgs e)
{
// As an example, this will toggle the control from
// enabled to disabled.
controlEnabled = !controlEnabled;
myUserControl.EnableMyButton(controlEnabled);
}
Upvotes: 1
Reputation: 577
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.
Upvotes: 1