Search code examples
mfceditbox

MFC editbox get cursor, In OnInitDialog function


How do I get cursor when dialog is start at OnInitDialog function,

without user to move click it, like as follow pic

enter link description here


Other problem is, when the editbox already fill with text, how do I select the text ?


Solution

  • On OnInitDialog function, I see as follow description,

    // return TRUE unless you set the focus to a control
    

    so I define a variable to the editbox, the set focus on it,

    then return false in OnInitDialog function.

    BOOL CInputTestPoint::OnInitDialog()
    {
    CDialog::OnInitDialog();
    
    // TODO:  在此加入額外的初始化
    m_editTestPointName.SetFocus();
    
    //return TRUE;  // return TRUE unless you set the focus to a control
    return false;
    // EXCEPTION: OCX 屬性頁應傳回 FALSE
    }
    

    For select whole text method is as follow

    define CEdit to the edit box and m_editTestPointName.SetSel(0, strTestPointName.GetLength(), true);
    
    ((CEdit *)GetDlgItem(IDC_EDIT_INPUT_TP))->SetSel(0, strTestPointName.GetLength(), true);