我从另一个问题复制了这段代码。它应该工作,但在我的尝试中失败了。谁能告诉我为什么?谢谢!
private void Form1_Load(object sender, EventArgs e)
{
textBox1.DataBindings.Add("Text", this, "Unit");
}
private int unit;
public event EventHandler UnitChanged; // or via the "Events" list
public int Unit
{
get { return unit; }
set
{
if (value != unit)
{
unit = value;
EventHandler handler = UnitChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Unit++;
}
答案 0 :(得分:1)
您正在递增私有字段(小写unit
),而不是绑定到TextBox
的属性(大写Unit
)。
尝试在此处更改代码:
private void button1_Click(object sender, EventArgs e)
{
Unit++;
}