[VC ++] Why does the ComboBox getcursel () variable change the cstring variable associated with it not get-1?

Source: Internet
Author: User
When ComboBox is input directly, the matched list items are automatically selected, and setwindowtext does not
---------------------------------------------------------------
 
// Get the ComboBox text
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 ();
}
 
// Set ComboBox text
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 );
}
}
 
---------------------------------------------------------------
 
Your combox can be edited. The default value is-1. Enter a D. When getcursel is used, it is also-1, because you have not selected any.
 
Cstring strinput;
Int NSEL = m_combobox.getcursel ();
M_combox.getlbtext (NSEL, strinput );
In this way, you can get the selected one. In strinput
---------------------------------------------------------------
 
If (m_combox.selectstring (m_combo) = cb_err)
{
This string m_combo does not exist in the original list.
}
---------------------------------------------------------------
 
ComboBox does not provide the "Enter content in edit" function. After you press enter, the content will be automatically added to the ComboBox drop-down list.
 
This function must be implemented by yourself...
 
In pretranslatemessage, the carriage return is detected, that is, the content in ComboBox is added to the list.
 
Bool ctest6dlg: pretranslatemessage (MSG * PMSG)
{
Cstring STR;
If (PMSG-> message = wm_keydown)
{
Switch (PMSG-> wparam)
{
Case vk_return:
Cedit * pedit = (cedit *) m_combo1.getwindow (gw_child );
If (PMSG-> hwnd = pedit-> m_hwnd)
{
M_combo1.getwindowtext (STR );
M_combo1.addstring (STR );
}
Return true;
}
}
Return cdialog: pretranslatemessage (PMSG );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.