Cursor location in Textbox

G

Guest

I want to get the cursor location in a textbox. If text in the box is selected, txtName.SelectionStart returns the start of the text. If no text is selected, SelectionStart returns 0 regardless of cursor location in control.

Is there a property that would return where the cursor is located? I am trying to use this property in combination with a context menu for that control. Specifically, I want to insert text at the current location of the cursor.
 
G

Gary Milton

Hi Genojoe,

SelectionStart is used to return the position of the cursor and will work
even if no text is selected. It may be something to do with the way you are
doing your processing that is somehow resetting the cursor position back to
the beginning of the textbox.

When you get focus back on the textbox again after performing your
processing, what position is the cursor at?

Do you have some code snippets you can post so that we can try and determine
what is going wrong. Also, it would be useful to know what version of the
VS.NET and the .NET Framework you are running on incase it is a known bug
with that particular version.

Gary

Genojoe said:
I want to get the cursor location in a textbox. If text in the box is
selected, txtName.SelectionStart returns the start of the text. If no text
is selected, SelectionStart returns 0 regardless of cursor location in
control.
Is there a property that would return where the cursor is located? I am
trying to use this property in combination with a context menu for that
control. Specifically, I want to insert text at the current location of the
cursor.
 
G

Guest

You are 100% correct; I can restate my problem as follows

I have a textbox containing "Hello World" and the cursor is after the "d". When I right click between " " and "W", I would like to detect the new location (6). Instead, SelectionStart responds with the cursor location prior to doing a right-click (11)

Is there a way for the Right-Click command to detect the new location of 6 instead of 11? Can the right-click command be forced to change the cursor location? Either will do
 
G

Guest

For completeness, I should add the following to my previous response

1. I am using a context sensitive menu and have the following code in my form

Me.TextBox1.ContextMenu = Me.ContextMenu
Me.TextBox1.Location = New System.Drawing.Point(40, 56
Me.TextBox1.Name = "TextBox1
Me.TextBox1.TabIndex =
Me.TextBox1.Text = "Hello World

Private Sub ContextMenu1_Popup(....) Handles ContextMenu1.Popu
Debug.WriteLine(Me.TextBox1.SelectionStart
End Su

This problem still exists without the context menu. Instead you see the Undo...Cut...Copy menu but the result is the same
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top