{"id":549,"date":"2010-03-15T17:23:11","date_gmt":"2010-03-15T09:23:11","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=549"},"modified":"2011-03-16T19:27:24","modified_gmt":"2011-03-16T11:27:24","slug":"mfc-combobox-%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=549","title":{"rendered":"[C\/C++]MFC ComboBox \u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p><strong>Step1 \u9996\u5148\u7576\u7136\u662f\u8981\u5728\u5de5\u5177\u7bb1\u62c9Combo Box\u51fa\u4f86<\/strong><br \/>\n\u5728Data\u5c6c\u6027\u4e2d\u53ef\u4ee5\u653e\u5165\u4f60\u60f3\u8981\u653e\u7684\u503c\u6216\u8cc7\u6599\uff0c\u53ea\u8981\u7528\u5206\u865f\u300e;\u300f\u9694\u958b\uff0c\u4f8b\u5982\uff1a10;20;30;40<\/p>\n<p><strong>Step2 \u518d\u4f86\u8a2d\u5b9a\u8b8a\u6578<\/strong><br \/>\nCComboBox *m_sTimeout;\u00a0\u00a0<span style=\"color: #339966;\">\/\/\u5ba3\u544aCombo Box\u7269\u4ef6\u4f4d\u5740\u8b8a\u6578<\/span><br \/>\nCString strCBTimeout;\u00a0<span style=\"color: #339966;\">\/\/\u5ba3\u544a\u6240\u9078\u7684\u503c\u6216\u8cc7\u6599\u8b8a\u6578<\/span><br \/>\nint SetTimeout  = 0;\u00a0\u00a0\u00a0<span style=\"color: #339966;\"> \/\/\u5ba3\u544a\u8f49\u63db\u5b57\u4e32\u6210\u6574\u6578\u503c\u8b8a\u6578<!--more--><\/span><\/p>\n<p><strong>Step3\u53d6\u5f97Combo Box\u7269\u4ef6\u8cc7\u6599<\/strong><br \/>\nm_sTimeout = (CComboBox *)GetDlgItem(IDC_COMBOTIMEOUT);<\/p>\n<p>if(m_sTimeout-&gt;GetCurSel() &gt;=0)\u00a0 <span style=\"color: #339966;\">\/\/\u7576\u6709\u53d6\u5230\u503c\u7684\u6642\u5019\u57f7\u884c\u6703\u56de\u50b3\u9078\u53d6\u7684\u7de8\u865f<\/span><br \/>\n{<br \/>\nm_sTimeout-&gt;GetLBText(m_sTimeout-&gt;GetCurSel(), strCBTimeout); <span style=\"color: #339966;\">\/\/\u525b\u9078\u53d6\u7684\u7de8\u865f\u503c\u653e\u5165\u5b57\u4e32<\/span><br \/>\nSetTimeout = atoi(strCBTimeout);  <span style=\"color: #339966;\">\/\/\u5c07\u9078\u53d6\u7684\u5b57\u4e32\u503c\u8f49\u6210\u6574\u6578\u503c<\/span><br \/>\n}<br \/>\nelse<br \/>\nSetTimeout = 8000;<\/p>\n<p>\u53ef\u53c3\u8003\u4e0b\u9762\u89e3\u8aaahttp:\/\/www.codersource.net\/mfc\/mfc-tutorials\/ccombobox-example.aspx<\/p>\n<div>\n<h2>CComboBox  Example<\/h2>\n<\/div>\n<div><input title=\"Print\" alt=\"Print\" name=\"dnn$ctr414$dnnACTIONBUTTON3$ctl00$ctl01\" src=\"http:\/\/www.codersource.net\/images\/action_print.gif\" type=\"image\" \/> <a id=\"dnn_ctr414_dnnVISIBILITY_cmdVisibility\" title=\"Minimize\" onclick=\"if (__dnn_ContainerMaxMin_OnClick(this,  'dnn_ctr414_ModuleContent')) return false;\" href=\"javascript:__doPostBack('dnn$ctr414$dnnVISIBILITY$cmdVisibility','')\"><img decoding=\"async\" id=\"dnn_ctr414_dnnVISIBILITY_imgVisibility\" title=\"Minimize\" src=\"http:\/\/www.codersource.net\/Portals\/_default\/Containers\/MinimalExtropy\/images\/DNN-minus.gif\" alt=\"Minimize\" \/><\/a><\/div>\n<p><!-- Start_Module_414 --><span style=\"font-family: Arial,Helvetica,sans-serif; font-size: x-small;\">Combo box controls are space savers. Wherever there is no need for  a multi-select from a list of items, combo box is a good choice in such  places. This article &#8221; CComboBox Example&#8221; explains how to use the MFC  CComboBox class for manipulation of a list of strings.<\/span><\/p>\n<h2>CComboBox Example &#8211; Initializing a Combo Box:<\/h2>\n<p>It is assumed that the readers of the sample have already created a  dialog box (either in a dialog based application or SDI\/MDI  application) and placed a combo box control from the controls toolbox on  the Resource Editor.<\/p>\n<p>After placing the combo box control on the dialog box, open the  class wizard by pressing <em>Ctrl + W <\/em>keys or <em>Menu &#8211;&gt; View  &#8211;&gt; ClassWizard<\/em>. In the <em>Member Variables<\/em> tab, Add a  Variable for the CComboBox class. This CComboBox example assumes that  the variable name is,<\/p>\n<p>CComboBox\u00a0 m_cbExample;<\/p>\n<p>This m_cbExample will be used further in our CComboBox example MFC  code.<\/p>\n<p><!-- Ad Link Place Holder here --><\/p>\n<h2>CComboBox Example &#8211; Adding Items to a Combo Box:<\/h2>\n<p>The function AddString is used for adding items to a combo box. If  there is a constant set of data, these values can also be added in the  Resource Editor itself. The Combo Box control properties dialog has a  tab for adding <em>data<\/em>. Otherwise the data can be added as follows.<\/p>\n<p>m_cbExample.AddString(&#8220;StringData1&#8221;);<br \/>\nm_cbExample.AddString(&#8220;StringData2&#8221;);<br \/>\nm_cbExample.AddString(&#8220;StringData3&#8221;);<\/p>\n<h2>CComboBox Example &#8211; Retrieving Items from a Combo Box:<\/h2>\n<p>Usually a requirement for retrieving items from the combo box will  arise from selecting the data. This article also assumes the same. Now  the data selected in a combo box needs to be retrieved.<\/p>\n<p>To do this, the first step is to find out the index of the  selected item inside the combo box control. Then the item at the  corresponding position needs to be pulled out as follows.<\/p>\n<p>int nIndex = m_cbExample.GetCurSel();<br \/>\nCString strCBText;<br \/>\nm_cbExample.GetLBText( nIndex, strCBText);<\/p>\n<p>In the above CComboBox example code, the value will be retrieved  and stored in strCBText variable. There is another overloaded version  for GetLBText. But the version which uses CString is the easiest one.<\/p>\n<h3>CComboBox Example &#8211; Finding Items inside a Combo Box:<\/h3>\n<p>This kind of Find operations on a Combo box will most probably be  useful in programs that dynamically modify the values in a combo box.  The function FindStringExact is used to find the exact string match  inside a combo box.<\/p>\n<p>int nIndex = m_cbExample.FindStringExact(0, &#8220;Value to be found&#8221;);<\/p>\n<p>The string position inside the combo box control is the return  value. It returns CB_ERR if it was unsuccessful in finding the string.<\/p>\n<h3>CComboBox Example &#8211; Deleting Items from a Combo Box:<\/h3>\n<p>This operation can be done by using the CCombobox member function  DeleteString. This function needs the index of the item inside the  combo box.<\/p>\n<p>m_cbExample.DeleteString(nIndex);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Step1 \u9996\u5148\u7576\u7136\u662f\u8981\u5728\u5de5\u5177\u7bb1\u62c9Combo Box\u51fa\u4f86 \u5728Data\u5c6c\u6027\u4e2d\u53ef\u4ee5\u653e\u5165\u4f60\u60f3\u8981\u653e\u7684\u503c\u6216\u8cc7\u6599\uff0c\u53ea\u8981\u7528\u5206<span class=\"post-excerpt-end\">&hellip;<\/span><\/p>\n<p class=\"more-link\"><a href=\"https:\/\/learn-house.idv.tw\/?p=549\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-549","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=549"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}