Cursor position in NumericUpDown control

T

Tantra Veda

Hello C# gurus,
I have a question about finding cursor position in NumericUpDown control. On
my form I have a numericUpDown control with 2 decimal places. I want to
increment value in the numeric box by 1 if my cursor is in front of the
decimal point. If the cursor is after the decimal point, I want to increment
the value by 0.1. How do I accomplish this? Is there a way to find out the
cusror position in the control relative to decimal point?

Greatly appreciate any help you can provide,

Shridev
 
N

Nicholas Paldino [.NET/C# MVP]

Shridev,

I don't think that this is possible out of the box. The control doesn't
expose anything about the text that is in the box.

If you were to do it, you would have to take a look at the Cursor class
in the System.Windows.Forms namespace. It has a static property, Position
which gives you the position of the cursor in screen coordinates. Once you
have that, you can pass that point to the PointToClient method on the
numeric up/down control. This will give you the coordinates relative to the
control.

At that point, you are going to have to determine on which side of the
decimal point it is. I would try and use the TextRenderer class in the
System.Windows.Forms namespace (it's a new class in 2.0, and I wouldn't even
think of trying to calculate this stuff manually) and it can tell you the
width of text given a font and the text. You will have to parse apart your
string to get the widths, and then calculate the offsets to see if your
cursor is in the right place.

Hope this helps.
 

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