Share via


How to update cursor position programatically in TextBox C# winforms

Question

Wednesday, August 20, 2014 10:04 AM

Inside TextBox i am adding button control, i need to set cursor position of textbox after button control. And each time i can button inside text box i need to set cursor position after newly added button.

Note:

No text will be loaded in this textbox

Can anyone help me on this?

MSDN ALERTS

All replies (9)

Wednesday, August 20, 2014 2:07 PM âś…Answered

may this windows api function does what you want (not sure)

[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool SetCaretPos(int x, int y);

Regards, Nico

pdfaid, my blog


Wednesday, August 20, 2014 10:34 AM | 1 vote

You can control the cursor position by setting the TextBox.SelectionStart and TextBox.SelectionLength properties: http://social.msdn.microsoft.com/Forums/en-US/04362a62-8cbf-4d86-a1bc-2aba8e4978ca/cursor-position-in-textbox?forum=Vsexpressvcs.

However, for this to work, you probably need to fill the TextBox with some text underneath the button(s). You could for example set the Text property to some long string of spaces:

            textBox1.Text = "                                                                                ";
            textBox1.SelectionStart = 50;
            textBox1.SelectionLength = 0;

You will probably have to know before hand how many spaces that corresponds to the length of a button.


Wednesday, August 20, 2014 11:34 AM

Hi,

Thanks for the reply.

I know this workaround. But i cannot add any text in textbox is there any other alternative to achieve this?

MSDN ALERTS


Wednesday, August 20, 2014 1:10 PM

Perhaps the following article and code sample about how you could insert controls into a RichTextBox (replace the TextBox with a RichTextBox) using OLE can could help you: http://www.codeproject.com/Articles/12135/Inserting-images-into-a-RichTextBox-control-the-OL.

I am afraid there is no easier way of doing this than the solution I proposed in my previous reply. Without any text, there is no where to put the caret...


Wednesday, August 20, 2014 1:36 PM

maybe you should make a composite control;

put a button and a textbox on a usercontrol next to each other,  and go from there.

Regards, Nico

pdfaid, my blog


Wednesday, August 20, 2014 3:16 PM

Hi,

Thanks for the reply.

I cannot replace TextBox with RichTextBox . I have found a workaround by using SendKeys.Send(" ") cursor position can be updated.

But it is not proper way..

There has to be some other ways to do this..

Can any one guide me on this?

MSDN ALERTS


Thursday, August 21, 2014 11:59 AM

Hi Nico,

Awesome... It works ..

But when i press any Key like [Left, Right,  Mouse Buttons] cursor position moves to zeroth position. 

Can you please guide me how to retain cursor position all cases?

Regards,

Kannan.R

MSDN ALERTS


Thursday, August 21, 2014 1:14 PM

I think the problem is you also need to set focus to the textbox:

            textBox1.Focus();
            textBox1.SelectionStart = 3;
            textBox1.SelectionLength = 0;

Hope that helps
Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.


Wednesday, August 27, 2014 10:46 AM

Hi,

As i have explained in my case there will be no text in text box. so Selection Start, length properties will not be applicable.

[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool SetCaretPos(int x, int y);Sorry i have checked that. Cursor position moves to 0th index every time when it gets focused. I have tried to set position in got focus event also but no useCan any one help me on this.

MSDN ALERTS