Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Control Arrays

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-15-2006   #1 (permalink)
fixitchris
Guest


 

Control Arrays

how can i create an array of RADIOBUTTONS and refer to them byindex instead
of individuall as per this code:

$opt0 = New-Object windows.forms.radiobutton;
$opt1 = New-Object windows.forms.radiobutton;
$opt2 = New-Object windows.forms.radiobutton;
$opt3 = New-Object windows.forms.radiobutton;
$opt4 = New-Object windows.forms.radiobutton;

My System SpecsSystem Spec
Old 11-15-2006   #2 (permalink)
dreeschkind
Guest


 

RE: Control Arrays

[object[]]$a = New-Object windows.forms.radiobutton;

$a += New-Object windows.forms.radiobutton;
$a += New-Object windows.forms.radiobutton;

$a.count

$a[0]

--
greetings
dreeschkind

"fixitchris" wrote:

> how can i create an array of RADIOBUTTONS and refer to them byindex instead
> of individuall as per this code:
>
> $opt0 = New-Object windows.forms.radiobutton;
> $opt1 = New-Object windows.forms.radiobutton;
> $opt2 = New-Object windows.forms.radiobutton;
> $opt3 = New-Object windows.forms.radiobutton;
> $opt4 = New-Object windows.forms.radiobutton;

My System SpecsSystem Spec
Old 11-15-2006   #3 (permalink)
Andrew Watt [MVP]
Guest


 

Re: Control Arrays

[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

[System.Windows.Forms.RadioButton[]]$buttons = new-object
System.Windows.Forms

for ($i = 0; $i -lt 4; $i++){$buttons += new-object
System.Windows.Forms.Radio

You can see that you have added the correct number of objects using:
$buttons.Length

To see that each element is of the desired type:
for ($i = 0; $i -lt 5; $i++){$buttons[$i].GetType()}

Reference an element using square brackets:
$buttons[2]

Andrew Watt MVP

On Wed, 15 Nov 2006 11:29:01 -0800, fixitchris
<fixitchris@discussions.microsoft.com> wrote:

>how can i create an array of RADIOBUTTONS and refer to them byindex instead
>of individuall as per this code:
>
>$opt0 = New-Object windows.forms.radiobutton;
>$opt1 = New-Object windows.forms.radiobutton;
>$opt2 = New-Object windows.forms.radiobutton;
>$opt3 = New-Object windows.forms.radiobutton;
>$opt4 = New-Object windows.forms.radiobutton;

My System SpecsSystem Spec
Old 11-15-2006   #4 (permalink)
Andrew Watt [MVP]
Guest


 

Re: Control Arrays

On Wed, 15 Nov 2006 20:11:38 +0000, "Andrew Watt [MVP]"
<SVGDeveloper@aol.com> wrote:

Oops, something went wrong in the pasting. The following ...

>for ($i = 0; $i -lt 4; $i++){$buttons += new-object
>System.Windows.Forms.Radio


should be
for ($i = 0; $i -lt 4; $i++){$buttons += new-object
System.Windows.Forms.RadioButton}

Andrew Watt MVP
My System SpecsSystem Spec
Old 11-15-2006   #5 (permalink)
Bruce Payette [MSFT]
Guest


 

Re: Control Arrays

Note that you should initialize the $buttons variable in this case which
might look like:

for ($($buttons = @();$i = 0); $i -lt 4; $i++){$buttons += new-object
System.Windows.Forms.RadioButton}

or, outside the for statement,

$buttons = @(); for ($i=0; $i -lt 4; $i++) { $buttons += new-object
System.Windows.Forms.RadioButton}

But here's a slightly different approach:

$buttons = 1..4 |%{new-object System.Windows.Forms.Button}

This takes advantage of the fact that we accumulate output from a pipeline
into an array. This is actually a good deal more efficient than using += to
build up the array. You could also do it using the for loop in a
sub-expression:

$buttons = @(for ($i=0; $i -lt 4; $i++) { new-object
System.Windows.Forms.Button })

-bruce

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.



"Andrew Watt [MVP]" <SVGDeveloper@aol.com> wrote in message
news:egtml29hbmkgoretvt9dfksr1mmrm49nh3@4ax.com...
> On Wed, 15 Nov 2006 20:11:38 +0000, "Andrew Watt [MVP]"
> <SVGDeveloper@aol.com> wrote:
>
> Oops, something went wrong in the pasting. The following ...
>
>>for ($i = 0; $i -lt 4; $i++){$buttons += new-object
>>System.Windows.Forms.Radio

>
> should be
> for ($i = 0; $i -lt 4; $i++){$buttons += new-object
> System.Windows.Forms.RadioButton}
>
> Andrew Watt MVP



My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays Jason Massie PowerShell 10 05-14-2008 07:37 AM
Arrays in Powershell Daren Daigle PowerShell 2 12-03-2007 07:37 AM
Multidimensional arrays Jacob Saaby Nielsen PowerShell 6 11-27-2007 10:44 AM
Working with arrays Jobbsy PowerShell 6 11-19-2007 05:15 AM
question about arrays Nicola Attico PowerShell 3 08-26-2006 04:41 PM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com 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 2005-2008