【问题标题】:Hide some part of text in the Richtextbox in vb.net在 vb.net 的 Richtextbox 中隐藏部分文本
【发布时间】:2013-05-01 03:04:30
【问题描述】:

我想在 vb.net 的 Richtextbox 中隐藏部分文本。 即

Richtextbox1.text ="Test1 test2 test3"

现在我想从富文本框文本中隐藏“test2”,它应该在富文本框控件上只显示“Test1 test3”文本。

那么有人帮助我并告诉我该怎么做吗?

提前致谢。

【问题讨论】:

  • 它是一个静态值,您可以调用 Replace() 来简单地将其从字符串中删除吗?如果没有,请详细说明该值以及您打算如何在实际问题中找到它。
  • 是的,它是静态值,我们只知道替换该值。

标签: winforms richtextbox selection


【解决方案1】:

“是的,它是静态值”

这似乎是一个微不足道的答案......

    RichTextBox1.Text = RichTextBox1.Text.Replace("test2", "")

*假设静态值不是另一个字符串的一部分。

【讨论】:

    【解决方案2】:

    RichTextBox 控件确实支持 Visible rtf 代码,所以这可以工作:

    string hideText = "test2 ";
    if (richTextBox1.Find(hideText) > -1) {
      richTextBox1.SelectedRtf = @"{\rtf1\ansi\v " + hideText + @"\v0}";    
      MessageBox.Show(richTextBox1.Text);
    }
    

    请注意,显示的消息仍将显示“test2”。

    根据您的评论,这是一种通过向后迭代来隐藏单词所有实例的方法:

    string hideText = "[test2]";
    int index = richTextBox1.TextLength;
    while (index > -1) {
      index = richTextBox1.Text.LastIndexOf(hideText, index);
      if (index > -1) {
        richTextBox1.Select(index, hideText.Length);
        richTextBox1.SelectedRtf = @"{\rtf1\ansi\v " + hideText + @"\v0}";
      }
    }
    

    【讨论】:

    • 效果很好。但如果 rtf 的值类似于richTextBox1.text = "[test2] [test1] [test2]"。现在我想隐藏 [test2] 那怎么可能。
    • @BrijeshPatel 更新帖子。
    猜你喜欢
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2014-12-20
    相关资源
    最近更新 更多