본문 바로가기
개발언어/C++

ComboBox 의 Item과 List 구성하기

by 엔돌슨 2010. 5. 14.
반응형

ComboBox 의 Item과 List 구성하기

연관글 보기
 2010/05/13 - [개발언어/C++] - ComboBox의 GetCurSel의 ItemDate를 가져와서 SQL Querey 하기

ComboBox의 Item과 List를 DB를 통해서 구성합니다.


1 void CSearchModule::LoadVIPInfo()
2 {
3     // VIP ComboBox 초기화
4 m_comboVIP.ResetContent(); 5     m_comboVIP.AddString(_T("전체"));
6     m_comboVIP.SetItemDataPtr(0, new CString(_T("전체")));
7
8     // SQL Query 작성
9     CString strQuery = _T("");
10     strQuery.Format(_T(" SELECT * FROM TB_CRM_VIP_KIND ORDER BY CODE "));
11
12    
13     CADORecordset RecordSet;        // RecordSet 객체선언
14     BOOL bExecuteResult, bAdoOpen;    // SQL Query 결과반환값 변수, AdoOpen 결과값 변수
15
16     // DataBase가 Open이 아니면 return;
17     if(!m_obADOConnection.IsOpen(&bAdoOpen))       
18         return;
19     
20     // SQL Query 를 질의 합니다.
21     bExecuteResult = m_obADOConnection.Execute(strQuery, &RecordSet);
22
23     // SQL Query의 질의가 성공적일 때
24     if(bExecuteResult)
25     {
26         // EOF 까지 읽어 들임.
27         while(!RecordSet.IsEOF())
28         {
29             CString strCode = _T("");
30             CString strVipKindName = _T("");
31             CString strRemark = _T("");
32
33             RecordSet.GetFieldValue(_T("CODE"), &strCode);                        // VIP CODE   
34             RecordSet.GetFieldValue(_T("VIP_KIND_NAME"), &strVipKindName);        // VIP 명칭
35             RecordSet.GetFieldValue(_T("REMARK"), &strRemark);                    // VIP Remark
36
37             // Combo의 List AddString과 Item을 세팅합니다.
38             int nItem = m_comboVIP.AddString(strVipKindName);
39             m_comboVIP.SetItemDataPtr(nItem, new CString(strVipKindName));
40
41             // 레코드셋 이동
42             RecordSet.MoveNext();
43         }
44     }
45     else
46     {
47         // Error Count 처리를 합니다.
48         long lCount;
49         if(m_obADOConnection.GetErrorCount(&lCount) )
50         {
51             for(long l = 0 ; l < lCount ; ++l)
52             {
53                 CADOError obError;
54                 if( m_obADOConnection.GetError(l, &obError) )
55                 {
56                     CString strDescription;
57                     if( obError.GetDescription(&strDescription) )
58                         ATLTRACE(strDescription);
59                 }
60             }
61         }
62         return;       
63     }
64    
65     // 만약 RecordSet이 Open이면 Close함.
66     if(RecordSet.IsOpen()) RecordSet.Close(); 67    
68     // 첫번째 Item을 선택하게 포커스 세팅.
69     m_comboVIP.SetCurSel(0);
70 }



 

CComboBox::SetItemDataPtr

int SetItemDataPtr( int nIndex, void* pData );

Return Value

CB_ERR if an error occurs.

Parameters

nIndex

Contains a zero-based index to the item.

pData

Contains the pointer to associate with the item.

Remarks

Sets the 32-bit value associated with the specified item in a combo box to be the specified pointer (void*). This pointer remains valid for the life of the combo box, even though the item’s relative position within the combo box might change as items are added or removed. Hence, the item’s index within the box can change, but the pointer remains reliable.

Example

// The pointer to my combo box.
extern CComboBox* pmyComboBox;

// Set the data pointer of each item to be NULL.
for (int i=0;i < pmyComboBox->GetCount();i++)
{
   pmyComboBox->SetItemDataPtr(i, NULL);
}

CComboBox OverviewClass MembersHierarchy Chart

See Also   CComboBox::GetItemData, CComboBox::GetItemDataPtr, CComboBox::SetItemData, CB_SETITEMDATA, CComboBox::AddString, CComboBox::InsertString