Dialog boxes-change the background an edit box

Hi,I create a dialog based project and  I want to change the background of an edit box when it takes a value.please, can you tell me how can I do it?If you can, send me the code of doing this.THANK you
geomviglaAsked:
Who is Participating?
 
DanRollinsCommented:
If the user tabs around and comes back to that edit box, you might want it to go back to white.  If so, add a handler for EN_SETFOCUS and do the reverse-- turn off m_fEditIsRed, and set the brush to white.  You will need to do corresponding checking of m_fIsEditRed (and color reversal) in your OnCtlColor() fn.
0
 
DanRollinsCommented:
Open the ClassWizard (ctrl+W)  
 Under the Message Maps tab, Select the main dialog class object
 In Messages, select  WM_CTLCOLOR.
 Click [Add Function]
 Click [Edit Code]
 Make it so:

HBRUSH CD31Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
      HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
      
      if ( pWnd->m_hWnd == GetDlgItem( IDC_EDIT1 )->m_hWnd ) {
            if ( m_fEditIsRed ) {
                  pDC->SetTextColor( RGB(255,255,255) );  // white
                  pDC->SetBkColor( RGB(255,0,0) ); // red
                  return( m_hbrEditBkgd );
            )
      }
      return hbr;
}

Back in the ClassWizard (Ctrl+W)
Under the Message Maps tab... Select the main dialog class object
In the Object IDs, select IDC_EDIT1 (or whatever is the ID of the edit box)
 In Messages, select  EN_CHANGE
 Click [Add Function]
 Click [Edit Code]
 Make it so:

void CD31Dlg::OnChangeEdit1()
{
      m_fEditIsRed= TRUE;
      ::DeleteObject(m_hbrEditBkgd );
      m_hbrEditBkgd= ::CreateSolidBrush( RGB(255,0,0) );  // Red (changed)
      GetDlgItem( IDC_EDIT1 )->Invalidate();
}


Scroll up to the OnInitDialogBox fn in theis main dlg cpp file.  Add these lines:

      m_hbrEditBkgd=     ::CreateSolidBrush( RGB(255,255,255) );  // white (unchanged)
      h_fEditIsRed= FALSE;

Open the .H file for the main dialog.  Add this into the class def:
      HBRUSH m_hbrEditBkgd;
      BOOL     m_fEditIsRed;

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Now, as soon as the user begins typing, the background of that edit control will change to RED.

If you want to wait until the user finishes typing, comment out the stuff in OnChangeEdit1 and use the ClassWizard to create a handler for EN_KILLFOCUS for IDC_EDIT1.  Make the code of you OnKillFocusEdit1() fn do the same as described above for OnCHangeEdit1.

-- Dan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.