|
-
Jan 6th, 2009, 09:55 AM
#1
Thread Starter
Addicted Member
[RESOLVED][3.0/LINQ] How to get word in RTB that mouse is over
What I am trying to do is find the word that I hover the mouse over and rest it against a list of keywords. I've googled around trying to find some information on how to do this but have only come up with some examples done in VB 6 that I have failed to try and do in C#. Can anyone help me out iwth this?
Last edited by Tewl; Jan 6th, 2009 at 05:12 PM.
-
Jan 6th, 2009, 05:12 PM
#2
Thread Starter
Addicted Member
Re: [3.0/LINQ] How to get word in RTB that mouse is over
I managed to sort it on my own. Here is an example of what I was looking for.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
richTextBox1.AppendText("This is a test word program!\n");
richTextBox1.AppendText("Hello there, how are you?\n");
richTextBox1.AppendText("Stop! Hammer time.");
label1.Text = "";
richTextBox1.MouseMove += new MouseEventHandler(richTextBox1_MouseMove);
}
void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
int pos = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y));
int l = richTextBox1.Text.Length;
if (l < 1)
label1.Text = "";
else
{
int s;
int end;
string c = string.Empty;
c = richTextBox1.Text.Substring(pos, 1);
Regex r = new Regex(@"[\s,\n,!,\,,\\,\/,\.,\?,:,;,`,~,\-,]");
if (r.IsMatch(c))
label1.Text = "";
else
{
for (s = pos; s > 0; --s)
{
c = richTextBox1.Text.Substring(s, 1);
if (r.IsMatch(c))
{
s++;
break;
}
}
for (end = pos; end < l; end++)
{
c = richTextBox1.Text.Substring(end, 1);
if (r.IsMatch(c))
break;
}
if ((end - s) > 0)
{
c = richTextBox1.Text.Substring(s, end - s);
label1.Text = "\"" + c + "\"";
}
else
label1.Text = "";
}
}
}
}
}
The regex pattern isn't perfect for pulling out words, I know. I also notice that if the rtb is multiline and if I click under a word where there is a blank space it will return the word above rather than no value. Any serious comments or suggestions welcome.
Last edited by Tewl; Jan 7th, 2009 at 08:08 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|