2

I want cursor index from datagridviewcell.
example: Cell contains the text "ABCDE". In this AB|CDE consider pipe as a blinking cursor which can be changed with a mouseclick. so, I need cursor position.

Detail: I've made a small project in which on button click adds text to datagridviewcell. But I want to add text to a specific position in that cell before or after the text depends on the cursor placed with a mouse.

If there is any solution in which I can fetch all text from the right of cursor then I'll do the long way getting cursor position.

NajiMakhoul
  • 1,623
  • 2
  • 16
  • 30
bonny
  • 688
  • 1
  • 14
  • 33
  • 1
    Does this help… [While editing datagridview cell; position cursor anywhere with mouse click](https://stackoverflow.com/questions/29701625/while-editing-datagridview-cell-position-cursor-anywhere-with-mouse-click) – JohnG Jan 21 '21 at 13:52
  • When or what would trigger you needing to know where the cursor was in a cell. In other words, the user clicks three times into a cell to edit the cell and this sets the pipe cursor position in the text, then the user types and/or moves the cursor position… at which point in the above scenario would you need to know where the cursor was? Again, what would trigger you needing to know where the cursor was? If it is external of the grid, then you will most likely need to keep track of this position globally. – JohnG Jan 21 '21 at 13:53
  • [Partially select the text of a DataGridView cell when clicked](https://stackoverflow.com/a/54777466/7444103). Handling the TextBox control (EditingControl), you can use standard means to get the caret position and insert text anywhere you like. You can also replace the EditingControl with a Custom Control of your own. – Jimi Jan 21 '21 at 17:10
  • Put the text you want to insert at the cursor on the clipboard and paste it. – Mary Jan 23 '21 at 04:59
  • @Mary How, I am currently writing as `dgvHeader.SelectedRows(0).Cells("CONTENTS").Value = dgvHeader.SelectedRows(0).Cells("CONTENTS").Value.ToString.Insert(0, tag1)` here 0 is cursor position which I want to get. How with clipboard I insert it ? Insert function needs a start index – bonny Jan 23 '21 at 05:50
  • @JohnG I actually checked that link but not thoroughly. I read the comment in that answer and the user said it's not that good method so I just passed it. Still I am checking it again if I get something.. I want that user selects the position in the cell on mouseclick but I need latest cursor position when he leaves the cell so I guess it should be `cellEndEdit` event. After selecting the position he goes to press the button and on button click the content should be pasted from that cursor point. – bonny Jan 23 '21 at 06:06
  • @Jimi That's a great example but all text selected after the last space I guess not from the cursor point. If I get the text after cursor point then I can do the long method like total character in string - string length from right side of cursor or something. – bonny Jan 23 '21 at 06:13
  • That code shows that handling the `EditingControlShowing` event you can get hold of the TextBox (it's actually a TextBox Control) used for editing, so you have all the tool needed to get or set the Caret position, usually setting / getting the `SelectionStart` property. As mentioned, you can also replace or Editing Control of a Column with your own TextBox Control (a Custom Control, a class derived from TextBox) and have full control on what a User enters. This include full control of the Caret position. – Jimi Jan 23 '21 at 06:53
  • @Jimi Oh yeah, not as same as the link but if I count that cell as textbox then textbox has a function to get cursor position which I can apply here. Well in that case wait I just give it a try. – bonny Jan 23 '21 at 06:58
  • @Jimi Yea did it. Its kinda seems a long way but it worked for me. Will post an answer in short time... – bonny Jan 23 '21 at 08:32

0 Answers0