RichTextBox last change check

2019-09-11 07:17发布

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?

3条回答
仙女界的扛把子
2楼-- · 2019-09-11 08:00

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;
    }
查看更多
Animai°情兽
3楼-- · 2019-09-11 08:12

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.

查看更多
我只想做你的唯一
4楼-- · 2019-09-11 08:17

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.

查看更多
登录 后发表回答