Combobox variable for memory set GB, MB, KB

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Combobox variable for memory set GB, MB, KB

Post by obrienc »

I have a req to load a combobox so someone can choose memory for a VM from a dropdown.
  1. Load-ComboBox -ComboBox $combobox4 -Items (1 .. 8)
In Powershell this works but I want to turn it into a variable
  1. Set-VM -Name $a$_ -DynamicMemory -MemoryStartupBytes 4GB
  1. $vmem = $combobox4.SelectedItem
  2.     $m = "GB"
I've tried a few different things $vmem"GB" $vmem$m
  1. Set-VM -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryStartupBytes $vmem+$m
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combobox variable for memory set GB, MB, KB

Post by jvierra »

JUst add an object to the combo that has the string 1Gb and the value 1Gb and use the value.

$items = 1..10 | %{ [pscustomobject]@{Name=$_;Value=$_ * 1Gb}}
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: Combobox variable for memory set GB, MB, KB

Post by obrienc »

thanks for the reply. it broke something so I'm not sure if I was to take your sample and use it or figure out how to use it but i am a little lost.

The dropdown displays this @{Name=1; Value=1073741824} instead of an integer 1..8

ERROR: Set-VM : Cannot bind parameter 'MemoryMinimumBytes'. Cannot convert value "@{Name=4; Value=4294967296}" to type "System.Nullable`1[System.Int64]". Error:
ERROR: "Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value."

I use $_ and $a to append VM names in a loop = $combobox1.SelectedItem
  1. foreach ($_ in 1 .. $a)
  2.         {
  3.             New-VM -Name $a$_ -ComputerName $s1 -SwitchName vSwitch0 -BootDevice CD -Generation 1
  4.             Set-VM -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryMinimumBytes $vmem
  5.             Set-VMProcessor -ComputerName $s1 -VMName $a$_ -Count $numproc -Reserve 10 -Maximum 100 -RelativeWeight 100
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combobox variable for memory set GB, MB, KB

Post by jvierra »

$combobox.Displayname = 'name'
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: Combobox variable for memory set GB, MB, KB

Post by obrienc »

I get a $combobox1.DisplayMember as a choice not a DisplayName. The dropdown doesn't display just an integer correctly. here is the form.
Attachments
TestForm.psf
(40.27 KiB) Downloaded 148 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combobox variable for memory set GB, MB, KB

Post by jvierra »

  1. $items = 1..10 |
  2.         ForEach-Object{
  3.             [pscustomobject]@{
  4.                 Name = '{0} Gb' -f $_
  5.                 Value = $_ * 1Gb
  6.             }
  7.         }
  8.     $combobox4.DataSource = [System.Collections.ArrayList]$items
  9.     $combobox4.DisplayMember = 'Name'
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combobox variable for memory set GB, MB, KB

Post by jvierra »

You can also do it like this:
  1. $items = 1..10 |
  2.         ForEach-Object{
  3.             [pscustomobject]@{
  4.                 Name = "$_`Gb"
  5.                 Value = $_ * 1Gb
  6.             }
  7.         }
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: Combobox variable for memory set GB, MB, KB

Post by obrienc »

Thank you. Should I omit
  1. $vmem = $combobox4.SelectedItem
?

I still get an error that complains about ERROR: A parameter that is not valid was passed to the operation. I used $vmem as the variable

here
  1. Set-VM -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryMinimumBytes $vmem
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combobox variable for memory set GB, MB, KB

Post by jvierra »

$vmem = $combobox4.SelectedItem.Value
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: Combobox variable for memory set GB, MB, KB

Post by obrienc »

It showed null. Set-VM : Cannot validate argument on parameter 'MemoryMinimumBytes'. The argument is null. Provide a valid value for the argument, and then try running the

MainForm.psf (39, 75): ERROR: At Line: 39 char: 75
ERROR: + ... -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryMinimumBytes $vmem

I tried
  1. $vmem = $combobox4.SelectedValue
This topic is 6 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked