我有一個用戶控制元件,如果我從其他用戶控制元件更改狀態值,它應該啟用/禁用一個按鈕。
有人可以幫助我知道如何實作這一目標嗎?如何捕獲啟用/禁用按鈕上更改的布林值?
用戶控制元件1 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...
}
用戶控制2 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
}
對不起我的英語不好...!謝謝
uj5u.com熱心網友回復:
Ok Laurent,這已經解決了:因為它可能很復雜,我為未來的讀者詳細介紹了所有步驟。
首先,我創建了一個名為 UserControl1 的 UserControl;它只包含一個名為 button1 的按鈕。我將為你們兩個使用這個 UserControl 以保持簡單。
為了在左側的工具箱中準備好 UserControl1,我編譯程式 (F5) 并停止它。現在,我將兩個 UserControl 從工具箱拖到空的 Form1 中。因此,自動創建的實體是 userControl11 和 userControl12。我會讓第一個控制第二個。
在 UserControl.cs 的開頭(在建構式之前)添加此代碼:
public bool ButtonEnable
{
get => button1.Enabled;
set { button1.Enabled = value; }
}
public UserControl1 InControl { get; set; }
bool 將使按鈕的狀態可供任何其他物件訪問。我說的是“公共”,但如果您在同一個解決方案中作業,“內部”就足夠了。不要使用“私人”,否則將無法訪問。
InControl 屬性將設定哪個其他實體應由當前 UserControl 控制。
現在,我雙擊 UserControl1 中的按鈕生成委托,完成如下:
private void Button1_Click(object sender, EventArgs e)
{
if (InControl != null)
{
InControl.ButtonEnable = !InControl.ButtonEnable;
}
}
這將切換受控按鈕的狀態。
現在,讓我們回到 Form1.cs 并通過添加第二行來修改建構式:
public Form1()
{
InitializeComponent();
userControl11.InControl = userControl12;
}
完成后,第一個 UserControl 的按鈕將切換第二個 UserControl 的狀態。
uj5u.com熱心網友回復:
如果您要做的只是啟用/禁用控制元件,請嘗試:
// 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);
}
uj5u.com熱心網友回復:
感謝@ken 和@fredy 向我展示了一種方法。我找到了另一種適用于我的解決方案:
Usercontrol1 實用程式
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...
}
用戶控制2 TCP
public Utils UndoRedo = Utils.Current;
public void Redo_CanRedoChanged(object Sender, bool CanRedo)
{
UndoRedo.btnRedo.Enabled = CanRedo;
}
再次感謝你的幫助 !
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/523309.html
標籤:C#表格
上一篇:文本C#的外發光效果
