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;
}
Asked
Active
Viewed 800 times
-6
marc_s
- 704,970
- 168
- 1,303
- 1,425
afsun shah
- 11
- 1
-
6What exactly are you are having problems with? What's currently not working with your if statements? – ryanyuyu Jan 23 '15 at 15:01
-
Are you missing brackets for the else or is that intentional? – Thijs Jan 23 '15 at 15:02
-
@Plue [that edit](http://stackoverflow.com/review/suggested-edits/6829258) is invalid. Don't change the code of a question, especially since the missing brackets may be the source of the issue. – CodeCaster Jan 23 '15 at 15:03
-
1@CodeCaster sorry I thought I deleted it myself while editing – Plue Jan 23 '15 at 15:06
1 Answers
0
Most probably you forgot to add brackets in your else statement.
Try this code:
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;
}
}
It is always good practice to place curly brackets in if, else statements, loops, etc.
Read answers in this question: Why is it considered a bad practice to omit curly braces?
Community
- 1
- 1
Nikolay Kostov
- 15,454
- 21
- 81
- 119