Visual Studio MFC C++ Move cursor after SetDlgItemText

daktaklakpak 41 Reputation points
2020-12-04T09:22:35.063+00:00

I am making a Visual Studio MFC C++ App, and I am having a (hopefully) small problem.

I have an Edit Control text box with ID txtInputBox, and it has a value C-String variable m_inputText assigned to it. After I do a SetDlgItemText(txtInputBox, L"Hello World"); the cursor moves to the front of the text, before the 'H'. How would I get the cursor to be at the end (after the 'd') right after doing a SetDlgItemText?

I read of a SetSel method, but don't know how to implement it if that's the right one.

What I have is:

someDlg::someFunction()  //this is used in an EN_CHANGE event
{
    //some logic stuff to get a result string
    SetDlgItemText(txtInputBox, L"Hello World");
    //need it to set the cursor to the end
    //I tried these, but it didn't recognize (expression must have class type?).  doesn't work
    //txtInputBox.SetSel(0, -1);
    //txtInputBox.SetSel(-1);
}

void someDlg::EnChangetxtinputbox()
{
    someFunction();
}
Developer technologies | Visual Studio | Debugging
Developer technologies | Visual Studio | Testing
Developer technologies | Visual Studio | Other
0 comments No comments
{count} votes

Accepted answer
  1. David Lowndes 4,726 Reputation points
    2020-12-04T09:50:14.31+00:00

    You could assign a control variable (not a string) for your edit controls and then use SetSel as you tried, or you can do it like this:

    ((CEdit*)GetDlgItem(ID_whatever_it_is))->SetSel( start. end );
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.