By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

TextBox - Character under Mouse

P: n/a
Hi

I want to be able to work out the character under the mouse as it
moves over a TextBox... I was thinking about aproaching it by
measuring the string and working it out that way. I am not convinced
this is a) going to be very reliable, or b) very tidy.

Anyone know of a good starting point for this kind of thing?

THanks

Blu.
Nov 20 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
This is probably doable if you are using single line textboxes, what you are
talkin about is measuring the character width for a particular character and
then seeing if the point is within that area. Thats easy if all the chars
are the same, but as you already know they are not likely to be so you cant
even do a division.

Depending on what you wan to do, would it be possible to use a set of labels
which represent the characters underneath the textbox for display & select
purposes. and use the textbox for edit, on change the label sets alters ?

Messy, but workable.

HTH
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"BluDog" <ne**@nospam.bludog.net> wrote in message
news:f9********************************@4ax.com...
Hi

I want to be able to work out the character under the mouse as it
moves over a TextBox... I was thinking about aproaching it by
measuring the string and working it out that way. I am not convinced
this is a) going to be very reliable, or b) very tidy.

Anyone know of a good starting point for this kind of thing?

THanks

Blu.

Nov 20 '05 #2

P: n/a
* BluDog <ne**@nospam.bludog.net> scripsit:
I want to be able to work out the character under the mouse as it
moves over a TextBox... I was thinking about aproaching it by
measuring the string and working it out that way. I am not convinced
this is a) going to be very reliable, or b) very tidy.


P/invoke on 'SendMessage' + 'EM_CHARFROMPOS'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #3

P: n/a
I might be mistaken, but I thought he meant from a graphical point of view,
in other words, if you had the string ABCD in the box, that he needed to
select say, any particular letter he was hovering over. Will your function
do that ?, if so, im impressed as I did not know this function existed, its
not really something most people would want is it ?

Cheers - OHM

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eG****************@TK2MSFTNGP11.phx.gbl...
* BluDog <ne**@nospam.bludog.net> scripsit:
I want to be able to work out the character under the mouse as it
moves over a TextBox... I was thinking about aproaching it by
measuring the string and working it out that way. I am not convinced
this is a) going to be very reliable, or b) very tidy.


P/invoke on 'SendMessage' + 'EM_CHARFROMPOS'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 20 '05 #4

P: n/a
* "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
I might be mistaken, but I thought he meant from a graphical point of view,
in other words, if you had the string ABCD in the box, that he needed to
select say, any particular letter he was hovering over. Will your function
do that ?, if so, im impressed as I did not know this function existed, its
not really something most people would want is it ?


According to my interpreation of the documentation, that's what
'EM_CHARFROMPOS' is doing:

<URL:http://msdn.microsoft.com/library/en-us/shellcc/platform/commctls/editcontrols/editcontrolreference/editcontrolmessages/em_charfrompos.asp>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #5

P: n/a

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:Oi**************@TK2MSFTNGP12.phx.gbl...
P/invoke on 'SendMessage' + 'EM_CHARFROMPOS'.
I might be mistaken, but I thought he meant from a graphical point of view, in other words, if you had the string ABCD in the box, that he needed to
select say, any particular letter he was hovering over. Will your function
do that ?, if so, im impressed as I did not know this function existed, its not really something most people would want is it ?


Yes, this function will do this. I used it myself in a VB6 app where I had a
Rich Text Box with "hot areas" for replacement text. When you clicked in the
control, this message would determine if you were over one of those areas
and float a text box over the control for you to type the new value. Worked
well once I got the proper declaration for the function....
Nov 20 '05 #6

P: n/a
* "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
I might be mistaken, but I thought he meant from a graphical point of view,
in other words, if you had the string ABCD in the box, that he needed to
select say, any particular letter he was hovering over. Will your function
do that ?, if so, im impressed as I did not know this function existed, its
not really something most people would want is it ?


I forgot to add that 'MeasureString' is rather useless in this
situation, because the textbox is drawn using GDI, and 'MeasureString'
uses GDI+ for measuring, which will result in inaccurate results.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #7

P: n/a
Thanks for pointing that out

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
* "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
I might be mistaken, but I thought he meant from a graphical point of view, in other words, if you had the string ABCD in the box, that he needed to
select say, any particular letter he was hovering over. Will your function do that ?, if so, im impressed as I did not know this function existed, its not really something most people would want is it ?


I forgot to add that 'MeasureString' is rather useless in this
situation, because the textbox is drawn using GDI, and 'MeasureString'
uses GDI+ for measuring, which will result in inaccurate results.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 20 '05 #8

P: n/a
>I want to be able to work out the character under the mouse as it
moves over a TextBox... I was thinking about aproaching it by
measuring the string and working it out that way. I am not convinced
this is a) going to be very reliable, or b) very tidy.


Thanks guys... i thought i'd include my solution to help anyone else
that may come across this thread:

Oh, it's an inherited textbox...

<code>
#Region "Overrides"

Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)

Dim MyPoint As Point = Me.PointToClient(New Point(e.X, e.Y))
Dim lResult As Integer = SendMessage(Handle, EM_CHARFROMPOS,
0, e.X + (e.Y * &H0))

Try
Debug.WriteLine(Text.Substring(lResult, 1))
Catch
'Beyond the last character of the string
End Try

MyBase.OnMouseMove(e)

End Sub

#End Region

#Region "Unmanaged"

Private Declare Auto Function SendMessage Lib "user32" ( _
ByVal handle As IntPtr, _
ByVal msg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer

Private Const EM_CHARFROMPOS As Integer = &HD7

#End Region

</code>

Cheers

Blu.
Nov 20 '05 #9

P: n/a
* BluDog <ne**@nospam.bludog.net> scripsit:
Thanks guys... i thought i'd include my solution to help anyone else
that may come across this thread:


Thank your for sharing the complete solution :-)!

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #10

P: n/a
On 20 Jul 2004 14:48:09 +0200, hi***************@gmx.at (Herfried K.
Wagner [MVP]) wrote:
* BluDog <ne**@nospam.bludog.net> scripsit:
Thanks guys... i thought i'd include my solution to help anyone else
that may come across this thread:


Thank your for sharing the complete solution :-)!


A bit premature I feel... it only works on the first line of textbox,
if the mouse is over the second (or subsequent) on a multiline text
box it returns the equivalent character on the first line...

Any ideas?

Cheers

Blu

Nov 20 '05 #11

P: n/a
On 20 Jul 2004 14:48:09 +0200, hi***************@gmx.at (Herfried K.
Wagner [MVP]) wrote:
* BluDog <ne**@nospam.bludog.net> scripsit:
Thanks guys... i thought i'd include my solution to help anyone else
that may come across this thread:


Thank your for sharing the complete solution :-)!


Though i shoud include the revised code:

Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)

SetDropPosition(New Point(e.X, e.Y))
MyBase.OnMouseMove(e)

End Sub

Protected Overrides Sub OnDragOver(ByVal e As DragEventArgs)

SetDropPosition(Me.PointToClient(New Point(e.X, e.Y)))
MyBase.OnDragOver(e)

End Sub
Private Sub SetDropPosition(ByVal pt As Point)

Dim newChar As Integer = SendMessage(Handle, EM_CHARFROMPOS,
0, pt)

'Work with newChar (DragDrop etc)
'Only returns the character position on the first line of a
multiline textbox

End Sub

Private Declare Auto Function SendMessage Lib "user32" ( _
ByVal handle As IntPtr, _
ByVal msg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Point) As Integer

Private Const EM_CHARFROMPOS As Integer = &HD7

Nov 20 '05 #12

P: n/a
* BluDog <ne**@nospam.bludog.net> scripsit:
Thanks guys... i thought i'd include my solution to help anyone else
that may come across this thread:


Thank your for sharing the complete solution :-)!


A bit premature I feel... it only works on the first line of textbox,
if the mouse is over the second (or subsequent) on a multiline text
box it returns the equivalent character on the first line...


\\\
....
Private Declare Auto Function SendMessage Lib "user32.dll" ( _
ByVal handle As IntPtr, _
ByVal msg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Integer _
) As Integer

Private Const EM_CHARFROMPOS As Integer = &HD7

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.Text = New StreamReader("C:\WINDOWS\WIN.INI").ReadToEnd
End Sub

Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
Dim MyPoint As Point = New Point(e.X, e.Y) 'Me.TextBox1.PointToClient(New Point(e.X, e.Y))
Dim w As WordConverter
w.LoWord = CShort(MyPoint.X)
w.HiWord = CShort(MyPoint.Y)
Dim lResult As Integer = SendMessage(Me.TextBox1.Handle, EM_CHARFROMPOS, 0, w.LongValue)
Me.Text = Mid(Me.TextBox1.Text, New WordConverter(lResult).LoWord, 1)
End Sub
End Class

<StructLayout(LayoutKind.Explicit)> _
Public Structure WordConverter
<FieldOffset(0)> Public LongValue As Integer
<FieldOffset(0)> Public LoWord As Short
<FieldOffset(2)> Public HiWord As Short

Public Sub New(ByVal LoWord As Short, ByVal HiWord As Short)
Me.LoWord = LoWord
Me.HiWord = HiWord
End Sub

Public Sub New(ByVal LongValue As Integer)
Me.LongValue = LongValue
End Sub
End Structure
///
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #13

P: n/a
Herfried - thanks dude!
Nov 20 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.