How to deal with a ComboBox control
There is a very simple, but very useful control. It may
– be filled dynamically and manually
– accept user input (to disable user input is also possibly)
– pass its data to another control by user’s choice.
Well, let’s list what we are going to do with a combobox today:
1) accept value from a single textbox (by clicking a button)
2) accept values from multiple textboxes (from two of them by clicking a button)
3) accept user input and add it to the list (and discuss how to disable user input)
4) clear the list
5) use a value of user’s choise to set a value of one another control (the caption of the form)
6) Intellisense on user input
7) sorting items
If it’s enough, now turn to coding. Traditionally, the code bundle is attached in the box at right and it’s named, not surprisingly, HowToDealWithAComboBoxControl.zip.
The first our topic is how to accept user input. The following lines of code are taken from the $handler_button1_Click eventhandler:
$comboBox1.Items.Add($textBox1.Text); $comboBox1.Items.AddRange(($textBox1.Text, $textBox2.Text)); $comboBox1.Items.Add($comboBox1.Text);
In all three cases, data, usually of string type, is added by using such methods as Add(item) or AddRange(array of items). As can be seen, both methods are very easy to use.
Next, how we can prevent typing into a combobox? The answer is to set the DropDownStyle property:
$comboBox1.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
After that, how to clean up our combobox?
$comboBox1.Items.Clear(); Next, how to grab a value from inside the user input?
$form1.Text = $comboBox1.Text;
How to sort data?
$comboBox1.Sorted = $true;
At last, how to use Intellisense? Please note, the DropDownStyle property should be set to System.Windows.Forms.ComboBoxStyle]::DropDown.
$comboBox1.AutoCompleteCustomSource.Add("System.Windows.Forms"); $comboBox1.AutoCompleteCustomSource.AddRange(("System.Data", "Microsoft")); $comboBox1.AutoCompleteMode = [System.Windows.Forms.AutoCompleteMode]::SuggestAppend; $comboBox1.AutoCompleteSource = [System.Windows.Forms.AutoCompleteSource]::CustomSource;
To test the Intellisense that is tuned above, type m or s in the combobox.
The whole sample is below:
#Generated Form Function function GenerateForm { ######################################################################## # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.8.0 # Generated On: 21.07.2010 1:32 # Generated By: Administrator ######################################################################## #region Import the Assemblies [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null #endregion #region Generated Form Objects $form1 = New-Object System.Windows.Forms.Form $button1 = New-Object System.Windows.Forms.Button $button2 = New-Object System.Windows.Forms.Button $textBox2 = New-Object System.Windows.Forms.TextBox $textBox1 = New-Object System.Windows.Forms.TextBox $comboBox1 = New-Object System.Windows.Forms.ComboBox $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState #endregion Generated Form Objects #---------------------------------------------- #Generated Event Script Blocks #---------------------------------------------- #Provide Custom Code for events specified in PrimalForms. #region $handler_comboBox1_SelectedIndexChanged $handler_comboBox1_SelectedIndexChanged= { #TODO: Place custom script here try{ if ($comboBox1.Text.Length -gt 0) { $form1.Text = $comboBox1.Text; } }catch{} #in case no items are available } #endregion $handler_comboBox1_SelectedIndexChanged #region $handler_button1_Click $handler_button1_Click= { #TODO: Place custom script here try{ #How to add one item #$textBox1.Text if ($textBox1.Text.Length -gt 0 -and ` $textBox2.Text.Length -eq 0) { $comboBox1.Items.Add($textBox1.Text); Write-Host "Added " $textBox1.Text "from `$textBox1"; } #$textBox2.Text elseif ($textBox1.Text.Length -eq 0 -and ` $textBox2.Text.Length -gt 0) { $comboBox1.Items.Add($textBox2.Text); Write-Host "Added " $textBox2.Text "from `$textBox2"; } #$textBox1.Text and $textBox2.Text elseif ($textBox1.Text.Length -gt 0 -and ` $textBox2.Text.Length -gt 0) { $comboBox1.Items.AddRange(($textBox1.Text, $textBox2.Text)); Write-Host "Added " $textBox1.Text "from `$textBox1"; Write-Host "Added " $textBox2.Text "from `$textBox2"; } else { #Nothing to add #$textBox1.Text.Length -eq 0 #$textBox2.Text.Length -eq 0 } #At last, if the user typed something if ($comboBox1.Text.Length -gt 0) { $comboBox1.Items.Add($comboBox1.Text); Write-Host "Added " $comboBox1.Text "from `$comboBox1"; } }catch{} #for example, no data in the both textboxes } #endregion $handler_button1_Click #region $handler_button2_Click $handler_button2_Click= { #TODO: Place custom script here try{ #Clean up the combo box $comboBox1.Items.Clear(); $comboBox1.Text = ""; }catch{} } #endregion $handler_button2_Click $OnLoadForm_StateCorrection= {#Correct the initial state of the form to prevent the .Net maximized form issue $form1.WindowState = $InitialFormWindowState } #---------------------------------------------- #region Generated Form Code #region $form1 $form1.Text = "Primal Form" $form1.Name = "form1" $form1.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 282 $System_Drawing_Size.Height = 220 $form1.ClientSize = $System_Drawing_Size #endregion $form1 #region $button1 $button1.TabIndex = 3 $button1.Name = "button1" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 70 $System_Drawing_Size.Height = 30 $button1.Size = $System_Drawing_Size $button1.UseVisualStyleBackColor = $True $button1.Text = "button1" $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 195 $System_Drawing_Point.Y = 160 $button1.Location = $System_Drawing_Point $button1.DataBindings.DefaultDataSourceUpdateMode = 0 $button1.add_Click($handler_button1_Click) $form1.Controls.Add($button1) #endregion $button1 #region $button2 $button2.TabIndex = 4 $button2.Name = "button2" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 70 $System_Drawing_Size.Height = 30 $button2.Size = $System_Drawing_Size $button2.UseVisualStyleBackColor = $True $button2.Text = "button2" $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 15 $System_Drawing_Point.Y = 160 $button2.Location = $System_Drawing_Point $button2.DataBindings.DefaultDataSourceUpdateMode = 0 $button2.add_Click($handler_button2_Click) $form1.Controls.Add($button2) #endregion $button2 #region $textBox1 $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 250 $System_Drawing_Size.Height = 20 $textBox1.Size = $System_Drawing_Size $textBox1.DataBindings.DefaultDataSourceUpdateMode = 0 $textBox1.Name = "textBox1" $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 15 $System_Drawing_Point.Y = 70 $textBox1.Location = $System_Drawing_Point $textBox1.TabIndex = 1 $form1.Controls.Add($textBox1) #endregion $textBox1 #region $textBox2 $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 250 $System_Drawing_Size.Height = 20 $textBox2.Size = $System_Drawing_Size $textBox2.DataBindings.DefaultDataSourceUpdateMode = 0 $textBox2.Name = "textBox2" $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 15 $System_Drawing_Point.Y = 120 $textBox2.Location = $System_Drawing_Point $textBox2.TabIndex = 2 $form1.Controls.Add($textBox2) #endregion $textBox2 #region $comboBox1 $comboBox1.FormattingEnabled = $True $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Width = 250 $System_Drawing_Size.Height = 21 $comboBox1.Size = $System_Drawing_Size $comboBox1.DataBindings.DefaultDataSourceUpdateMode = 0 $comboBox1.Name = "comboBox1" $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 15 $System_Drawing_Point.Y = 20 $comboBox1.Location = $System_Drawing_Point $comboBox1.TabIndex = 0 $comboBox1.add_SelectedIndexChanged($handler_comboBox1_SelectedIndexChanged) $comboBox1.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDown; $comboBox1.Sorted = $true; $comboBox1.AutoCompleteCustomSource.Add("System.Windows.Forms"); $comboBox1.AutoCompleteCustomSource.AddRange(("System.Data", "Microsoft")); $comboBox1.AutoCompleteMode = [System.Windows.Forms.AutoCompleteMode]::SuggestAppend; $comboBox1.AutoCompleteSource = [System.Windows.Forms.AutoCompleteSource]::CustomSource; $form1.Controls.Add($comboBox1) #endregion $comboBox1 #endregion Generated Form Code #Save the initial state of the form $InitialFormWindowState = $form1.WindowState #Init the OnLoad event to correct the initial state of the form $form1.add_Load($OnLoadForm_StateCorrection) #Show the Form $form1.ShowDialog()| Out-Null } #End Function #Call the Function GenerateForm
[…] HowToDealWithAComboBoxControl.zip referring to How to deal with a ComboBox control […]
Direct links also came « PowerShell In GUI Blog
March 15, 2011 at 6:50 pm
Thanks man, just what I needed!
Yomodo
May 30, 2012 at 7:30 pm
Hi Yomodo, if you’d like to test 🙂 the graphical PS tool you will be creating, visit my blog http://SoftwareTestingUsingPowerShell.com
Alexander Petrovskiy
May 31, 2012 at 7:10 pm
[…] https://powershelldevtools.wordpress.com/2010/07/20/how-to-deal-with-a-combobox-control/ […]
PowerShell GUI – Pelipalvelimien pingaustyökalu & skripti .exe-tiedostoksi – Markus Pyhäranta
July 22, 2017 at 10:53 pm
“Next, how we can prevent typing into a combobox? The answer is to set the DropDownStyle property:
$comboBox1.DropDownStyle =
[System.Windows.Forms.ComboBoxStyle]::DropDownList;”
This was a great tip! Thanks so much!
Anonymous
September 27, 2017 at 9:49 pm
$comboBox1.DropDownStyle =
[System.Windows.Forms.ComboBoxStyle]::DropDownList;
This saved me heaps ! thank you
Anonymous
February 2, 2018 at 7:37 am