{"id":125,"date":"2016-02-17T17:06:45","date_gmt":"2016-02-17T17:06:45","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=125"},"modified":"2023-04-25T13:43:03","modified_gmt":"2023-04-25T12:43:03","slug":"using-combo-boxes","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/combo-box\/using-combo-boxes","title":{"rendered":".Using Combo Boxes"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Only permitting items in the list<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">(YOU HAVE TO REMEMBER TO DO THIS TO STOP USERS ENTERING THEIR OWN VALUES!!!)<\/mark><\/h5>\n\n\n\n<p>DropDownStyle = DropDownList (Only accepts strings that are part of the selection list)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Loading the contents of a comboBox at application startup<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tcmbMyComboBox.BeginUpdate();\n\tcmbMyComboBox.SelectedIndex = -1;\n\tcmbMyComboBox.Items.Clear();\t\t\/\/Remove Existing Items If Necessary\n\n\tfor (Count = 0; Count &lt;= 500; Count++)\t\/\/Add Items\n\t\tcmbMyComboBox.Items.Add((Count * 100) + \"mm\");\n\n\tcmbMyComboBox.SelectedIndex = 0;\t\/\/Select default item\n\tcmbMyComboBox.EndUpdate();\n\n\t\/\/while (cmbMyComboBox.Items.Count)\t\/\/Another method to remove items\n\t\/\/\tcmbMyComboBox.Items.RemoveAt(0);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Allowing User To Type Text To Find An Entry<\/h4>\n\n\n\n<p>DropDownStyle = DropDown<br>AutoCompleteMode = Append (or an alternative if preffered)<br>AutoCompleteSource = List Items<br>In this mode the user can enter text that doesn&#8217;t match an entry in the list. You need to detect this yourself. Unfortunately you can&#8217;t use the &#8216;Leave&#8217; event of the combobox as if a user types the start of a name then uses tab to select it the SelectedIndex value is still -1 on the Leave call and only changes afterwards. You need to do the check using some other means.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tif (cmbUsername.SelectedIndex &lt; 0)\n\t\tcmbUsername.Text = \"\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Select An Item In the List<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tcmbMyComboBox.SelectedIndex = 0;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Getting Currently Selected Item Text<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyString = cmbMyComboBox.SelectedItem.ToString()\n\/\/or\n\tMyString = cmbMyComboBox.Items&#91;cmbMyComboBox.SelectedIndex].ToString();<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Only permitting items in the list (YOU HAVE TO REMEMBER TO DO THIS TO STOP USERS ENTERING THEIR OWN VALUES!!!) DropDownStyle = DropDownList (Only accepts strings that are part of the selection list) Loading the contents of a comboBox at application startup Allowing User To Type Text To Find An Entry DropDownStyle = DropDownAutoCompleteMode = [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-combo-box"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":2,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":1307,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/125\/revisions\/1307"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}