简体   繁体   English

如何知道在RichText框中是否已替换文本ix

[英]How to know Text ix replaced or not in a RichText Box

This is my code that i use: 这是我使用的代码:

Richtextbox.Text = Richtextbox.Text.Replace(ReplaceOldWord, ReplaceNewWord);

How do I know if the old word is replaced with new word or not.... 我怎么知道旧单词是否被新单词替换。

Fist way is to check if text changed: 第一种方法是检查文本是否更改:

    String text = Richtextbox.Text.Replace(ReplaceOldWord, ReplaceNewWord);
    if(RichTextBox.Text != text)
    {
        Richtextbox.Text = text;
        DoSomething();
    }

The second is to use TextChanged Event . 第二种是使用TextChanged Event This event will rize only if richtextbox text changes. 仅当richtextbox文本更改时,此事件才会发生。

public Form()
{
    InitializeComponent();
    RichTextBox.TextChanged += Richtextbox_TextChanged;
}

private void Richtextbox_TextChanged(object sender, EventArgs e)
{
    DoSomething();
}

我不确定这一点,但是如果您有一个文本框,我认为您有GUI,只需尝试在此打印单词或使用streamwriter器将单词保存在文本文件中即可。

If将在替换之前调用String.Contains如果它返回true,则返回与RichTextBox中相同的旧单词:

var replaced = Richtextbox.Text.Contains(ReplaceOldWord);

You can write code as this: 您可以这样编写代码:

richTextBox1.Text = richTextBox1.Text.Replace("old", "new");
if (richTextBox1.Text.Contains("old"))
{
  MessageBox.Show("Not replaced");
  //further actions
}

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

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