2014-01-13 16 views

回答

0
myDynCheckBox.CheckedChanged += CBCheckedChanged; 
     private void CBCheckedChanged(object sender, EventArgs e) 
     { 
      var temp = sender as CheckBox; 
      if (temp != null) 
      { 
       if (temp.Checked) 
       { 
        MyButton.Enabled = false; 
       } 
       else 
       { 
        MyButton.Enabled = true; 
       } 
      } 
     } 
0

我would'nt使用數據綁定做禁用按鈕,但使用事件:

var mycheckBox = new CheckBox(); 
mycheckBox.CheckedChanged += new EventHandler(object sender, EventArgs e) 
    { somebutton.Enabled = mycheckBox.Checked; } 
+0

您的示例並不能真正幫助OP,因爲它不顯示實際啓用/禁用按鈕的複選框事件。其實,你的樣本根本不適合這個問題...... –

0

你不能只是要求代碼,但這可能會讓你在路上。這些是步驟,你必須遵循:

  • 通過代碼
  • 創建一個CheckBox附加一個事件處理程序的複選框(也在代碼)
  • 在活動現場,檢查它是否是檢查或不
  • 延遲檢查狀態,啓用/禁用按鈕

此外,這與數據綁定有什麼關係?

+0

我同意你的看法,**這與數據綁定有什麼關係?!** – Amir

1
using System; 
using System.Windows.Forms; 

namespace WindowsFormsApplication 
{ 
    public partial class Form1 : Form 
    { 
     private CheckBox checkBox; 
     private Button button; 

     public Form1() 
     { 
      InitializeComponent(); 

      checkBox = new CheckBox(); 
      checkBox.Left = 12; 
      checkBox.Top = 41; 
      Controls.Add(checkBox); 

      button = new Button(); 
      button.Left = 12; 
      button.Top = 64; 
      button.Text = "Action"; 
      Controls.Add(button); 

      button.DataBindings.Add("Enabled", checkBox, "Checked"); 
     } 
    } 
}