如何在C#中提取RichTextBox中的最后一个单词 [英] How to extract last word in richtextbox in c#

查看:430
本文介绍了如何在C#中提取RichTextBox中的最后一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows窗体.在我的项目中,我需要为富文本框的最后一个单词上色.当有人在应用程序的给定文本框上书写时,我需要将刚写在richtextbox上的最后一个单词涂成红色或其他颜色.

I am working with windows form. In my project i need to color the last word of rich text box. When someone write on the given text box of the application i need the last word that is just written on the richtextbox to be colored red or whatever.

我找到了一种从以下链接中提取最后一个单词的方法

I found a way to extract the last word from the following link

http://msdn.microsoft.com/zh-cn/library/system.windows.documents.textselection.select%28v=vs.95%29.aspx

但是,如果可能的话,我需要更多方便的代码来提取最后一个单词.请帮忙.

But i need more handy code to extract the last word if its possible. Please help.

推荐答案

以下是示例代码

*> char [] arr =新的char [50];

*> char[] arr = new char[50];

      int i = 0;
    private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
     {
    if (e.KeyCode == Keys.Space)            {
        string str = new string(arr); 
        MessageBox.Show(str);
        Array.Clear(arr, 0, arr.Length);
        i = 0;
    }
    else if (e.KeyCode == Keys.Back)
       {
        i--;
        if (i < 0)
        {
            i = 0;
        }
        arr[i] = ' ';


    }

    else
    {
        arr[i] = (char)e.KeyValue;

        i++;
    }
    }*

这是您将能够提取最新单词的方式.现在,给自己加上喜欢的词.

This is how you will be able to extract the latest word. Now color yourself the word you like.

这篇关于如何在C#中提取RichTextBox中的最后一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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