2017-05-26 63 views
0

我有個任務,使按鈕活躍後經過兩個文本框填充和 文件路徑字符串不是空電量三條的bool是真正的製作按鈕是真實

public bool isFileOpened = false; 
public bool isDrive = false; 
public bool isPrice = false; 

他們正在成爲真正的

private void textBox1_TextChanged(object sender, EventArgs e) { 
    drive = CheckIntInput(sender, "not valid"); 
    if (drive != 0) { 
     isDrive = true; 
    } 
} 
private void textBox2_TextChanged(object sender, EventArgs e) { 
    price = CheckIntInput(sender, "not valid"); 
    if (price != 0) { 
     isPrice = true; 
    } 
} 
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e) { 
    filePath = openFileDialog1.FileName; 
    label1.Text = filePath; 
    isFileOpened = true; 
} 

CheckIntInput方法從文本框或0返回號碼,如果傾斜字符串轉換爲編號

,我怎麼能實現的東西LIK E本:

if (isFileOpened && isDrive && isPrice) { 
    showButton.Enabled = true; 
} 

我要讓按鈕立即啓動所有三個布爾變量之後變爲真實的,theese三個字段可以以不同的方式輸入,如

  1. TextBox1的
  2. TextBox2中
  3. openfiledialog1

or

  1. TextBox1的
  2. openfiledialog1
  3. TextBox2中

回答

1

有多種方法可以做到這一點,我會使用一個屬性與支持字段,就像這樣:

public bool IsFileOpened 
{ 
    get { return _isFileOpened; } 
    set 
    { 
     _isFileOpened = value; 
     UpdateShowButton(); 
    } 
} 

public bool IsDrive 
{ 
    get { return _isDrive; } 
    set 
    { 
     _isDrive = value; 
     UpdateShowButton(); 
    } 
} 

public bool IsPrice 
{ 
    get { return _isPrice; } 
    set 
    { 
     _isPrice = value; 
     UpdateShowButton(); 
    } 
} 

private void UpdateShowButton() 
{ 
    if (IsPrice && IsDrive && IsFileOpened) 
     showButton.Enabled = true; 
} 
private void textBox1_TextChanged(object sender, EventArgs e) 
{ 
    drive = CheckIntInput(sender, "not valid"); 
    if (drive != 0) 
    { 
     IsDrive = true; 
    } 
} 
private void textBox2_TextChanged(object sender, EventArgs e) 
{ 
    price = CheckIntInput(sender, "not valid"); 
    if (price != 0) 
    { 
     IsPrice = true; 
    } 
} 
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e) 
{ 
    filePath = openFileDialog1.FileName; 
    label1.Text = filePath; 
    IsFileOpened = true; 
} 

其實我重新命名它,所以你必須使用大寫首字母的屬性。現在,每次更新屬性時,都會檢查是否啓用showButton。

Here你可以閱讀更多關於字段和屬性(還有後臺字段)。