Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将文本从一个文本框传输到另一个文本框,而不使其循环且不发送文本?_C# - Fatal编程技术网

C# 如何将文本从一个文本框传输到另一个文本框,而不使其循环且不发送文本?

C# 如何将文本从一个文本框传输到另一个文本框,而不使其循环且不发送文本?,c#,C#,我已经搜索了我要求你的内容,但我可能用词有误,所以我希望我键入的内容容易理解 我正在使用WinForms 我希望它在第一个textBoxLongFormat中使用css长格式代码,然后在第二个textboxShortFormat中显示为css短代码 我为两个文本框都编写了一些代码,并为幕后更改格式编写了一个单独的代码类 这个类可以工作,但是我对textBoxLongFormat有一个问题,我猜它会以另一种方式发生,代码本身在循环,没有关闭,所以它没有将格式发送到textBoxShortForma

我已经搜索了我要求你的内容,但我可能用词有误,所以我希望我键入的内容容易理解

我正在使用
WinForms

我希望它在第一个
textBoxLongFormat
中使用css长格式代码,然后在第二个
textboxShortFormat
中显示为css短代码

我为两个文本框都编写了一些代码,并为幕后更改格式编写了一个单独的代码类

这个类可以工作,但是我对
textBoxLongFormat
有一个问题,我猜它会以另一种方式发生,代码本身在循环,没有关闭,所以它没有将格式发送到
textBoxShortFormat
,所以什么也没有发生

我知道我做错了什么,但我看不出来。我做错了什么?有你的帮助会很好

下面是文本框的代码,如果有帮助的话

我需要添加什么或使其工作

    private void textBoxLongFormat_TextChanged(object sender, EventArgs e)
    {
        CssFormatConverter cssLongFormatConverter = new CssFormatConverter();
        string longFormatCss = textBoxLongFormat.Text;
        string shortFormatCss = cssLongFormatConverter.ToShortFormat(longFormatCss);
        textBoxShortFormat.Text = shortFormatCss;
    }

    private void textBoxShortFormat_TextChanged(object sender, EventArgs e)
    {
        CssFormatConverter cssShortFormatConverter = new CssFormatConverter();
        string shortFormatCss = textBoxShortFormat.Text;
        string longFormatCss = cssShortFormatConverter.ToLongFormat(shortFormatCss);
        textBoxLongFormat.Text = longFormatCss;
    }

提前感谢

添加一个布尔检查,指示另一个文本框正在更新

bool isUpdating = false;
private void textBoxLongFormat_TextChanged(object sender, EventArgs e)
{
    if (!isUpdating)
    {
        isUpdating = true;
        CssFormatConverter cssLongFormatConverter = new CssFormatConverter();
        string longFormatCss = textBoxLongFormat.Text;
        string shortFormatCss = cssLongFormatConverter.ToShortFormat(longFormatCss);
        textBoxShortFormat.Text = shortFormatCss;
        isUpdating = false;
    }
}

private void textBoxShortFormat_TextChanged(object sender, EventArgs e)
{
    if (!isUpdating)
    {
        isUpdating = true;
        CssFormatConverter cssShortFormatConverter = new CssFormatConverter();
        string shortFormatCss = textBoxShortFormat.Text;
        string longFormatCss = cssShortFormatConverter.ToLongFormat(shortFormatCss);
        textBoxLongFormat.Text = longFormatCss;
        isUpdating = false;
    } 
}

在更新文本框之前,请从
TextChanged
事件中取消订阅该文本框。然后更新。然后重新订阅

第一种情况是:

textBoxShortFormat.TextChanged -= textBoxShortFormat_TextChanged;
textBoxShortFormat.Text = shortFormatCss;
textBoxShortFormat.TextChanged += textBoxShortFormat_TextChanged;

我建议您使用计时器,在内存中获取textbox1实际状态的“副本”,使用该副本,并将其发送到TextBox2。我同意这一点,根据类的操作,您需要
返回
某些内容(可能是
布尔值
),让循环知道它已经完成了。谢谢你,约翰·科纳,它工作得很好。这一直困扰着我,我希望我能尽快掌握c#soon,谢谢again@plast1K我相信这样做的目的是使两个文本框保持同步,使循环成为一个意外的副作用。因此,不需要返回值。