0

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. CEdit control MFC, placing cursor to end of string after SetWindowText

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?)
        //txtInputBox.SetSel(0, -1);
        //txtInputBox.SetSel(-1);
    }
    
    void someDlg::EnChangetxtinputbox()
    {
        someFunction();
    }

  • 1
    The technique in the answer to the post you linked is pretty much what I would have otherwise suggested. – Adrian Mole Dec 04 '20 at 08:06
  • 2
    [Dialog Data Exchange](https://learn.microsoft.com/en-us/cpp/mfc/dialog-data-exchange). That's how the system is intended to be used. Associating a member variable with control contents and calling `SetDlgItemText` (and friends) are orthogonal. Regardless, that's not going to make your issue any easier to solve. If you implement the solution from the linke Q&A, you're going to wind up with a fairly user-hostile user experience. It won't work for cases where a user might be inserting characters into the middle of the content, for example. – IInspectable Dec 04 '20 at 08:17
  • 1
    @IInspectable But it's something I use for text edits that are for filenames/paths. It is much more likely that the user will want to change the name than the whole path shebang, so I scroll to the right (end) of the text on presentation. – Adrian Mole Dec 04 '20 at 08:55
  • 1
    @adr It's perfectly fine to move the input caret to the end of text on initial presentation of a dialog. When you run the same code under an `EN_CHANGE` notification handler the rules changed and you can no longer ignore user interaction. – IInspectable Dec 04 '20 at 09:01
  • @IInspectable Ah, OK - I see what you mean. And I agree. – Adrian Mole Dec 04 '20 at 09:03
  • @AdrianMole (and IInspectable, can't @ two ppl) Thank you for helpful comments on the problems that may arise from using it this way. I am trying to implement the solution just to try to see it in action, but I may have confused myself. Would I put the 'CEdit* e = (CEdit*)GetDlgItem(txtInputBox);' within the same Dlg.cpp file or would I create a new class somehow and call it from there? I perhaps don't understand utilize it with what I have at the moment, as the answer in that post utilizes pointers. – SeePlusPlus Dec 04 '20 at 09:11
  • 1
    The way you describe *looks* about right to get a pointer to the edit control. – Adrian Mole Dec 04 '20 at 09:13
  • 2
    This would be a lot easier if you took MFC out of the equation. Try to see if [Get Started with Win32 and C++](https://learn.microsoft.com/en-us/windows/win32/learnwin32/learn-to-program-for-windows) isn't easier to follow. MFC doesn't buy you anything, unless you understand the API it hides. – IInspectable Dec 04 '20 at 13:16
  • Where the variable `txtInputBox` does come from? If it is by DDX, you are missing an `UpdataData(FALSE);` call statement after your code. – sergiol Feb 03 '21 at 14:14
  • If I recall it correctly, to put the cursor at the end, you need to do `SetSel(-1, -1);` – sergiol Feb 03 '21 at 14:18

1 Answers1

0

you said txtInputBox is the ID to your edit control (which should be written like so: IDC_NAME), you can pass IDs to SetDlgItemText.

what they are refering to in the link you posted is a member variable, you can't pass a member variable to SetDlgItemText. you can add a variable to a control by right clicking it in editor and clicking "Add variable".