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();
}
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,193 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
1,004 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
351 questions
0 comments No comments
{count} votes

Accepted answer
  1. David Lowndes 4,716 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.