locked
listbox with selection and entry field RRS feed

  • Question

  • below is portion of VM deploy code I've scraped together and actually learned a good bit but i'm struggling on how to add a 'entry' field if the selections (1, 2 4) are not what the user needs and wants to type in something else.  Am i going about this the wrong way trying to jam these two together in one box?

    thanks for any guidance!

    ###CPU
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    $form = New-Object System.Windows.Forms.Form 
    $form.Text = "Select number of vCPU's"
    $form.Size = New-Object System.Drawing.Size(300,200) 
    $form.StartPosition = "CenterScreen"
    
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $OKButton
    $form.Controls.Add($OKButton)
    
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(150,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $CancelButton
    $form.Controls.Add($CancelButton)
    
    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10,20) 
    $label.Size = New-Object System.Drawing.Size(280,20) 
    $label.Text = "Please select number of vCPUs:"
    $form.Controls.Add($label) 
    
    $listBox = New-Object System.Windows.Forms.ListBox 
    $listBox.Location = New-Object System.Drawing.Point(10,40) 
    $listBox.Size = New-Object System.Drawing.Size(260,20) 
    $listBox.Height = 80
    
    [void] $listBox.Items.Add("1")
    [void] $listBox.Items.Add("2")
    [void] $listBox.Items.Add("4")
    
    $form.Controls.Add($listBox) 
    
    $form.Topmost = $True
    
    $result = $form.ShowDialog()
    
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        $vmcpu = $listBox.SelectedItem
    }
    else
    {
        Write-Host "user exited script"
        Exit
    }



    • Edited by monderick Thursday, June 22, 2017 1:23 PM
    Thursday, June 22, 2017 12:51 PM

Answers

  • Just thrown this together. Hopefully it works for you or you can elaborate off of it :)

    Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $form = New-Object System.Windows.Forms.Form $form.Text = "Select number of vCPU's" $form.Size = New-Object System.Drawing.Size(300,220) $form.StartPosition = "CenterScreen" $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Point(75,150) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = "OK" $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.AcceptButton = $OKButton $form.Controls.Add($OKButton) $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Point(150,150) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Cancel" $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.CancelButton = $CancelButton $form.Controls.Add($CancelButton) $label = New-Object System.Windows.Forms.Label $label.Location = New-Object System.Drawing.Point(10,20) $label.Size = New-Object System.Drawing.Size(280,20) $label.Text = "Please select number of vCPUs:" $form.Controls.Add($label) $listBox = New-Object System.Windows.Forms.ListBox $listBox.Location = New-Object System.Drawing.Point(10,40) $listBox.Size = New-Object System.Drawing.Size(260,20) $listBox.Height = 80 $textBox = New-Object System.Windows.Forms.TextBox $textBox.Location = New-Object System.Drawing.Point(10,120) $textBox.Size = New-Object System.Drawing.Size(260,20) $form.Controls.Add($textBox) [void] $listBox.Items.Add("1") [void] $listBox.Items.Add("2") [void] $listBox.Items.Add("4") $form.Controls.Add($listBox) $form.Topmost = $True $result = $form.ShowDialog() if ($result -eq [System.Windows.Forms.DialogResult]::OK) { $y = $listBox.SelectedItem $y } $textbox.text

    By the way, the $testbox.text is the output from the GUI. Please remember to mark replies as answers if they are useful.


    • Edited by I.T Delinquent Thursday, June 22, 2017 1:50 PM
    • Marked as answer by monderick Thursday, June 22, 2017 1:59 PM
    Thursday, June 22, 2017 1:49 PM

All replies

  • Could put something like the below in an until loop so that the user has to enter a valid option.

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    $form = New-Object System.Windows.Forms.Form 
    $form.Text = "Last Logon Checker"
    $form.Size = New-Object System.Drawing.Size(300,200) 
    $form.StartPosition = "CenterScreen"
    
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $OKButton
    $form.Controls.Add($OKButton)
    
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(150,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $CancelButton
    $form.Controls.Add($CancelButton)
    
    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10,20) 
    $label.Size = New-Object System.Drawing.Size(280,20) 
    $label.Text = "Input the amount in days of inactivity to check for:"
    $form.Controls.Add($label) 
    
    $textBox = New-Object System.Windows.Forms.TextBox 
    $textBox.Location = New-Object System.Drawing.Point(10,40) 
    $textBox.Size = New-Object System.Drawing.Size(260,20) 
    $form.Controls.Add($textBox) 
    
    $form.Topmost = $True
    
    $form.Add_Shown({$textBox.Select()})
    $result = $form.ShowDialog()
    
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        $x = $textBox.Text
        $x
    }
    By the way, please use the "Insert Code Block" when posting code next time and select the correct language. Cheers.

    Thursday, June 22, 2017 1:18 PM
  • Thanks Mark!  Points me in a good direction but i'm hoping to have a list selection as well as a textbox together in one.

    i'll look into combining the two above

    Thursday, June 22, 2017 1:26 PM
  • Just thrown this together. Hopefully it works for you or you can elaborate off of it :)

    Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $form = New-Object System.Windows.Forms.Form $form.Text = "Select number of vCPU's" $form.Size = New-Object System.Drawing.Size(300,220) $form.StartPosition = "CenterScreen" $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Point(75,150) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = "OK" $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.AcceptButton = $OKButton $form.Controls.Add($OKButton) $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Point(150,150) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Cancel" $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.CancelButton = $CancelButton $form.Controls.Add($CancelButton) $label = New-Object System.Windows.Forms.Label $label.Location = New-Object System.Drawing.Point(10,20) $label.Size = New-Object System.Drawing.Size(280,20) $label.Text = "Please select number of vCPUs:" $form.Controls.Add($label) $listBox = New-Object System.Windows.Forms.ListBox $listBox.Location = New-Object System.Drawing.Point(10,40) $listBox.Size = New-Object System.Drawing.Size(260,20) $listBox.Height = 80 $textBox = New-Object System.Windows.Forms.TextBox $textBox.Location = New-Object System.Drawing.Point(10,120) $textBox.Size = New-Object System.Drawing.Size(260,20) $form.Controls.Add($textBox) [void] $listBox.Items.Add("1") [void] $listBox.Items.Add("2") [void] $listBox.Items.Add("4") $form.Controls.Add($listBox) $form.Topmost = $True $result = $form.ShowDialog() if ($result -eq [System.Windows.Forms.DialogResult]::OK) { $y = $listBox.SelectedItem $y } $textbox.text

    By the way, the $testbox.text is the output from the GUI. Please remember to mark replies as answers if they are useful.


    • Edited by I.T Delinquent Thursday, June 22, 2017 1:50 PM
    • Marked as answer by monderick Thursday, June 22, 2017 1:59 PM
    Thursday, June 22, 2017 1:49 PM
  • A ComboBox has autocomplete features and the ability to add whatever is typed as a new item.  A ComboBox can be displayed that looks just like a listbox but with a data entry box that both searches and adds new items.

    See: https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.autocompletemode(v=vs.110).aspx

    Here is a complete example of a dynamic list/combobox with auto detection of new items.

    function Show-Demo-CBAutoComplete_psf {
    	
    	Add-Type -AssemblyName System.Windows.Forms
    	#----------------------------------------------
    	#region Generated Form Objects
    	#----------------------------------------------
    	[System.Windows.Forms.Application]::EnableVisualStyles()
    	$form1 = New-Object 'System.Windows.Forms.Form'
    	$textbox1 = New-Object 'System.Windows.Forms.TextBox'
    	$cbTestList = New-Object 'System.Windows.Forms.ComboBox'
    	$buttonQuit = New-Object 'System.Windows.Forms.Button'
    	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    	#endregion Generated Form Objects
    	
    	#----------------------------------------------
    	# User Generated Script
    	#----------------------------------------------
    	
    	$form1_Load = {
    	}
    	
    	
    	$cbTestList_SelectedIndexChanged = {
    		$textbox1.Text = $cbTestList.SelectedItem
    	}
    	
    	$cbTestList_Validating = [System.ComponentModel.CancelEventHandler]{
    		#Event Argument: $_ = [System.ComponentModel.CancelEventArgs]
    		$textbox1.Clear()
    		if ($cbTestList.Text -and $cbTestList.Text -notin $cbTestList.Items) {
    			$msg = "Value not in list $($cbTestList.Text).`n Do you wish to use this value?"
    			if ('Yes' -eq [System.Windows.Forms.MessageBox]::Show($msg, 'Add value', 'YesNo')) {
    				$textbox1.Text = $cbTestList.Text
    				$cbTestList.Items.Add($cbTestList.Text)
    			}
    			
    		} else {
    			$textbox1.Text = $cbTestList.Text
    		}
    	}
    	
    	# --End User Generated Script--
    	#----------------------------------------------
    	#region Generated Events
    	#----------------------------------------------
    	
    	$Form_StateCorrection_Load =
    	{
    		#Correct the initial state of the form to prevent the .Net maximized form issue
    		$form1.WindowState = $InitialFormWindowState
    	}
    	
    	$Form_Cleanup_FormClosed =
    	{
    		#Remove all event handlers from the controls
    		try {
    			$cbTestList.remove_SelectedIndexChanged($cbTestList_SelectedIndexChanged)
    			$cbTestList.remove_Validating($cbTestList_Validating)
    			$form1.remove_Load($form1_Load)
    			$form1.remove_Load($Form_StateCorrection_Load)
    			$form1.remove_FormClosed($Form_Cleanup_FormClosed)
    		} catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
    	}
    	#endregion Generated Events
    	
    	#----------------------------------------------
    	#region Generated Form Code
    	#----------------------------------------------
    	$form1.SuspendLayout()
    	#
    	# form1
    	#
    	$form1.Controls.Add($textbox1)
    	$form1.Controls.Add($cbTestList)
    	$form1.Controls.Add($buttonQuit)
    	$form1.AutoScaleDimensions = '8, 17'
    	$form1.AutoScaleMode = 'Font'
    	$form1.CancelButton = $buttonQuit
    	$form1.ClientSize = '339, 343'
    	$form1.FormBorderStyle = 'FixedDialog'
    	$form1.Margin = '5, 5, 5, 5'
    	$form1.MaximizeBox = $False
    	$form1.MinimizeBox = $False
    	$form1.Name = 'form1'
    	$form1.StartPosition = 'CenterScreen'
    	$form1.Text = 'Form'
    	$form1.add_Load($form1_Load)
    	#
    	# textbox1
    	#
    	$textbox1.Location = '185, 16'
    	$textbox1.Margin = '4, 4, 4, 4'
    	$textbox1.Name = 'textbox1'
    	$textbox1.Size = '132, 23'
    	$textbox1.TabIndex = 2
    	#
    	# cbTestList
    	#
    	$cbTestList.AutoCompleteMode = 'Append'
    	$cbTestList.AutoCompleteSource = 'ListItems'
    	$cbTestList.DropDownStyle = 'Simple'
    	$cbTestList.FlatStyle = 'System'
    	$cbTestList.FormattingEnabled = $True
    	[void]$cbTestList.Items.Add('cats')
    	[void]$cbTestList.Items.Add('dogs')
    	[void]$cbTestList.Items.Add('one')
    	[void]$cbTestList.Items.Add('three')
    	[void]$cbTestList.Items.Add('two')
    	$cbTestList.Location = '16, 16'
    	$cbTestList.Margin = '5, 5, 5, 5'
    	$cbTestList.Name = 'cbTestList'
    	$cbTestList.Size = '160, 254'
    	$cbTestList.Sorted = $True
    	$cbTestList.TabIndex = 1
    	$cbTestList.add_SelectedIndexChanged($cbTestList_SelectedIndexChanged)
    	$cbTestList.add_Validating($cbTestList_Validating)
    	#
    	# buttonQuit
    	#
    	$buttonQuit.Anchor = 'Bottom, Right'
    	$buttonQuit.DialogResult = 'Cancel'
    	$buttonQuit.Location = '226, 240'
    	$buttonQuit.Margin = '4, 4, 4, 4'
    	$buttonQuit.Name = 'buttonQuit'
    	$buttonQuit.Size = '100, 30'
    	$buttonQuit.TabIndex = 0
    	$buttonQuit.TabStop = $False
    	$buttonQuit.Text = '&Quit'
    	$buttonQuit.UseVisualStyleBackColor = $True
    	$form1.ResumeLayout()
    	#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($Form_StateCorrection_Load)
    	#Clean up the control events
    	$form1.add_FormClosed($Form_Cleanup_FormClosed)
    	#Show the Form
    	$form1.ShowDialog()
    	
    } #End Function
    
    #Call the form
    Show-Demo-CBAutoComplete_psf | Out-Null


    \_(ツ)_/


    • Edited by jrv Thursday, June 22, 2017 1:51 PM
    Thursday, June 22, 2017 1:50 PM
  • Thanks again Mark!  Exactly what i was looking for and the code makes sense
    Thursday, June 22, 2017 2:23 PM
  • thanks jrv!  copying this for future reference
    Thursday, June 22, 2017 2:24 PM