richtextbox 中每个单词的额外信息 - richtextbox 和 sql 表之间的链接

发布于 2024-10-13 05:03:52 字数 160 浏览 2 评论 0原文

我有一个 Richtextbox,它的文本是特定表中一些单词的串联。 (表列是“单词”、“翻译”和“id”)

我需要当用户将鼠标悬停在每个单词上时,相关的翻译会显示在单词的工具提示中。 (类似于谷歌翻译,但在 Windows 窗体应用程序中。)

有人可以指出我的解决方案吗?

I have a richtextbox that its text is a concatenation of some words from specific table. (table columns are 'word','translate' and 'id')

I need that when a user hover on each word, the related translate is showed in word's tooltip.
(something like google translate but in windows form application.)

Can somebody point me towards a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

像你 2024-10-20 05:03:52

使用 Web 浏览器控制和注入 JavaScript 解决了我的问题。 ;)

using Web-browser Control and Injecting JavaScript solved my problem. ;)

泛滥成性 2024-10-20 05:03:52

这就能解决问题...

private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
     // whitespace definition
     char[] whitespace = new char[] { ' ', '\r', '\n', '\t' };

     int charPosition = this.richTextBox1.GetCharIndexFromPosition(e.Location);

     string fullText = this.richTextBox1.Text;

     // if we are on whitespace, exit
     if (whitespace.Contains(fullText[charPosition]))
     {
         return;
     }

     // find a whitespace towards the start of the text
     int firstWhiteSpace = charPosition;
     while (firstWhiteSpace > 0
         && firstWhiteSpace < fullText.Length
         && !whitespace.Contains(fullText[firstWhiteSpace]))
     {
         firstWhiteSpace--;
     }
     if (firstWhiteSpace!=0)
         firstWhiteSpace++;
     // find the next whitespace
     int lastWhiteSpace = fullText.IndexOfAny(whitespace, charPosition);
     if (lastWhiteSpace == -1)
         lastWhiteSpace = fullText.Length;

     // substring the word out of the flat text
     string word = fullText.Substring(
         firstWhiteSpace, 
         lastWhiteSpace - firstWhiteSpace);

     // show the result
     label1.Text = String.Format("pos:{0} fsp:{1}, lsp:{2}, len:{3}, word:{4}", 
         charPosition, 
         firstWhiteSpace, 
         lastWhiteSpace, 
         fullText.Length, word);

 }

This will do the trick...

private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
     // whitespace definition
     char[] whitespace = new char[] { ' ', '\r', '\n', '\t' };

     int charPosition = this.richTextBox1.GetCharIndexFromPosition(e.Location);

     string fullText = this.richTextBox1.Text;

     // if we are on whitespace, exit
     if (whitespace.Contains(fullText[charPosition]))
     {
         return;
     }

     // find a whitespace towards the start of the text
     int firstWhiteSpace = charPosition;
     while (firstWhiteSpace > 0
         && firstWhiteSpace < fullText.Length
         && !whitespace.Contains(fullText[firstWhiteSpace]))
     {
         firstWhiteSpace--;
     }
     if (firstWhiteSpace!=0)
         firstWhiteSpace++;
     // find the next whitespace
     int lastWhiteSpace = fullText.IndexOfAny(whitespace, charPosition);
     if (lastWhiteSpace == -1)
         lastWhiteSpace = fullText.Length;

     // substring the word out of the flat text
     string word = fullText.Substring(
         firstWhiteSpace, 
         lastWhiteSpace - firstWhiteSpace);

     // show the result
     label1.Text = String.Format("pos:{0} fsp:{1}, lsp:{2}, len:{3}, word:{4}", 
         charPosition, 
         firstWhiteSpace, 
         lastWhiteSpace, 
         fullText.Length, word);

 }
唔猫 2024-10-20 05:03:52

我对 C# 不太熟悉,而且我也是这个论坛的新手。然而,在我看来,如果你用一个函数来补充 rene 发布的代码,该函数查询你的翻译表并返回翻译文本,那么你就会得到这个(原谅我的伪代码屠杀 - 我对 vb 非常流利)。 net,很快就会学习 C# 语法):

Private String TranslatedWord(ByVal SelectedWord String)
    {
        //Use ADO and SQL to retrieve the Translation(s) associated with the submitted Word

        // A string SQL Statement (the GROUP ON is in case there are multiple instances of the same word, with different translations (synonyms). THis SQL Should return a a single record for each possible translation of the submitted word (more than one result possible):
            Dim SQL as String = _
            "SELECT tr.ID, tr.Translate " & _
            "FROM MyTranslationTable AS tr " & _
            "WHERE tr.Word LIKE @Word"

        //Since I could be ALL DAY struggling to write the C# code for this, I will just step through it in "pseudocode": 
        // 1. Execute the SQL using ADO.Net, set up the @Word Param in your command, and return a sqlDataReader
        // 2. Iterate through the returned records, and append the Translation results to a System.Text.Stringbuilder object. Delimit each returned value with a semi-colon (or your delimiter of choice). 
        // Return the Stringbuilder.ToString property as the result of this function;

}

然后按如下方式更改 rene 代码的最后一部分(“//显示结果”)(适合纠正我可怕的 C# 问题!):

        private void richTextBox1_MouseMove(object sender, MouseEventArgs e)                                        
    {                                             
        // whitespace definition                                             
        char[] whitespace = new char[] { ' ', '\r', '\n', '\t' };                                                                                     
        int charPosition = this.richTextBox1.GetCharIndexFromPosition(e.Location);                                                                                     
        string fullText = this.richTextBox1.Text;

        // if we are on whitespace, exit                                             
        if (whitespace.Contains(fullText[charPosition]))                                             
        {                                                 
            return;                                             
        }                                                                                     
        // find a whitespace towards the start of the text                                             
        int firstWhiteSpace = charPosition;                                             
        while (firstWhiteSpace > 0                                                 
            && firstWhiteSpace < fullText.Length                                                 
            && !whitespace.Contains(fullText[firstWhiteSpace]))                                             
            {                                                 
            firstWhiteSpace--;                                             
        }                                             
        if (firstWhiteSpace!=0)                                                 
            firstWhiteSpace++;

        // find the next whitespace                                             
        int lastWhiteSpace = fullText.IndexOfAny(whitespace, charPosition);                                             
        if (lastWhiteSpace == -1)                                                 
            lastWhiteSpace = fullText.Length;

        // substring the word out of the flat text                                             
        string word = fullText.Substring(                                                 
            firstWhiteSpace,                                                  
            lastWhiteSpace - firstWhiteSpace);  

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
        //My CHanges start here, and will likely require 
        // some tweaking . . .

        //Use the function I have poorly described above to retreive the 
        //translation(s) for the current Word:
        string TRANSLATION = TranslatedWord(word);                                                         

        // show the result     
        //Since there are so many minor but important differences between C# and VB, I am not going to attempt
        //to account for them. Essentially, display the translated word instead of the word over which the mouse is hovering:                
        label1.Text = String.Format("pos:{0} fsp:{1}, lsp:{2}, len:{3}, word:{4}",                                                  
            charPosition,                                                  
            firstWhiteSpace,                                                  
            lastWhiteSpace,                                                  
            fullText.Length, TRANSLATION);      

    }

如果有帮助,我可以崩溃相当快地给出了 vb.net 代码,但除非有帮助,否则我不会这样做。

希望这有帮助。我将不得不努力学习 C#,并提高我对在这个论坛上发帖的理解!让代码看起来正确是一个挑战。 。 。

I am not fluent in C#, and I am also brand new to this forum. However, it looks to me like if you were to supplement the code rene posted with a function which queries your Translation table and returns the translation text, you would then have this (forgive my pseudo-code butchery - I am very fluent in vb.net, gonna learn the C# syntax soon):

Private String TranslatedWord(ByVal SelectedWord String)
    {
        //Use ADO and SQL to retrieve the Translation(s) associated with the submitted Word

        // A string SQL Statement (the GROUP ON is in case there are multiple instances of the same word, with different translations (synonyms). THis SQL Should return a a single record for each possible translation of the submitted word (more than one result possible):
            Dim SQL as String = _
            "SELECT tr.ID, tr.Translate " & _
            "FROM MyTranslationTable AS tr " & _
            "WHERE tr.Word LIKE @Word"

        //Since I could be ALL DAY struggling to write the C# code for this, I will just step through it in "pseudocode": 
        // 1. Execute the SQL using ADO.Net, set up the @Word Param in your command, and return a sqlDataReader
        // 2. Iterate through the returned records, and append the Translation results to a System.Text.Stringbuilder object. Delimit each returned value with a semi-colon (or your delimiter of choice). 
        // Return the Stringbuilder.ToString property as the result of this function;

}

Then change the last part of rene's code ("//Show the result") as follows (suitable corrected for my horrible C# problem!):

        private void richTextBox1_MouseMove(object sender, MouseEventArgs e)                                        
    {                                             
        // whitespace definition                                             
        char[] whitespace = new char[] { ' ', '\r', '\n', '\t' };                                                                                     
        int charPosition = this.richTextBox1.GetCharIndexFromPosition(e.Location);                                                                                     
        string fullText = this.richTextBox1.Text;

        // if we are on whitespace, exit                                             
        if (whitespace.Contains(fullText[charPosition]))                                             
        {                                                 
            return;                                             
        }                                                                                     
        // find a whitespace towards the start of the text                                             
        int firstWhiteSpace = charPosition;                                             
        while (firstWhiteSpace > 0                                                 
            && firstWhiteSpace < fullText.Length                                                 
            && !whitespace.Contains(fullText[firstWhiteSpace]))                                             
            {                                                 
            firstWhiteSpace--;                                             
        }                                             
        if (firstWhiteSpace!=0)                                                 
            firstWhiteSpace++;

        // find the next whitespace                                             
        int lastWhiteSpace = fullText.IndexOfAny(whitespace, charPosition);                                             
        if (lastWhiteSpace == -1)                                                 
            lastWhiteSpace = fullText.Length;

        // substring the word out of the flat text                                             
        string word = fullText.Substring(                                                 
            firstWhiteSpace,                                                  
            lastWhiteSpace - firstWhiteSpace);  

        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
        //My CHanges start here, and will likely require 
        // some tweaking . . .

        //Use the function I have poorly described above to retreive the 
        //translation(s) for the current Word:
        string TRANSLATION = TranslatedWord(word);                                                         

        // show the result     
        //Since there are so many minor but important differences between C# and VB, I am not going to attempt
        //to account for them. Essentially, display the translated word instead of the word over which the mouse is hovering:                
        label1.Text = String.Format("pos:{0} fsp:{1}, lsp:{2}, len:{3}, word:{4}",                                                  
            charPosition,                                                  
            firstWhiteSpace,                                                  
            lastWhiteSpace,                                                  
            fullText.Length, TRANSLATION);      

    }

If it would be helpful, I could bust out the vb.net code for this fairly quickly, but I wasn't going to do that unless it would be helpful.

Hope that's helpful. I am going to have to work a little at learning C#, and at improving my understanding of posting in this forum! Getting the code to look right is proving a challenge . . .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文