private void textBox1_TextChanged(object sender, EventArgs e)
{
if (a.Equals(b))
{
result = a.ToString();
textBox1.Text = result;
textBox1.TextAlign = HorizontalAlignment.Left;
}
else
result = b.ToString();
textBox1.Text = result;
textBox1.TextAlign = HorizontalAlignment.Center;
}
答案 0 :(得分:0)
很可能你忘记在else语句中添加括号。
试试这段代码:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (a.Equals(b))
{
result = a.ToString();
textBox1.Text = result;
textBox1.TextAlign = HorizontalAlignment.Left;
}
else
{
result = b.ToString();
textBox1.Text = result;
textBox1.TextAlign = HorizontalAlignment.Center;
}
}
将大括号放在if
,else
语句,loops
等中始终是一种好习惯。
阅读此问题的答案:Why is it considered a bad practice to omit curly braces?