RichTextBox last change check

2019-09-11 07:46发布

问题:

How can i know the last change that the user did in the text in RichTextBox? Is there a function? Or I need to build to it a function?

回答1:

Add an event to your textbox. Select your textbox, go to properties, in event tab you will see different events. Find TextChanged event. Add that event to your code.

This function will trigger everytime textbox is changed. You can write your logic in that function.



回答2:

It provides an event called TextChanged. You'll need to define an EventHandler and specify what is to happen, when the text changes.

For example:

private void rtb_TextChanged(object sender, EventArgs e)
{
    char c = rtb.Text.ElementAt(rtb.Text.Length);
    if(c == mystring.ElementAt(mystring.Length))
      //is equal
    mystring = rtb.Text; //save string for next comparison
}

and now you need to add the eventhandler to the event like

rtb.TextChanged += rtb_TextChanged;

Look here for documentation.



回答3:

UpdatedChar would contain the newly added character to the textbox1

OldValue would always contain the Old Value in the textbox1

From your example : if my text is: "Text" and than I click 8 so the Text will be "Text8" so the char is 8.

UpdatedChar=8 and OldValue = Text

    public string OldValue = string.Empty;

    public char UpdateChar;

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        var newText = sender as TextBox;

        foreach (var val in newText.Text)
        {
            if(!OldValue.ToCharArray().Contains(val))
                UpdateChar = val;
        }

        OldValue = newText.Text;
    }