Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

Tedit caret and char position

Hi there

I am making my own editor as an array of single line TEdit boxes.
I need to be able to move the caret (cursor position) from one line to
another when the user presses the down arrow key.
The functions GetCaretPos and SetCaretPos work correctly as expected
however when the user presses a key for new input it appears in the Edit
box
at the last position that the caret was at. Not where I have the caret
positioned!!
I am using a system font (all characters the same size).

e.g. Say I have the caret between the Y and X on line 2 below. Then I use
the
mouse and go to a position between  D and E on line 1. If I press down
arrow
I can position the caret between the V and U as required. If I now enter a
character
Say Q it will appear between the Y and X  NOT between the V and U that I
want !!

Line 1       ABCDEF
Line 2       ZYXVUW

How do I get around this problem?

Below is my MoveCursorDown function

void TMyView::MoveCursorDown(int iCurrentEditCntIdx)
{
  TPoint  point;
  int iTemp;

  if(iCurrentEditCntIdx < iNbrEditControls - 1)
  {
    EditCnt[iCurrentEditCntIdx]->GetCaretPos(point);
    EditCnt[iCurrentEditCntIdx + 1]->SetFocus();
    EditCnt[iCurrentEditCntIdx + 1]->SetCaretPos(point);
  }
}


Thanks in anticipation of a reply

David Hicks.
0
DHICKS
Asked:
DHICKS
  • 2
  • 2
1 Solution
 
NickRepinCommented:
You should use Get/SetSelection, not Get/SetCaretPos:

uint start,end;
EditCnt[iCurrentEditCntIdx]->GetSelection(start,end);
EditCnt[iCurrentEditCntIdx + 1]->SetFocus();
EditCnt[iCurrentEditCntIdx + 1]->SetSelection(end,end);
 
0
 
DHICKSAuthor Commented:
GetSelection(start,end);
Returns the starting (startPos) and ending (endPos) positions of the currently selected text. By using GetSelection in conjunction with GetSubText, you can get the currently selected text.

I have no selected text !!! Therefore I have no startPos or EndPos.

bool SetSelection(uint startPos, uint endPos);

Forces the selection of the text between the positions specified by startPos and endPos, but not including the character at endPos.  I don't want to select text !!!  I just want to move the caret (cursor) and entry position for new text !!
0
 
NickRepinCommented:
Did you try it?
If there is no selection, GetSelection returns startPos==endPos==current caret position.
SetSelection(pos,pos) sets caret at pos position.
Please, refer to Win32 API.
0
 
DHICKSAuthor Commented:
Microsofts Get/SetSelection is poorly documented.
It works as you say even if you have no text selected.
Thanks
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now