尝试从一个文本框更新另一个文本框,反之亦然C#Winforms

尝试从一个文本框更新另一个文本框,反之亦然C#Winforms,c#,winforms,sender,C#,Winforms,Sender,我在Winform上有两个文本框。我们的想法是在一个框中输入华氏温度,在另一个框中显示Celcius作为用户类型,反之亦然 我使用文本框的“离开”方法可以很好地工作,但是我尝试使用TextChanged方法来避免点击或按下按钮来获得结果 我现在遇到的问题是,当处理第一个字符时,焦点移动到第二个框,这将触发TextChanged方法 我尝试使用发送方启用if(),但发送方现在是目标框 有什么想法吗 private void TB_Fahrenheit_TextChanged(obje

我在Winform上有两个文本框。我们的想法是在一个框中输入华氏温度,在另一个框中显示Celcius作为用户类型,反之亦然

我使用文本框的“离开”方法可以很好地工作,但是我尝试使用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();
    }
private void TB_Fahrenheit_TextChanged(对象发送方,事件参数e)
{
浮点数C,F;
如果(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();
}
}
私有void TB_Celcius_TextChanged(对象发送方,事件参数e)
{
浮点数C,F;
字符串SentBy=((文本框)sender).Name;
MessageBox.Show(SentBy);
返回;
if(SentBy!=TB_Fahrenheit.Text)
{
如果(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();
}
}
}
私有void TB_Celcius_Enter(对象发送方,事件参数e)
{
TB_Celcius.Clear();
华氏度;
}
私有void TB_Fahrenheit_Enter(对象发送方,事件参数e)
{
TB_Celcius.Clear();
华氏度;
}

我将使用布尔检查程序。您正在更改具有文本更改事件的文本框的文本。如果当前正在编辑另一个框,则需要确保它不执行任何操作。我的例子

private void setSliderValue(int currentAudioSecond)
{
    valueFromTimer = true;
    //Do Stuff
    valueFromTimer = false;
}
然后,在我的滑块控件上更改值时:

private void sliderAudio_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    if (!valueFromTimer)
    {
        //Do stuff  
    }
}
private void sliderAudio\u值已更改(对象发送方,RoutedPropertyChangedEventArgs e)
{
如果(!valueFromTimer)
{
//做事
}
}

我必须经常这样做,当加载窗口或表单时,我填写一个组合框。为了避免选定的值/索引更改为激发,我必须实现这样一个标志。

我建议使用KeyPress事件来计算另一个文本框中的值,类似这样,尽管您必须对float.Parse进行一些格式检查以确保您有一个数值

        private void TB_Fahrenheit_KeyPress(object sender, KeyPressEventArgs 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_KeyPress(object sender, KeyPressEventArgs e)
    {
        float C, F;
        string SentBy = ((TextBox)sender).Name;


        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_Fahrenheit_按键(对象发送器,按键事件参数e)
{
浮点数C,F;
如果(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();
}
}
私有无效TB_Celcius_按键(对象发送器,按键事件参数e)
{
浮点数C,F;
字符串SentBy=((文本框)sender).Name;
如果(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();
}
}

我只需在和上订阅和取消订阅活动即可

当您
输入
华氏温度文本框时,它将停用celsiusTextBox的TextChanged事件。输入celsiusTextBox时也会发生同样的情况,您的华氏温度Extbox.TextChanged事件将被取消订阅,从而允许您利用适当的事件TextChanged事件来实现您的目标

除此之外,我不鼓励在GUI中使用数学公式,因为您可能会有错误,而且很难进行测试。最好有一个static
Convert
类,该类具有用于所需转换的适当方法

public static class Convert {
    public float ToFahrenheit(float celsius) {
        float f = celsius * 9 / 5 + 32;
        return f;
    }

    public float ToCelsius(float fahrenheit) {
        float c = (fahrenheit - 32) * 5 / 9;
        return c;
    }
}
然后就可以编写单元测试了

[TestMethod]
public void ZeroCelsiusShouldConvertTo32Fahrenheit() {
    // Given
    float expected 32.0;
    float celsius = 0.0;        


    // When
    var actual = Convert.ToFahrenheit(celsius);

    // Then
    Assert.AreEqual(typeof(float), actual);
    Assert.AreEqual(expected, actual);
}
我认为您必须使用事件,而不是
TextChanged
。因为它是在

  • 通过使用键盘(TAB、SHIFT+TAB等)、调用Select或SelectNextControl方法,或者将ContainerControl.ActiveControl属性设置为当前表单,可以更改焦点

  • 使用鼠标或调用focus方法更改焦点时


  • 好主意。每当我有一个带有DataGrid或其他UI元素的项目时,我都会使用类似的方法,其中我可能会通过编程更改元素。这样,如果我通过编程更改用户也可以编辑的元素,我就可以避免触发事件。Andrew,我发现了类似的东西,这对我来说很有用。谢谢史蒂夫,谢谢,但这正是我想要避免的。使用“离开”事件意味着在手动移动焦点之前不会显示任何内容。@Cimbian:为什么要避免它?因为在我所做的事情中它看起来更好。随着输入的改变,输出也会自动更新。威尔,谢谢。我做了更多的打猎,我现在基本上是这样做的,但是在进入比赛时设置了一个标志。我将检测TextChanged方法中的标志。谢谢你的帮助。我正在使用按键事件来构建我的字符串,并检查每个输入的数字、小数点或符号,但字符串值在焦点移动或通过文本更改之前不可用(我想)。