我有一项任务是在三个bool为真后使Button处于活动状态
public bool isFileOpened = false;
public bool isDrive = false;
public bool isPrice = false;
填充两个文本框后,它们变得正确 filePath字符串不为空
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
方法从文本框返回数字,如果不能将字符串转换为数字
我怎么能意识到这样的事情:
if (isFileOpened && isDrive && isPrice) {
showButton.Enabled = true;
}
我希望在所有三个bool变为true后立即启用按钮,并且可以以不同方式输入theese三个字段,例如
或
答案 0 :(得分: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您可以阅读有关字段和属性的更多信息(还有支持字段)。