I have two text fields in Winform. The idea is to enter Fahrenheit in one box and present it to Celcius in another as a user, and vice versa.
I work fine using the Leave method in the text box, but I tried to use the TextChanged method to not press or not click the button for the result.
Now the problem is that when processing the first character, the focus moves to the second block, which calls the TextChanged method.
I tried using the sender to include if (), but the sender is now the destination field.
Any thoughts please?
private void TB_Fahrenheit_TextChanged(object sender, EventArgs e)
{
float C, F;
if (TB_Fahrenheit.Text != "")
{
if (float.Parse(TB_Fahrenheit.Text) < 1.0F)
{
TB_Fahrenheit.Text = "0" + TB_Fahrenheit.Text;
}
F = float.Parse(TB_Fahrenheit.Text);
C = ((F - 32) * 5) / 9;
TB_Celcius.Text = C.ToString();
}
}
private void TB_Celcius_TextChanged(object sender, EventArgs e)
{
float C, F;
string SentBy = ((TextBox)sender).Name;
MessageBox.Show(SentBy);
return;
if(SentBy != TB_Fahrenheit.Text)
{
if (TB_Celcius.Text != "")
{
if (float.Parse(TB_Celcius.Text) < 1.0F)
{
TB_Celcius.Text = "0" + TB_Celcius.Text;
}
C = float.Parse(TB_Celcius.Text);
F = ((C * 9) / 5) + 32;
TB_Fahrenheit.Text = F.ToString();
}
}
}
private void TB_Celcius_Enter(object sender, EventArgs e)
{
TB_Celcius.Clear();
TB_Fahrenheit.Clear();
}
private void TB_Fahrenheit_Enter(object sender, EventArgs e)
{
TB_Celcius.Clear();
TB_Fahrenheit.Clear();
}
source
share