【发布时间】:2014-08-05 18:31:23
【问题描述】:
我在 Winform 上有两个文本框。这个想法是在一个框中输入华氏温度,并让它在另一个框中显示摄氏度作为用户键入,反之亦然。
我使用文本框的“离开”方法可以正常工作,但我试图使用 TextChanged 方法来避免必须单击或按下按钮来获得结果。
我现在遇到的问题是,当第一个字符被处理时,焦点会移动到第二个框,它会触发 TextChanged 方法。
我已尝试使用发送方启用 if(),但发送方现在是目标框。
有什么想法吗?
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();
}
【问题讨论】: