Vaishnavi K V
Vaishnavi K V

Reputation: 23

Place cursor in edit control box in visual studio

I am trying to make a password dialog box in a visual studio software. As soon as my password dialog box opens, I want the cursor to be placed in the edit control box so I can type password on it.

How can I put or place cursor in a text edit box in visual studio MFC without a mouse click?

Please suggest me how to do this.

static CEdit *ptrCurrentEditWindow;

BOOL GET_PASSWORD::OnInitDialog()
{
    CDialog::OnInitDialog();

    SetDlgItemText(IDC_PASSWORD, L"");
    ptrCurrentEditWindow = &m_wnd_password;

    return TRUE;  
}

Upvotes: 1

Views: 955

Answers (1)

IInspectable
IInspectable

Reputation: 51489

The documentation for CDialog::OnInitDialog explains, how to do this. The return value section has this information:

Specifies whether the application has set the input focus to one of the controls in the dialog box. If OnInitDialog returns nonzero, Windows sets the input focus to the default location, the first control in the dialog box. The application can return 0 only if it has explicitly set the input focus to one of the controls in the dialog box.

That leaves you with 2 options:

  • Make your edit control the first control, and have Windows handle everything for you by returning TRUE.
  • Move input to your edit control manually (CDialog::GotoDlgCtrl), and return FALSE.

Addressing the updated question: The implementation already does, what you need, as long as you make IDC_PASSWORD the first control in your dialog template. If you do not want to or cannot arrange for this, you'll have to manually move input focus, like this:

BOOL GET_PASSWORD::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Not needed; an edit control is initially empty
    SetDlgItemText(IDC_PASSWORD, L"");

    // Set input focus to the desired control
    GotoDlgCtrl(GetDlgItem(IDC_PASSWORD));
    // Let the framework know, that you already set input focus
    return FALSE;
}

Upvotes: 4

Related Questions