Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Listbox

Reply
 
Old 12-02-2007   #1 (permalink)
Anatoli


 
 

Listbox

Hi,

Can someone kindly show me how to create a listbox where I can select
multiple selections and pass them to a variable? I can't seem to find an
example of this anywhere using powershell.

thanks,

My System SpecsSystem Spec
Old 12-02-2007   #2 (permalink)
Shay Levi


 
 

Re: Listbox

Hi

I used Jeffery Snover's get-guiinput.ps1 script and added the listbox code:




[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")


###############################
# Create the form
$form = New-Object System.Windows.Forms.Form
$form.add_shown({$form.Activate()})
$form.Text = "PowerShell GUI Input"
$form.height = 110

###############################
# Create the OK button and when the OK button is clicked hide the form
$OK = new-object system.windows.forms.button
$OK.Text = "OK"
$OK.dock = "bottom"
$OK.Add_Click({
$form.hide()
})

$lb = new-object System.Windows.Forms.ListBox
[void]$lb.Items.Add("powershell")
[void]$lb.Items.Add("rocks!")

###############################
# add the controls to the form
$form.Controls.AddRange(@($lb,$OK))


###############################
# show the form
$results = $form.showdialog()
# show the results
$lb.SelectedItem
$lb.SelectedIndex
write-host "SelectedItem=$SelectedItem"
write-host "SelectedIndex=$SelectedIndex"

$form.dispose()





-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Hi,
>
> Can someone kindly show me how to create a listbox where I can select
> multiple selections and pass them to a variable? I can't seem to find
> an example of this anywhere using powershell.
>
> thanks,
>

My System SpecsSystem Spec
Old 12-02-2007   #3 (permalink)
Anatoli


 
 

Re: Listbox

Thanks Shay, is there a way to change this around to be a drop down list and
how does one select more than one item? I tried to select both items supplied
in the script and couldn't.

regards,

Anatoli

"Shay Levi" wrote:
Quote:

> Hi
>
> I used Jeffery Snover's get-guiinput.ps1 script and added the listbox code:
>
>
>
>
> [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
>
>
> ###############################
> # Create the form
> $form = New-Object System.Windows.Forms.Form
> $form.add_shown({$form.Activate()})
> $form.Text = "PowerShell GUI Input"
> $form.height = 110
>
> ###############################
> # Create the OK button and when the OK button is clicked hide the form
> $OK = new-object system.windows.forms.button
> $OK.Text = "OK"
> $OK.dock = "bottom"
> $OK.Add_Click({
> $form.hide()
> })
>
> $lb = new-object System.Windows.Forms.ListBox
> [void]$lb.Items.Add("powershell")
> [void]$lb.Items.Add("rocks!")
>
> ###############################
> # add the controls to the form
> $form.Controls.AddRange(@($lb,$OK))
>
>
> ###############################
> # show the form
> $results = $form.showdialog()
> # show the results
> $lb.SelectedItem
> $lb.SelectedIndex
> write-host "SelectedItem=$SelectedItem"
> write-host "SelectedIndex=$SelectedIndex"
>
> $form.dispose()
>
>
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > Hi,
> >
> > Can someone kindly show me how to create a listbox where I can select
> > multiple selections and pass them to a variable? I can't seem to find
> > an example of this anywhere using powershell.
> >
> > thanks,
> >
>
>
>
My System SpecsSystem Spec
Old 12-02-2007   #4 (permalink)
Shay Levi


 
 

Re: Listbox

Anatoli,

Add this line after $lb the declaration:

$lb.SelectionMode = [System.Windows.Forms.SelectionMode]::MultiSimple

or

$lb.SelectionMode = [System.Windows.Forms.SelectionMode]::MultiExtended


Then call

$lb.SelectedItems


From MSDN

The SelectionMode property enables you to determine how many items in the
ListBox a user can select at one time and how the user can make multiple-selections.
When the SelectionMode property is set to SelectionMode.MultiExtended, pressing
SHIFT and clicking the mouse or pressing SHIFT and one of the arrow keys
(UP ARROW, DOWN ARROW, LEFT ARROW, and RIGHT ARROW) extends the selection
from the previously selected item to the current item. Pressing CTRL and
clicking the mouse selects or deselects an item in the list. When the property
is set to SelectionMode.MultiSimple, a mouse click or pressing the SPACEBAR
selects or deselects an item in the list.


I'll have to check for the drop down list tommorow, my wife wants to watch
a movie now :-)
You can find more information here:

http://msdn2.microsoft.com/en-us/lib...roperties.aspx


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Thanks Shay, is there a way to change this around to be a drop down
> list and how does one select more than one item? I tried to select
> both items supplied in the script and couldn't.
>
> regards,
>
> Anatoli
>
> "Shay Levi" wrote:
>
Quote:

>> Hi
>>
>> I used Jeffery Snover's get-guiinput.ps1 script and added the listbox
>> code:
>>
>> [void][reflection.assembly]::LoadWithPartialName("System.Windows.Form
>> s")
>>
>> ###############################
>> # Create the form
>> $form = New-Object System.Windows.Forms.Form
>> $form.add_shown({$form.Activate()})
>> $form.Text = "PowerShell GUI Input"
>> $form.height = 110
>> ###############################
>> # Create the OK button and when the OK button is clicked hide the
>> form
>> $OK = new-object system.windows.forms.button
>> $OK.Text = "OK"
>> $OK.dock = "bottom"
>> $OK.Add_Click({
>> $form.hide()
>> })
>> $lb = new-object System.Windows.Forms.ListBox
>> [void]$lb.Items.Add("powershell")
>> [void]$lb.Items.Add("rocks!")
>> ###############################
>> # add the controls to the form
>> $form.Controls.AddRange(@($lb,$OK))
>> ###############################
>> # show the form
>> $results = $form.showdialog()
>> # show the results
>> $lb.SelectedItem
>> $lb.SelectedIndex
>> write-host "SelectedItem=$SelectedItem"
>> write-host "SelectedIndex=$SelectedIndex"
>> $form.dispose()
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> Hi,
>>>
>>> Can someone kindly show me how to create a listbox where I can
>>> select multiple selections and pass them to a variable? I can't seem
>>> to find an example of this anywhere using powershell.
>>>
>>> thanks,
>>>

My System SpecsSystem Spec
Old 12-02-2007   #5 (permalink)
Shay Levi


 
 

Re: Listbox

Small update before the movie starts, to display the listbox with pre selected
items:

$lb = new-object System.Windows.Forms.ListBox
$lb.SelectionMode = [System.Windows.Forms.SelectionMode]::MultiExtended

[void]$lb.Items.Add("powershell")
[void]$lb.Items.Add("rocks!")
$lb.SetSelected(0,$true)
$lb.SetSelected(1,$true)
$form.Controls.AddRange(@($lb,$OK))



HTH


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Anatoli,
>
> Add this line after $lb the declaration:
>
> $lb.SelectionMode = [System.Windows.Forms.SelectionMode]::MultiSimple
>
> or
>
> $lb.SelectionMode =
> [System.Windows.Forms.SelectionMode]::MultiExtended
>
> Then call
>
> $lb.SelectedItems
>
> From MSDN
>
> The SelectionMode property enables you to determine how many items in
> the ListBox a user can select at one time and how the user can make
> multiple-selections. When the SelectionMode property is set to
> SelectionMode.MultiExtended, pressing SHIFT and clicking the mouse or
> pressing SHIFT and one of the arrow keys (UP ARROW, DOWN ARROW, LEFT
> ARROW, and RIGHT ARROW) extends the selection from the previously
> selected item to the current item. Pressing CTRL and clicking the
> mouse selects or deselects an item in the list. When the property is
> set to SelectionMode.MultiSimple, a mouse click or pressing the
> SPACEBAR selects or deselects an item in the list.
>
> I'll have to check for the drop down list tommorow, my wife wants to
> watch
> a movie now :-)
> You can find more information here:
> http://msdn2.microsoft.com/en-us/lib...forms.listbox_
> properties.aspx
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>> Thanks Shay, is there a way to change this around to be a drop down
>> list and how does one select more than one item? I tried to select
>> both items supplied in the script and couldn't.
>>
>> regards,
>>
>> Anatoli
>>
>> "Shay Levi" wrote:
>>
Quote:

>>> Hi
>>>
>>> I used Jeffery Snover's get-guiinput.ps1 script and added the
>>> listbox code:
>>>
>>> [void][reflection.assembly]::LoadWithPartialName("System.Windows.For
>>> m s")
>>>
>>> ###############################
>>> # Create the form
>>> $form = New-Object System.Windows.Forms.Form
>>> $form.add_shown({$form.Activate()})
>>> $form.Text = "PowerShell GUI Input"
>>> $form.height = 110
>>> ###############################
>>> # Create the OK button and when the OK button is clicked hide the
>>> form
>>> $OK = new-object system.windows.forms.button
>>> $OK.Text = "OK"
>>> $OK.dock = "bottom"
>>> $OK.Add_Click({
>>> $form.hide()
>>> })
>>> $lb = new-object System.Windows.Forms.ListBox
>>> [void]$lb.Items.Add("powershell")
>>> [void]$lb.Items.Add("rocks!")
>>> ###############################
>>> # add the controls to the form
>>> $form.Controls.AddRange(@($lb,$OK))
>>> ###############################
>>> # show the form
>>> $results = $form.showdialog()
>>> # show the results
>>> $lb.SelectedItem
>>> $lb.SelectedIndex
>>> write-host "SelectedItem=$SelectedItem"
>>> write-host "SelectedIndex=$SelectedIndex"
>>> $form.dispose()
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>>>> Hi,
>>>>
>>>> Can someone kindly show me how to create a listbox where I can
>>>> select multiple selections and pass them to a variable? I can't
>>>> seem to find an example of this anywhere using powershell.
>>>>
>>>> thanks,
>>>>

My System SpecsSystem Spec
Old 12-02-2007   #6 (permalink)
Oisin Grehan


 
 

Re: Listbox

On Dec 2, 3:05 pm, Anatoli <Anat...@xxxxxx> wrote:
Quote:

> Hi,
>
> Can someone kindly show me how to create a listbox where I can select
> multiple selections and pass them to a variable? I can't seem to find an
> example of this anywhere using powershell.
>
> thanks,
Hi Anatoli,

I've written a blog post detailing how to do this (it requires a
snapin to do it cleanly):

http://www.nivot.org/2007/12/02/Usin...l1ListBox.aspx

It's also available on my PowerShellCommunity.org blog, available via:
http://oisin.powershell.com/

Hope this helps,

- Oisin / x0n
http://www.nivot.org
http://oisin.powershell.com


My System SpecsSystem Spec
Old 12-03-2007   #7 (permalink)
Shay Levi


 
 

Re: Listbox

Hi

here's a sample with a combo box, it works quite the same as ListBox

$cb = new-object System.Windows.Forms.ComboBox
[void]$cb.Items.Add("powershell")
[void]$cb.Items.Add("rocks!")
$form.Controls.AddRange(@($cb,$OK))
$results = $form.showdialog()

write-host "SelectedItem=$($cb.SelectedItem)"
write-host "SelectedIndex=$($cb.SelectedIndex)"

More info on MSDN:
http://msdn2.microsoft.com/en-us/lib....combobox.aspx

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Thanks Shay, is there a way to change this around to be a drop down
> list and how does one select more than one item? I tried to select
> both items supplied in the script and couldn't.
>
> regards,
>
> Anatoli
>
> "Shay Levi" wrote:
>
Quote:

>> Hi
>>
>> I used Jeffery Snover's get-guiinput.ps1 script and added the listbox
>> code:
>>
>> [void][reflection.assembly]::LoadWithPartialName("System.Windows.Form
>> s")
>>
>> ###############################
>> # Create the form
>> $form = New-Object System.Windows.Forms.Form
>> $form.add_shown({$form.Activate()})
>> $form.Text = "PowerShell GUI Input"
>> $form.height = 110
>> ###############################
>> # Create the OK button and when the OK button is clicked hide the
>> form
>> $OK = new-object system.windows.forms.button
>> $OK.Text = "OK"
>> $OK.dock = "bottom"
>> $OK.Add_Click({
>> $form.hide()
>> })
>> $lb = new-object System.Windows.Forms.ListBox
>> [void]$lb.Items.Add("powershell")
>> [void]$lb.Items.Add("rocks!")
>> ###############################
>> # add the controls to the form
>> $form.Controls.AddRange(@($lb,$OK))
>> ###############################
>> # show the form
>> $results = $form.showdialog()
>> # show the results
>> $lb.SelectedItem
>> $lb.SelectedIndex
>> write-host "SelectedItem=$SelectedItem"
>> write-host "SelectedIndex=$SelectedIndex"
>> $form.dispose()
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> Hi,
>>>
>>> Can someone kindly show me how to create a listbox where I can
>>> select multiple selections and pass them to a variable? I can't seem
>>> to find an example of this anywhere using powershell.
>>>
>>> thanks,
>>>

My System SpecsSystem Spec
Old 12-04-2007   #8 (permalink)
Jon


 
 

Re: Listbox


"Anatoli" <Anatoli@xxxxxx> wrote in message
news:3D895363-ED33-4564-8CFC-C12D0A76599B@xxxxxx
Quote:

> Thanks Shay, is there a way to change this around to be a drop down list
> and
> how does one select more than one item? I tried to select both items
> supplied
> in the script and couldn't.
>
> regards,
>
> Anatoli
>

I'll make a general suggestion.

I've found it sometimes helps to design your form the way you like it, in
Visual Studio, - customize it according to taste - and then set about
converting the code in the associated form file to powershell code.

Hope that helps.

--
Jon




My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
ListBox vs. ListView .NET General
Set Listbox Value .NET General
Easy to get handle from listbox. How to get listbox from handle? .NET General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46