caret repositions itself
I am trying to manipulate caret position in an edit control using Winapi SetCaretPos() function. When the user clicks on a button, the caret in the edit box advances a certain number of pixels forward(its like inserting a blank automatically). Till here everything goes fine, and the caret blinks at its new position, but when I try to write anything in the edit box now, it goes back to the previous place before the automatic space. Any suggestions on how can I skip certain space in edit box?
A few lines of code:
The caret flashes at the right position, but I am not able to write at that position.
GetCaretPos(&Cursor_Pos); //Cursor_Pos is of type LPPOINT
SetCaretPos(Cursor_Pos.x + 50,Cursor_Pos.y);
Actually it's returning to the end of the text buffer and adding text there.
Moving the typing caret beyond the end of the text does NOT add spaces...
If you want to add spaces, you have to add spaces to the end of the text buffer.
I got your point, but can't I insert spaces using code?, like on press of a button, the caret should move ahead as if the user manually entered the space. Is it difficult to implement or not possible at all?
That's what Tater is suggesting you do, add spaces using code.
I suppose you might do something like SendInput with a space, or if you can access the text field of your edit box, then just add a space to the end of it.
It's easy enough... Look up the various messages you can send to an edit control... everything you need is there.
Originally Posted by gaurav_13191
SendInput is dangerous in this situation since it only works with the foreground window... i.e. the button his user just clicked.
Originally Posted by tabstop
Ok I solved the above problem of inserting text.. but I have got another issue that I am unable to position the caret at the end position after updating my edit control text. It positions itself at the beginning, I searched for this on the internet and found EM_SETSEL message to place it at desired position. But here the problem arises is that the text behind the current caret position is highlighted which I don't need. I just want that the caret places itself at the correct position after I insert my blank/space. Any suggestions?
I resolved the issue.. no need to answer.. thanks for all the answers.. :D :)