RichTextBox last change check

我怕爱的太早我们不能终老 提交于 2019-12-25 01:50:03

问题


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;
    }


来源:https://stackoverflow.com/questions/15658483/richtextbox-last-change-check

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!