为什么Combo Box动态绑定数据没有反应?
基本思路是打开某个路径并取一个名字
然后动态保存于ComboBox中。。。
似乎AddComboItem根本就没有把路径
加入到ComboBox中 但是路径却是得到了!
困扰很久。。。望达人指点。。。
PS 小弟才刚学习MFC 谢谢
具体实现如下:
void CVolumeLocation::OnBnClickedBtnFile()
{
HWND hComboBox = (HWND)GetDlgItem(IDC_COMBO_FILE);
AddComboItem(hComboBox, szFileName);
}
头文件:
void AddComboItem(HWND hComboBox, char *lpszFileName);
CPP文件:
void AddComboItem(HWND hComboBox, char *lpszFileName)
{
LPARAM nIndex;
/*Reset ComboBox Contents*/
SendMessage(hComboBox, CB_RESETCONTENT, 0, 0);
SetWindowText(hComboBox, lpszFileName);
/*Get nIndex*/
nIndex = SendMessage(hComboBox, CB_FINDSTRINGEXACT, (WPARAM) - 1, (LPARAM) & lpszFileName[0]);
/**
if(SetWindowText(hComboBox, lpszFileName))
{
MessageBox(GetParent(hComboBox),(LPCSTR)GetWindowText(hComboBox,lpszFileName,5),"Hello",MB_OK);
return;
}
else
{
AfxMessageBox("Error");
}
**/
/*The return value is the zero-based index of the matching item.
If the search is unsuccessful, it is CB_ERR. */
if(nIndex == CB_ERR && *lpszFileName)
{
time_t lTime = time(NULL);
nIndex = SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM) & lpszFileName[0]);
if(nIndex != CB_ERR)
SendMessage(hComboBox, CB_SETITEMDATA, nIndex, (LPARAM) lTime);
}
if(nIndex != CB_ERR && *lpszFileName)
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM) & lpszFileName[0]);
nIndex = SendMessage(hComboBox, CB_SETCURSEL, nIndex, 0);
if(*lpszFileName == 0)
{
SendMessage(hComboBox, CB_SETCURSEL, (WPARAM) - 1, 0);
}
//AfxMessageBox("AddComboItem Invoked");
}