2017-05-26 47 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你可以阅读更多关于字段和属性(还有后台字段)。