简体   繁体   English

c#Textchanged事件在多行TextBox中触发两次

[英]c# Textchanged event fires twice in a multiline TextBox

I have a very bizarre problem with the TextChanged event in a multiline TextBox (WinForms). 我在多行TextBox(WinForms)中的TextChanged事件有一个非常奇怪的问题。 it appears that the event fires twice under certain circumstances. 在某些情况下,事件似乎会发生两次。

This complete code demonstrates the issue: 完整的代码演示了这个问题:

public partial class TextChangedTest : Form
{
    public TextChangedTest()
    {
        InitializeComponent();
    }

    private void TextChangedTest_Load(object sender, EventArgs e)
    {
        TextBox tb = new TextBox();
        //Remove the following line and the code behaves as expected
        tb.Multiline = true;
        this.Controls.Add(tb);
        tb.TextChanged += new EventHandler(tb_TextChanged);
    }
    private void tb_TextChanged(object sender,EventArgs e)
    {
        //Need to validate and use the new text here.
        //For testing, just use a MessageBox
        MessageBox.Show("Handler fired");
    }
}

If you now type a character in the TextBox, the event fires. 如果现在在TextBox中键入一个字符,则会触发该事件。 Correct behaviour. 正确的行为。

If you delete the character, the event fires once. 如果删除该字符,则该事件将触发一次。 Correct behaviour. 正确的行为。

If you delete the character with backspace, the event fires once. 如果您使用退格键删除该字符,则该事件将触发一次。 Correct behaviour. 正确的行为。

If you delete the character by selecting it and pressing Delete, the event fires once. 如果通过选择并按Delete删除该字符,则事件将触发一次。 Correct behaviour. 正确的行为。

However 然而

If you select the character and type another character, the event fires twice, once after the text box is cleared (look at the textbox when the first event fires) and once when the character is added. 如果选择字符并键入另一个字符,则事件将在文本框清除后触发两次(在第一个事件触发时查看文本框),并在添加字符时触发一次。 This behaviour is nonsense, if understandable. 如果可以理解,这种行为是无稽之谈。

This only happens when the Multiline property is set (which I am using to resize the TextBox). 这仅在设置Multiline属性时发生(我用它来调整TextBox的大小)。 It just took me 5 hours to work that out! 我花了5个小时才完成工作!

My problem is that I have to validate each character in the TextChanged event, and I get invalid results. 我的问题是我必须验证TextChanged事件中的每个字符,并且我得到无效的结果。

Does anyone have any ideas? 有没有人有任何想法?

I could possibly use the Keypress event, but I would need to do a lot of recoding to do that. 我可以使用Keypress事件,但我需要做很多重新编码才能做到这一点。

The workaround for my particular case is even more bizarre. 我的具体案例的解决方法更加奇怪。

I needed the Textbox to have a specific size, but the height cannot be changed unless it is multiline. 我需要文本框具有特定的大小,但除非是多行,否则不能更改高度。

The following, however, works for me in my little world: 然而,以下内容适用于我的小世界:

    tb.Multiline = true;
    tb.Size = new Size(x,y);
    tb.Multiline = false;
    this.Controls.Add(tb);
    tb.TextChanged += new EventHandler(tb_TextChanged);

Solution found at http://en.code-bude.net/tag/c-textbox-height-resize/ 解决方案可在http://en.code-bude.net/tag/c-textbox-height-resize/找到

Doesn't resolve the underlying problem, though. 但是,不能解决潜在的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM