C#在RichTexbox中选择文本删除文本 [英] Selecting text in RichTexbox in C# deletes the text

查看:73
本文介绍了C#在RichTexbox中选择文本删除文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从 Richtextbox 派生的控件中输入了以下文本

I have typed in the following text in a control derived from Richtextbox

世界是{美丽}".

我的主要目的是为美丽这个词创建一个链接.我可以使用 CFE_LINK 创建它,但那是我选择文本的时候.

My main intention is to create a link for the word beautful. I can create this using CFE_LINK , but that's when I select the text.

当我使用 Select (4,9) 时,4 到 9 范围内的文本会被删除.

有人可以帮我解决我遗漏的问题吗?

Can someone please help me with what I am missing out?

代码:

我正在创建一个从 Richtextbox 派生的用户控件.

I am creating a User Control, derived from Richtextbox.

我在下面给出了确切的代码;我没有做任何颜色变化.我认为 Select 命令默认将所选文本设置为蓝色.

I am giving the exact code below; I have not done any color change. I think the Select command sets the selected text to blue by default.

protected override void OnKeyPress(KeyPressEventArgs e)
{
   String keypressed =  e.KeyChar.ToString();
   if(keypressed == "}")
      Select(4,9)        
   base.OnKeyPress(e);
}

推荐答案

一开始我也很困惑.但是后来它击中了我,很可能您按下的键被发送到文本框以在 KeyUp 上呈现.果然,当我将您的代码更改为此时,它起作用了:

At first when I started messing with this, I was puzzled as well. But then it hit me, it's very possible that your key that's being pressed is being sent to the textbox to render at KeyUp. Sure enough, when I changed your code to this it worked:

    protected override void OnKeyUp(KeyEventArgs e)
    {
        base.OnKeyUp(e);
        if (e.KeyCode == Keys.Oem6)
        {
           Select(4, 9);
        }

    }

这篇关于C#在RichTexbox中选择文本删除文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆