Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

change edit box background color on condition

There are many examples on the board regarding how to change the background color of an edit control when an application starts up, mainly by implementing the WM_CTLCOLOR and putting in the OnCtlColor() method. That part I understand...

What I'm looking for is, not for the edit box color to change on startup, but ONLY when it's empty. So I have a check in the code, if the string in the edit box is empty, I want to call a function to change the color of the edit box so it can bring the user's attn to it.

any ideas? I can create a func to do it, but it seems like I would need to pass it the CDC param? But where do I get this param?
0
jade03
Asked:
jade03
  • 3
2 Solutions
 
jade03Author Commented:
actually, I have half of it working so far...instead of just changing the color of the edit box, it changes the color of the entire dialog bg plus the edit box.

Another little thing: IDC_TABONE_HOST is a CIPAddressCtrl while IDC_TABONE_PRT_NUM is a CEdit, and the edit box one changes color, but the CIPAddressCtrl doesn't....any ideas?


This this what I have in my code:

.h
      CBrush m_brush;
      COLORREF m_bgColor, m_txtColor;
      BOOL m_changeColor;


.cpp

constructor:
                 m_bgColor = RGB(255,255,0); // yellow background
       m_txtColor = RGB(0,0,0); // black text
       m_brush.CreateSolidBrush(m_bgColor);

       m_changeColor = FALSE;


// do my checking condition, if true:
      m_changeColor = TRUE;
      GetDlgItem(IDC_TABONE_HOST)->RedrawWindow();
      GetDlgItem(IDC_TABONE_PRT_NUM)->RedrawWindow();

HBRUSH CTabOne::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
            // Call the base class implementation first! Otherwise, it may
            // undo what we're trying to accomplish here.
      HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

            // change background color of
            // host and port field in tab one
            // ONLY if no config file is present
      if(m_changeColor)
      {
            if (pWnd->GetDlgCtrlID() == IDC_TABONE_PRT_NUM ||
                  pWnd->GetDlgCtrlID() == IDC_TABONE_HOST )
            {
                  pDC->SetBkColor(m_bgColor);
                  pDC->SetTextColor(m_txtColor);
            }

            hbr = m_brush;
      }
   
      return hbr;
0
 
Jaime OlivaresSoftware ArchitectCommented:
I think you just need something like this:

if (pWnd->GetDlgCtrlID() == IDC_TABONE_PRT_NUM ||  pWnd->GetDlgCtrlID() == IDC_TABONE_HOST )
{
       if (pWnd->GetWindowTextLength()==0) { // or inverse logic
               pDC->SetBkColor(m_bgColor);
               pDC->SetTextColor(m_txtColor);
        }
}

0
 
jade03Author Commented:
I got it to work now...checking on the nCtlColor, and only setting the bk and txt color if it's the correct control.

However, I'm still not sure if the IP address control is part of the nCtlColor list...I didn't see it listed in msdn's doc....so the part on coloring the IP address control is still not being done...

any ideas on that?
0
 
AndyAinscowCommented:
A minor modification to Jaimes code - use the IsBlank member function of the IP adress control to check if nothing entered
if ( pWnd->GetDlgCtrlID() == IDC_TABONE_PRT_NUM )
{
       if (pWnd->GetWindowTextLength()==0) { // or inverse logic
               pDC->SetBkColor(m_bgColor);
               pDC->SetTextColor(m_txtColor);
        }
}
else if ( pWnd->GetDlgCtrlID() == IDC_TABONE_HOST )
{
       if (((CIPAddressCtrl*)pWnd)->IsBlank()) {
               pDC->SetBkColor(m_bgColor);
               pDC->SetTextColor(m_txtColor);
        }
}
0
 
jade03Author Commented:
thanx, guys! sorry for the late acceptance of your answers.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now