{"id":3121,"date":"2011-06-21T10:06:00","date_gmt":"2011-06-21T18:06:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=3121"},"modified":"2013-02-25T14:21:24","modified_gmt":"2013-02-25T22:21:24","slug":"primalforms-2011-spotlight-on-the-combobox-control","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2011\/06\/21\/primalforms-2011-spotlight-on-the-combobox-control\/","title":{"rendered":"PrimalForms 2011: Spotlight on the ComboBox control"},"content":{"rendered":"<p>The \u201cSpotlight on Controls\u201d series focuses on a single WinForms control in PrimalForms 2011 , details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms.<\/p>\n<p>Last time we took a look at the <a href=\"http:\/\/www.sapien.com\/blog\/2011\/06\/13\/primalforms-2011-spotlight-on-the-textbox-control\/\">TextBox control<\/a>. This time we will look at the ComboBox control:<\/p>\n<p><font size=\"4\"><strong>ComboBox Control <\/strong>[System.Windows.Forms.ComboBox]<\/font><\/p>\n<p>Represents a Windows combo box control.  <\/p>\n<p>Default Event: SelectedIndexChanged<\/p>\n<p><em>Why use the ComboBox control?<\/em>  <\/p>\n<p>Use the ComboBox to create a pull down list of items.<\/p>\n<p><strong><font size=\"3\">Important Properties:<\/font><\/strong><\/p>\n<p><strong>DropDownStyle<\/strong><\/td>\n<p>Controls the appearance and functionality of the combo box  <\/p>\n<p><em>Why use the DropDownStyle property?<\/em>  <\/p>\n<p>Use the DropDownStyle when you want change the style of the ComboBox or limit the user so they are restricted to selecting a value from the list (DropDownList).  <\/p>\n<p>Values (Default: <em>DropDown<\/em>)  <\/p>\n<blockquote>\n<p><em>Simple<\/em><br \/>Specifies that the list is always visible and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.  <\/p>\n<p><em>DropDown<\/em><br \/>Specifies that the list is displayed by clicking the down arrow and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list. When using this setting, the Append value of AutoCompleteMode works the same as the SuggestAppend value. This is the default style.  <\/p>\n<p><em>DropDownList<\/em><br \/>Specifies that the list is displayed by clicking the down arrow and that the text portion is not editable. This means that the user cannot enter a new value. Only values already in the list can be selected. The list displays only if AutoCompleteMode is Suggest or SuggestAppend.<\/p>\n<\/blockquote>\n<p><strong>Items<\/strong>  <\/p>\n<p>The values in the combo box.  <\/p>\n<p><em>Why use the Items property?<\/em>  <\/p>\n<p>Use the Items property when you want to add values the combo box\u2019s list.  <\/p>\n<p>Set the ComboBox\u2019s values via the Designer:  <\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/06\/Add-Items-in-the-Designer.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Add Items in the Designer\" border=\"0\" alt=\"Add Items in the Designer\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/06\/Add-Items-in-the-Designer_thumb.png\" width=\"244\" height=\"160\"><\/a>  <\/p>\n<p>Set the ComboBox\u2019s values dynamically in the script editor, by using the Items property\u2019s Add method: <\/p>\n<pre><span style=\"color: #8b0000\">$comboBox<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #8b0000\">$Value<\/span><span style=\"color: #000000\">)<\/span><\/pre>\n<p>&nbsp; <\/p>\n<p>The following is a helper function that will allow you dynamically load the ComboBox:<\/p>\n<pre><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Load-ComboBox<\/span><span style=\"color: #000000\"> \n{\n<\/span><span style=\"color: #008000\">&lt;#\n    .SYNOPSIS\n        This functions helps you load items into a ComboBox.\n\n    .DESCRIPTION\n        Use this function to dynamically load items into the ComboBox control.\n\n    .PARAMETER  ComboBox\n        The ComboBox control you want to add items to.\n\n    .PARAMETER  Items\n        The object or objects you wish to load into the ComboBox's Items collection.\n\n    .PARAMETER  DisplayMember\n        Indicates the property to display for the items in this control.\n    \n    .PARAMETER  Append\n        Adds the item(s) to the ComboBox without clearing the Items collection.\n#&gt;<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff\">Param<\/span><span style=\"color: #000000\"> (\n        [Parameter(Mandatory<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">)]\n        [System.Windows.Forms.ComboBox]<\/span><span style=\"color: #8b0000\">$ComboBox<\/span><span style=\"color: #000000\">,\n        [Parameter(Mandatory<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">)]\n        <\/span><span style=\"color: #8b0000\">$Items<\/span><span style=\"color: #000000\">,\n        [Parameter(Mandatory<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\">)]\n        [string]<\/span><span style=\"color: #8b0000\">$DisplayMember<\/span><span style=\"color: #000000\">,\n        [<\/span><span style=\"color: #0000ff\">switch<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #8b0000\">$Append<\/span><span style=\"color: #000000\">\n    )\n    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$Append<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #8b0000\">$comboBox<\/span><span style=\"color: #000000\">.Items.Clear()    \n    }\n    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$Items<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-is<\/span><span style=\"color: #000000\"> [Array])\n    {\n        <\/span><span style=\"color: #8b0000\">$comboBox<\/span><span style=\"color: #000000\">.Items.AddRange(<\/span><span style=\"color: #8b0000\">$Items<\/span><span style=\"color: #000000\">)\n    }\n    <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\">\n    {\n        <\/span><span style=\"color: #8b0000\">$comboBox<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #8b0000\">$Items<\/span><span style=\"color: #000000\">)    \n    }\n\n    <\/span><span style=\"color: #8b0000\">$comboBox<\/span><span style=\"color: #000000\">.DisplayMember <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$DisplayMember<\/span><span style=\"color: #000000\">    \n}<\/span><\/pre>\n<p>Example uses of <span style=\"color: #008080\">Load-ComboBox<\/span>:<\/p>\n<pre><span style=\"color: #008080\">Load-ComboBox<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$combobox1<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"Red\"<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #ff0000\">\"White\"<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #ff0000\">\"Blue\"<\/span><\/pre>\n<p>Use the append parameter to add an additional item to the combo box\u2019s existing list: <\/p>\n<pre><span style=\"color: #008080\">Load-ComboBox<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$combobox1<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"Black\"<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">Append<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Load-ComboBox Append\" border=\"0\" alt=\"Load-ComboBox Append\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/06\/Load-ComboBox-Append.png\" width=\"276\" height=\"92\"> <\/p>\n<p>Use the DisplayMember parameter to use item object\u2019s&nbsp; property as the display value in the combo box:<\/p>\n<pre><span style=\"color: #008080\">Load-ComboBox<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$combobox1<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000ff; font-weight: bold\">Get-Process<\/span><span style=\"color: #000000\">) <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">DisplayMember <\/span><span style=\"color: #ff0000\">\"ProcessName\"<\/span><\/pre>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"DisplayMember\" border=\"0\" alt=\"DisplayMember\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/06\/DisplayMember.png\" width=\"288\" height=\"143\"> <\/p>\n<p><span style=\"color: #008080\">Load-ComboBox<\/span> without DisplayMember: <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Get-Process without DisplayMember\" border=\"0\" alt=\"Get-Process without DisplayMember\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/06\/Get-Process-without-DisplayMember.png\" width=\"288\" height=\"144\"> <\/p>\n<p>&nbsp; <\/p>\n<p><strong>SelectedItem<\/strong> <\/p>\n<p>Gets the selected item in the list. <\/p>\n<p><em>Why use the SelectedItem property?<\/em> <\/p>\n<p>Use the SelectedItem property when you want to retrieve the value selected by the user. If the combo box\u2019s DropDownStyle is not <em>DropDownList<\/em> and you are going to accept new values,&nbsp; it is recommend to use the Text property.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Text<\/strong><\/p>\n<p>Gets or sets the text the editable portion of a ComboBox. <\/p>\n<p><em>Why use the Text property?<\/em> <\/p>\n<p>Use the Text property when the user can manually enter a value or select a value from the dropdown list. Note: When the edit value is not found in the list the SelectedIndex will remain unchanged. <\/p>\n<p>&nbsp; <\/p>\n<p><strong>Sorted<\/strong><\/p>\n<p>This property specifies whether items in the list portion of the combo box are sorted. <\/p>\n<p>Set the Sorted property to <em>True<\/em> to display the list of items in alphabetical order.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>AutoComplete Properties:<\/strong><\/p>\n<p>To enable the Auto Complete feature of the ComboBox, refer to the <a href=\"http:\/\/www.sapien.com\/blog\/2011\/06\/13\/primalforms-2011-spotlight-on-the-textbox-control\/\">Spotlight on the TextBox control<\/a> blog. Note: In order to use the ComboBox\u2019s values as a source for the AutoComplete feature, set the AutoCompleteSource property to \u201cListItems\u201d.<\/p>\n<p>&nbsp;<\/p>\n<p><strong><font size=\"3\">Important Events:<\/font><\/strong><\/p>\n<p><strong>SelectedIndexChanged<\/strong><\/p>\n<p>Occurs when the value of the SelectedIndex property changes. <\/p>\n<p><em>Why use the SelectedIndexChanged event?<\/em> <\/p>\n<p>Use the SelectedIndexChanged event when you wish to respond to the user changing the value in the combo box. For example, you may want to display certain information when the user selects a new value from the combo box.<\/td>\n<p><strong><\/strong>&nbsp;<\/p>\n<p><strong>TextChanged<\/strong><\/p>\n<p>Occurs when the Text property value changes.<\/p>\n<p><em>Why use the TextChanged event?<\/em> <\/p>\n<p>Use the TextChanged event when you want to detect if the user types in the edit portion of the ComboBox or the user selects an item from the ComboBox\u2019s list.<\/p>\n<p>&nbsp;<\/p>\n<p>[Updated: 2\/25\/2013 \u2013 Replaced SelectedText with Text property and added TextChange event]<\/p>\n<p><strong>Next on Spotlight on Controls:<\/strong><\/p>\n<p>The CheckBox control.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The \u201cSpotlight on Controls\u201d series focuses on a single WinForms control in PrimalForms 2011 , details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms. Last time we took a look at the [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[575,644,25],"tags":[659,641,28,633,327],"class_list":["post-3121","post","type-post","status-publish","format-standard","hentry","category-primalforms-2011-software-news","category-spotlight-on-controls","category-windows-powershell","tag-combobox","tag-controls","tag-powershell","tag-primalforms-2011","tag-winform"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=3121"}],"version-history":[{"count":5,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3121\/revisions"}],"predecessor-version":[{"id":5752,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3121\/revisions\/5752"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=3121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=3121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=3121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}