• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

How to change colour or highlight in edit control based on ListCtrl

Hallo :)

I have 5 Edit controls and 1 ListCtrl.

For example,

Edit contrl ID
1. IDC_A
2. IDC_B
3. IDC_C
4. IDC_D
5. IDC_E

In ListCtrl, there are 10 items
1. a1
2. b1
3. c1
4. d1
5. e1
6. a2
7. b2
8. c2
9. d2
10. e2

When I click at item 1 or 6, IDC_A should change the colour or highlight.
When I click at item 2 or 7, IDC_B should change the colour or highlight.
When I click at item 3 or 8, IDC_C should change the colour or highlight.
and so on.

I have implement it according to this website.
http://www.codeguru.com/Cpp/controls/editctrl/backgroundcolor/article.php/c3929

But I can't match it with the item in ListCtrl. How should I do?

Thank you very much in advance
0
chang2008
Asked:
chang2008
  • 3
  • 2
1 Solution
 
alb66Commented:
0
 
chang2008Author Commented:
I don't understand exactly what you mean alb66. I already have LVN_ITEMCHANGED
Some of my code below :

P.S. Please don't laugh at my code. I'm newbie. lol

What should I do next?
void dlgExample:OnNMClickListExample(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: Add your control notification handler code here
	*pResult = 0;
 
	POSITION p = m_ctrlListExample.GetFirstSelectedItemPosition();
 
	while (p)
	{
		int nSelected = m_ctrlListExample.GetNextSelectedItem(p);
		if(nSelected%5==0)
		{
			UpdateData(TRUE);
			m_a = m_ctrlListExample.GetItemText(nSelected, 0);
			m_b = m_ctrlListExample.GetItemText(nSelected, 1);
			m_c = m_ctrlListExample.GetItemText(nSelected+1, 1);
			m_d = m_ctrlListExample.GetItemText(nSelected+2, 1);
			m_e = m_ctrlListExample.GetItemText(nSelected+3, 1);
			UpdateData(FALSE);
		}
		else if(nSelected%5==1)
		{
			UpdateData(TRUE);
			m_a = m_ctrlListExample.GetItemText(nSelected-1, 0);
			m_b = m_ctrlListExample.GetItemText(nSelected-1, 1);
			m_c = m_ctrlListExample.GetItemText(nSelected, 1);
			m_d = m_ctrlListExample.GetItemText(nSelected+1, 1);
			m_e = m_ctrlListExample.GetItemText(nSelected+2, 1);
			UpdateData(FALSE);		
		}
		else if(nSelected%5==2)
		{
			UpdateData(TRUE);
			m_a = m_ctrlListExample.GetItemText(nSelected-2, 0);
			m_b = m_ctrlListExample.GetItemText(nSelected-2, 1);
			m_c = m_ctrlListExample.GetItemText(nSelected-1, 1);
			m_d = m_ctrlListExample.GetItemText(nSelected, 1);
			m_e = m_ctrlListExample.GetItemText(nSelected+1, 1);
			UpdateData(FALSE);		
		}
		else if(nSelected%5==3)
		{
			UpdateData(TRUE);
			m_a = m_ctrlListExample.GetItemText(nSelected-3, 0);
			m_b = m_ctrlListExample.GetItemText(nSelected-3, 1);
			m_c = m_ctrlListExample.GetItemText(nSelected-2, 1);
			m_d = m_ctrlListExample.GetItemText(nSelected-1, 1);
			m_e = m_ctrlListExample.GetItemText(nSelected, 1);
			UpdateData(FALSE);		
		}
		else if(nSelected%5==4)
		{
			UpdateData(TRUE);
			m_a = m_ctrlListExample.GetItemText(nSelected-4, 0);
			m_b = m_ctrlListExample.GetItemText(nSelected-4, 1);
			m_c = m_ctrlListExample.GetItemText(nSelected-3, 1);
			m_d = m_ctrlListExample.GetItemText(nSelected-2, 1);
			m_e = m_ctrlListExample.GetItemText(nSelected-1, 1);
			UpdateData(FALSE);
		}
		else
		{
		}
	}
}
 
HBRUSH dlgExample::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr;
 
	switch (pWnd->GetDlgCtrlID())
	{
		case IDC_A:
 
				pDC->SetBkColor(m_bluecolor);  
				pDC->SetTextColor(m_textcolor); 
				hbr = (HBRUSH) m_bluebrush;   
		break;
 
		case IDC_B:
 
			pDC->SetBkColor(m_redcolor);  
			pDC->SetTextColor(m_textcolor);
			hbr = (HBRUSH) m_redbrush; 
		break;	
 
		case IDC_C:
 
			pDC->SetBkColor(m_bluecolor);   
			pDC->SetTextColor(m_textcolor);
			hbr = (HBRUSH) m_bluebrush;  
		break;
	
		case IDC_D:
 
			pDC->SetBkColor(m_redcolor);   
			pDC->SetTextColor(m_textcolor); 
			hbr = (HBRUSH) m_redbrush;   
		break;
 
		case IDC_E:
 
			pDC->SetBkColor(m_bluecolor);  
			pDC->SetTextColor(m_textcolor);
			hbr = (HBRUSH) m_bluebrush;  
		break;	
 
		default:
		hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
	}
 
	return hbr; 
}

Open in new window

0
 
alb66Commented:
You can use  5 boolean to store which edit must be highlighted ( say m_bA, m_bB... )

Set or reset them in OnNMClickListExample();



At the end of OnNMClickListExample() invalidate the edit control:

GetDlgItem( IDC_A )->Invalidate();
...
GetDlgItem( IDC_E )->Invalidate();



Then, in OnCtlColor(), you can do:
HBRUSH dlgExample::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
        HBRUSH hbr;
 
        switch (pWnd->GetDlgCtrlID())
        {
                case IDC_A:
                        if ( m_bA )
                        { 
                                pDC->SetBkColor(m_bluecolor);  
                                pDC->SetTextColor(m_textcolor); 
                                hbr = (HBRUSH) m_bluebrush;   
                        }
                        else
                        { 
                                pDC->SetBkColor(m_defaultcolor);  
                                pDC->SetTextColor(m_textcolor); 
                                hbr = (HBRUSH) m_defaultbrush;   
                        }
                break;

Open in new window

0
 
chang2008Author Commented:
Thank you very much :)
0
 
chang2008Author Commented:
Thank you very much. It woks. :)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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