Turtle Lover
Turtle Lover

Reputation: 45

How to handle the 'Left mouse Clicked' Event of an Edit Box in MFC

I have a Dialog with 8 Dynamic Read-Only Edit Boxes, 7/8 of them will hold different text strings, and the last one is empty. What i'm trying to do is: when a user clicks at 1 of those Edit Boxes (which hold the text string), the text will be shown in the empty Edit Box. If you guys have any ideas on how this should be done, I would be grateful.

Here are some codes that i've tried:

void CTab1::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
        ...
    DDX_Text(pDX, IDC_TAB1CMTBOX, m_StrShow);
}

BEGIN_MESSAGE_MAP(CTab1, CDialog)
...
ON_CONTROL_RANGE(EN_SETFOCUS, 4000, 4100, &CTab1::OnEditBoxClicked)
END_MESSAGE_MAP()

void CTab1::OnEditBoxClicked(UINT nID)
{
    switch (nID)
    {
    case 4001:

        GetDlgItemText(4001, m_CmtText);
        m_CmtText = m_StrShow; 
        UpdateData(FALSE);
        break;

    case 4003:
        GetDlgItemText(4003, m_CmtText);
        m_CmtText = m_StrShow;
        SetDlgItemText(IDC_TAB1CMTBOX, m_StrShow);//This line doesn't work
        UpdateData(FALSE);
        break;
...
}

Upvotes: 0

Views: 379

Answers (1)

Tom Tom
Tom Tom

Reputation: 1209

What I see You obviously only have swapped the variables.

void CTab1::OnEditBoxClicked(UINT nID)
{
   switch (nID)
   {
    case 4003:
      GetDlgItemText(4003, m_CmtText);  // Read ctrl Text to m_CmtString 
  //  m_CmtText = m_StrShow;            // then Write immediately m_strShow to m_CmtText.  Which make no sense
      m_StrShow = m_CmtText;            // <-- swapped
  //  SetDlgItemText(IDC_TAB1CMTBOX, m_StrShow);  // sure? You want show the Text in IDC_TAB1CMTBOX ?
      SetDlgItemText(IDC_SHOWBOX, m_StrShow); // replace IDC
      UpdateData(FALSE);
      break;
  ..
}

This is what I would do, simplify the code.

void CTab1::OnEditBoxClicked(UINT nID)
{
  if (UpdateData(TRUE))
  {
    GetDlgItemText(nID, m_CmtText);         // Read ctrl Text nID 
    SetDlgItemText(IDC_SHOWBOX, m_CmtText); // Show the ctrl nID Text to ShowBox
    UpdateData(FALSE);
  }

}

Upvotes: 1

Related Questions