Cursor Position in KeyPressEvent

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a TextBox that I am putting some conditions on, such as restricting
the number of decimal places that the user can enter. I have been using the
KeyPress event, which allow me to examine the current text and prevent
inappropriate keys from being pressed. But I can not figure out how to
determine where in the text the text cursor is. For example, a digit key
might be allowed before the decimal place, but not allowed after, but I can
not determine where the key they pressed is going to be placed, there does
not appear to be a TextBox member that provides this information. Is there
one? Or can it be done another way? Thanks.
 
Richard said:
I have a TextBox that I am putting some conditions on, such as restricting
the number of decimal places that the user can enter. I have been using the
KeyPress event, which allow me to examine the current text and prevent
inappropriate keys from being pressed. But I can not figure out how to
determine where in the text the text cursor is. For example, a digit key
might be allowed before the decimal place, but not allowed after, but I can
not determine where the key they pressed is going to be placed, there does
not appear to be a TextBox member that provides this information. Is there
one? Or can it be done another way? Thanks.
Try TextBox.SelectionStart

Cheers
JB
 
Hi Richard,

Thank you for posting.

Would you tell me what the "decimal place" means?

There's a SelectionStart property of the TextBox, which you can use to get
or set the starting point of text selected in the text.

If you know the position the text cursor might be placed, you could use the
SelectionStart property of the TextBox to put the text cursor to the
desired place.

Hope this is helpful to you.




Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
Thanks SelectionStart was exactly what I was looking for. By the decimal
place I meant the ".", I needed to know whether the cursor was to the left or
right of the "." to know whether more numerals were allowed. Thanks again.
 
Hi Richard,

You're welcome!

If you have any other questions or need anything else, please don't
hesitate to contact us. It is always our pleasure to be of assistance.

Have a nice day!

Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 

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

Back
Top