3
votes

in my dialog, there is a CEdit box, which set maximum character number. below in DoDataExchange function:

void CDlgSurvey::DoDataExchange(CDataExchange* pDX)
{
    DDX_Control(pDX, IDC_EDIT_SURVEY_ID, m_SurveyIDEdit);//ACUTALLY THE ISSUE IS HERE,SO LATER WE MODIFIED THE CLASS FUNCTION Onchar in m_SurveyIDEdit RELATED
    DDX_Text(pDX, IDC_EDIT_SURVEY_ID, m_SurveyID);
    DDV_MaxChars(pDX, m_SurveyID, SURVEY_ID_FIELD_LENGTH);
}

I found it works. it means I cannot key in characters more than SURVEY_ID_FIELD_LENGTH. But the problem is when I already key in the SURVEY_ID_FIELD_LENGTH length of characters, and I tried to delete some character by using backspace at the end of text. it doesn't work. Did somebody met such problem? and I also try to using another way to set max text in OnInitDialog,

BOOL CDlgSurvey::OnInitDialog()
{
    //set Max Text in Edit Box
    CEdit* pEditControl = (CEdit*)GetDlgItem(IDC_EDIT_SURVEY_ID);
    if (pEditControl)
    {
        pEditControl->SetLimitText(SURVEY_ID_FIELD_LENGTH);
    }

    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

the problem is the same, again, I cannot use backspace after it reached the maximum character. Does someone have any idea on how to fix it? thanks,

1
I've never come across this. Do you get the problem also with a freshly created application? What is your Platform (OS, Visual Studio version, etc)? Post also the dialog resource from the .rc fileJabberwocky
Never heard of such a thing. Validation does not occur until the OK button is pressed. So, while you are editing fields, the validators are inactive and would not cause any issues with editing the field.Joseph Willcoxson

1 Answers

2
votes

After checking the code, it is not related to SetLimitText or DDV_MaxChars. The actual issue relates to DDX_Control.

With variable m_SurveyIDEdit we check the character restriction. Once we find the text length has already been reached (MaxLength), it just simply returns. That's the problem.

So we have modified the code. We still handle the CEdit::OnChar method. So the key point to handle the issue is: Every time you should examine all unrelated code and see what happens.

My edit control was actually derived from CRestrictedEdit. My solution was to adjust the OnChar handler.

void CRestrictedEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // Get the text of the edit control
    CString sText;
    GetWindowText(sText);

    // if the control limit is already reached, no need to validate the character.
    if ((static_cast<UINT>(sText.GetLength())) == this->GetLimitText())
    {
        CEdit::OnChar(nChar, nRepCnt, nFlags); //THIS IS NEW LINE ADDED
        return;
    }
}