我有一个richtextbox,它的文本是来自特定表的某些单词的串联。 (表格列是“单词”,“翻译”和“ID”)richtextbox中每个单词的额外信息richtextbox和sql表之间的链接
我需要当用户将鼠标悬停在每个单词上时,相关翻译会显示在单词的工具提示中。 (类似谷歌翻译,但在Windows窗体应用程序。)
有人可以指向我的解决方案吗?
我有一个richtextbox,它的文本是来自特定表的某些单词的串联。 (表格列是“单词”,“翻译”和“ID”)richtextbox中每个单词的额外信息richtextbox和sql表之间的链接
我需要当用户将鼠标悬停在每个单词上时,相关翻译会显示在单词的工具提示中。 (类似谷歌翻译,但在Windows窗体应用程序。)
有人可以指向我的解决方案吗?
使用Web浏览器控制和注入JavaScript解决了我的问题。 ;)
这将这样的伎俩......
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);
}
我不是精通C#,我也是品牌新来这个论坛。然而,在我看来,如果你要补充代码rene发布一个函数,查询你的翻译表并返回翻译文本,你会有这个(原谅我的伪代码屠杀 - 我很流利的vb。净,会很快学会了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;
}
然后改变刘若英的代码的最后部分(“//显示结果”)如下(我可怕的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#时进行一些工作,并提高我对在此论坛发帖的理解!让代码看起来正确是一个挑战。 。 。
感谢您的回复。但是,最大的问题是“...在哪里,在哪里,如何?”。因为在我的应用程序中,一个词可能有多个翻译基于其上下文。那么我们必须确切地指向相应的翻译ID。 – ARZ 2011-01-27 06:43:10
感谢您的回复。这是得到的话。但是,我们怎么能有相关的身份证和翻译?! – ARZ 2011-01-22 15:10:04