[VC]得到/设置DropDown ComboBox的文字

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

在使用DropDown类型的ComboBox时,如果使用者输入非ComboBox中列出的值,那么ComboBox的GetCurSel()的值是CB_ERR,相关的GetLBText()和GetLBTextLen()因为没有index也就无法工作。

在DDX时,可以把ComboBox和CString对应起来,采用它的原理,就可以自己来得到或设置DropDown ComboBox的文字了。

//得到ComboBox的文字

void GetComboBoxString(HWND hWndCtrl, CString& value)

{

// just get current edit item text (or drop list static)

int nLen = ::GetWindowTextLength(hWndCtrl);

if (nLen > 0)

{

// get known length

::GetWindowText(hWndCtrl, value.GetBufferSetLength(nLen), nLen+1);

}

else

{

// for drop lists GetWindowTextLength does not work - assume

// max of 255 characters

::GetWindowText(hWndCtrl, value.GetBuffer(255), 255+1);

}

value.ReleaseBuffer();

}

//设置ComboBox的文字

void SetComboBoxString(HWND hWndCtrl, const CString& value)

{

// set current selection based on model string

OutputDebugString("SetComboBoxString:" + value);

if (::SendMessage(hWndCtrl, CB_SELECTSTRING, (WPARAM)-1,

(LPARAM)(LPCTSTR)value) == CB_ERR)

{

// just set the edit text (will be ignored if DROPDOWNLIST)

AfxSetWindowText(hWndCtrl, value);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航