富文本框中的可点击文本 [英] clickable text in richtextbox

查看:105
本文介绍了富文本框中的可点击文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 c# 中的代码(它不是 URL 只是普通文本)制作可点击的文本,所以当我点击该文本时,它会执行 linkclicked 事件.这是一个字典,所以当我搜索某个东西时,在描述中有一个相同的词从存储中我可以点击它来搜索那个词.

I want to make a clickable text through code in c# (its not a URL just ordinary text) so when i click that text it do linkclicked event. It is for a dictionary so when I search something and in the description there is a word same from the storage I can click it to search that word.

for (int j = 0; j <= jml[i]; j++)
        {
            richTextBox4.AppendText(j + 1 + ". ");
            string[] desk = sk[i, j].Split(' ');
            for (int l = 0; l < desk.Count(); l++)
            {
                for (int m = 0; m < kata.Count(); m++)
                {
                    if (desk[l] == kata[m])
                    {
                        richTextBox4.SelectionColor = Color.Blue;
                        desk[l] = LinkArea;
                    }
                }
                richTextBox4.AppendText(desk[l] + " ");
                richTextBox4.SelectionColor = Color.Black; 
            }
            richTextBox4.AppendText("\n");

        }

我暂时的代码(对于超链接类型的文本,只是有效的颜色)

my code for the moment(for the hyperlink type text just the color that work)

推荐答案

没有内置的方法,但如果你知道你想要什么,它并不难构建.

There is no built-in way but if you know what you want it is not hard to build.

  • 第一步是决定可点击文本的定义,阅读:什么是分隔符?

  • 如果您想要单个单词,这很容易.一种方法是使用 regEx 来识别 RTB 中的单词.如果您不喜欢定义单词的内置方式,我可以发布带有用户定义分隔符的代码.但也许您对已有的方法感到满意.
  • 如果您需要将整个短语(包括空格)作为一个整体进行点击,那么您就必须更有创意:
  • 您可以在短语周围添加特殊字符
  • 或者你可以用不可破坏的空格替换空格
  • 或者您可以将完整的地图写入文本,但这可能违背您的目的.

  • If you want single words it is rather easy. One way is using a regEx to identify words in a RTB. If you don't like the built-in way to define a word, I can post a code with user-defined separators.. But maybe you are happy with what have already.
  • If you need whole phrases (including spaces) to be clickable as one whole entity then you will have to be a little more inventive:
  • either you can add special characters around the phrases
  • or you can replace the spaces by non-breakable spaces
  • Or you could write a complete map into your text, but that probably defies your purpose.

除了简单的单词之外的任何其他内容都意味着您需要对文本进行一些更改.

Anything but simple words will mean that you will need to apply some changes to your text.

结果应该是一个函数,可以找到可点击文本的开头和结尾,并返回点击的单词/短语.

The result should be a function that can find the start and the end of the clickable text and return the clicked word/phrase.

  • 第二步是在 Dictionary 中查找文本,然后对值执行您想要执行的操作,例如在标签中显示翻译:
  • Step Two is to look up the text in a Dictionary<string, string> and do what you want to do with the value, e.g. show the translation in a Label:
if (dictEnGE.ContainsKey(theClickedWord)) 
    label.Text = dictEnGE[theClickedWord];

  • 为此,您必须首先通过添加键值对来准备字典:键是可点击的文本,值是您需要查找的任何内容:
  • Dictionary<string, string> dictEnGE = new Dictionary<string, string>();
    dictEnGE.Add("house", "Haus");
    dictEnGE.Add("man", "Mann");
    dictEnGE.Add("mouse", "Maus");
    //..
    

    所以整个解决方案是:准备一个字典,编写 Mouseclick 事件代码来找到被点击的文本,在字典中查找文本,用你找到的值做你的事情..

    So the whole solution is: Prepare a dictionary, code the Mouseclick event to find the clicked text, look up the text in the dictioanry, do your thing with the value you find..

    更新:这是一个获取点击词的函数:

    Update: Here is a function to get the clicked word:

    string getWordAtIndex(RichTextBox RTB, int index)
    {
        string wordSeparators = " .,;-!?\r\n\"";
        int cp0 = index;
        int cp2 = RTB.Find(wordSeparators.ToCharArray(), index);
        for (int c = index; c > 0; c--)
        { if (wordSeparators.Contains(RTB.Text[c])) { cp0 = c + 1; break; } }
        int l = cp2 - cp0;
        if (l > 0) return RTB.Text.Substring(cp0, l); else return "";
    }
    

    这里是你如何使用它:

    private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
    {
        string word = getWordAtIndex(richTextBox1, richTextBox1.SelectionStart);
        if (dictEnGE.ContainsKey(word)) aLabel.Text = dictEnGE[word];
    }
    

    这篇关于富文本框中的可点击文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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