0

I have piece of code that erases the last character of a string and then set the text in the Edit Control to that new string. The problem is that, afterwards, the position of the character that will be typed next changes. Example:

Edit control box: [ 12345| ] (The slash is where the next character typed will be placed)

After doing the code mentioned

Edit control box: [ |12345 ] (The position now moved to the front, before 1)

How would I move the position to the end of the string again? My code:

    CString str1 = ""; //Temporary CString
    eb1->GetWindowText(str1); //Store text from Edit Control to the CString
    string strCheck1 =  str1.GetString(); //Place the CString into a regular string
    int s1 = strCheck1.length() -1; //Get index of last character and new size
    bool check1 = true; //Boolean variable for the checking
    //Get if character is valid
    if((strCheck1[s1] <= '0' || strCheck1[s1] >='9') && strCheck1[s1] != '.') {check1 =       false;}
    //If is invalid I erase last character and put it back intact into the Edit Control
    if(check1 == false) {strCheck1.erase(s1,1); eb1->SetWindowTextA(strCheck1.c_str());}
Artie
  • 493
  • 1
  • 7
  • 18

2 Answers2

1

Have you tried SetSel() operation of edit control?

// get the initial text length
int nLength = edit.GetWindowTextLength();
// put the selection at the end of text
edit.SetSel(nLength, nLength);
Niklas Hansson
  • 503
  • 2
  • 16
  • For some reason while browsing the functions I thought CEdit::SetSel was to select the character like highlighting it, not this; I guess I should have tried. Thanks. – Artie Dec 04 '11 at 18:53
0

You can use CEdit::SetSel() (I'm assuming that you are using CEdit). Just let the start and end of selection both be the end of string, you should be able to move the cursor there. Details may be found at http://msdn.microsoft.com/en-us/library/w9kftda4(v=vs.80).aspx

fefe
  • 3,342
  • 2
  • 23
  • 45