C# .NET - how to find out current position of cursor in textbox using c#

Asked By kamaldeep singh on 19-Jun-08 01:29 AM

how to find out current position of cursor in textbox using c#

how to find out current position of cursor in textbox using c#.how to find out current position of cursor in textbox using c#

Using javascript - Sagar P replied to kamaldeep singh on 19-Jun-08 01:35 AM

You can find it using javascript function.

Just write a js function as;

function getMouseXY(e)

{
tempX = e.clientX ;
tempY = e.clientY ;

Also you can get current position by using

event.clientX

event.clientY

Also refer this;

function showit() {
  document.forms['theform'].xcoord.value=event.x;
  document.getElementById('spanx').innerHTML='x = '+event.x;
  document.forms.theform.ycoord.value=event.y;
  document.getElementById('spany').innerHTML='y = '+event.y;
}

function showitMOZ(e) {
  document.forms['theform'].xcoord.value=e.pageX;
  document.getElementById('spanx').innerHTML='x = '+e.pageX;
  document.getElementById('spany').innerHTML='y = '+e.pageY;
  document.forms.theform.ycoord.value=e.pageY;
}

if (!document.all) {
  window.captureEvents(Event.CLICK);
  window.onclick=showitMOZ;
} else {
  document.onclick=showit;
}

You can call this function on any event;

Also go thr this link;

http://www.brenz.net/snippets/xy.asp

Best Luck!!!!!!!!!!!!!!
Sujit.

here is the code - Vasanthakumar D replied to kamaldeep singh on 19-Jun-08 01:52 AM

Hi,

here is the code for getting the textbox cursor postition in C#....

int xPos = textBox1.Cursor.HotSpot.X;

int yPos = textBox1.Cursor.HotSpot.Y;

Retrieve the Caret Position by Using an Extended TextBox Class - Deepak Ghule replied to kamaldeep singh on 19-Jun-08 01:58 AM



I think , u might have wanted a caret position (which is blinked while typing)

for that u have to Extend TextBox Class


using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
namespace TextBoxUtils
{
   /// <summary>
   /// struct to handle the caret (L, C) co-ordinates
   /// L = one-based line number of the line containing the caret
   /// C = one-based column number in the line containing the caret
   /// </summary>
   public struct CharPoint
   {
      private int l, c;

      public static readonly CharPoint Empty;

      static CharPoint()
      {
         CharPoint.Empty = new CharPoint();
      }

      public CharPoint(int l, int c)
      {
         this.l = l;
         this.c = c;
      }

      public override string ToString()
      {
         return(String.Format("{{L={0}, C={1}}}", this.l, this.c));
      }

   }

   public class ExtTextBox: TextBox
   {
      [DllImport("user32")] private static extern IntPtr
                            SendMessage(HandleRef hWnd, int msg,
                                        int wParam, int lParam);
      [DllImport("user32")] private static extern int
                            GetCaretPos(ref Point lpPoint);
      private int EM_LINEINDEX    = 0xbb;
      private int EM_LINEFROMCHAR = 0xc9;
      private int EM_GETSEL       = 0xb0;

      /// <summary>
      /// Gets the caret current (X, Y) position.
      /// </summary>
      /// <value>
      /// Point struct
      /// </value>
      public Point GetCaretXYPosition
      {
         get
         {
            Point pt = Point.Empty;
            // get a Point struct with the caret current (X, Y)
            // position
            GetCaretPos(ref pt);
            // return the Point struct with the caret current
            // (X, Y) position
            return pt;
         }
      }

      /// <summary>
      /// Gets the caret current (L, C) position.
      /// </summary>
      /// <value>
      /// CharPoint struct
      /// </value>
      public CharPoint GetCaretLCPosition
      {
         get
         {
            CharPoint cpt = CharPoint.Empty;
            // save the handle reference for the ExtToolBox
            HandleRef hr = new HandleRef(this, base.Handle );
            // Send the EM_LINEFROMCHAR message with the value of
            // -1 in wParam.
            // The return value is the zero-based line number
            // of the line containing the caret.
            int l = (int)SendMessage(hr,EM_LINEFROMCHAR, -1, 0);
            // Send the EM_GETSEL message to the ToolBox control.
            // The low-order word of the return value is the
            // character position of the caret relative to the
            // first character in the ToolBox control,
            // i.e. the absolute character index.
            int sel = (int)SendMessage(hr, EM_GETSEL,0, 0);
            // get the low-order word from sel
            int ai  = sel & 0xffff;
            // Send the EM_LINEINDEX message with the value of -1
            // in wParam.
            // The return value is the number of characters that
            // precede the first character in the line containing
            // the caret.
            int li = (int)SendMessage(hr,EM_LINEINDEX, -1, 0);
            // Subtract the li (line index) from the ai
            // (absolute character index),
            // The result is the column number of the caret position
            // in the line containing the caret.
            int c = ai - li;
            cpt = new CharPoint(l+1,c+1);
            // Add 1 to the l and c since these are zero-based.
            // Return a CharPoint with the caret current (L,C)
            // position
            return new CharPoint(l+1,c+1);
         }
      }
   }
}



Refer this link for details


http://www.codeguru.com/csharp/csharp/cs_controls/custom/article.php/c8145/




textBox1.Cursor.HotSpot.X; is coming in c# - kamaldeep singh replied to Vasanthakumar D on 19-Jun-08 01:59 AM
textBox1.Cursor.HotSpot.X; is  not coming in c#
if you wnat position of character... - Vasanthakumar D replied to kamaldeep singh on 19-Jun-08 01:59 AM

Hi,

if you want to get the cursour's current charatcter postion... use this...

int Pos = textBox1.SelectionStart;

Use SelectionStart property - Santhosh N replied to kamaldeep singh on 19-Jun-08 02:20 AM

You can make use of SelectionStart property which returns the current position of the cursor of the textbox..

int pos = txtName.SelectionStart


find out current position of cursor in textbox using c# - Swapnil Salunke replied to kamaldeep singh on 19-Jun-08 02:37 PM
Hello KamalDeep
There is a proerty in the of text box which gives the starting point of the selected text
Now if you want it for mulitline text box then it can be done using the following code

int Pos = txtbox1.selectionstart;

For position and line number in multiline textbox
taken from http://www.vb-helper.com/howto_track_textbox_caret.html

Private Sub CheckPosition()
Dim char_pos As Long
Dim row As Long
Dim col As Long

char_pos = SendMessage(Text1.hwnd, EM_GETSEL, 0, 0)
char_pos = char_pos \ &H10000

row = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, _
char_pos, 0) + 1
col = char_pos - SendMessage(Text1.hwnd, EM_LINEINDEX, _
-1, 0) + 1

lblPosition.Caption = "(" & Format$(row) & ", " & _
Format$(col) & ")"
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As _
Integer)
CheckPosition
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As _
Integer)
CheckPosition
End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
CheckPosition
End Sub

Private Sub Text1_MouseUp(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
CheckPosition
End Sub

Happy Coding take care

Numeric String format showing 2000 as 2K and 30000 as 30K - Majid Bhutta replied to kamaldeep singh on 02-Apr-09 05:54 AM
HI Dears!
I want to know the string format to show the numbers in the following short format
2000 as 2K
30000 as 30K
50000000 as 50M

 and so on. I need to format this numeric column in the datagridview in C#.
What may be the possible way out , i have tried custom format but was unable to get it.
Any idea would be highly appreciated.
Thanks in advance.