COMBOBOX Collection Items that store variables

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 11 years and 3 weeks 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
David.Bowman
Posts: 3
Last visit: Tue Jul 28, 2015 1:45 pm

COMBOBOX Collection Items that store variables

Post by David.Bowman »

Hello All,

I am creating a Form that will be used to perform Network Adapter settings on newly imaged computers in my company. The form will be called at first startup and will perform the following.

Setting IP and Subnet Mask
Setting the Default Gateway
Setting the OU's DNS Primary and 3 other alternate DNS's
Renaming the Local Area Connection

I am using the CMDLETs package from Vexasoft and have been able to run selections in ISE successfully for each part of the script so far. Here is where I am hung up.

One of my controls in the form is a COMBOBOX with a collection of all of the geographic OU's in the company. All have different primary DNS on each geographic Domain Controller. I have created variables for each OU to each OU's DNS settings and am trying to set these variables as the SelectedItem. What I have not been able to do is associate the strings or arrays of DNS variables with the Collection Items. I am unsure which property should be used SelectedItem or SelectedIndex, or some other property and where they are placed in the script.

The line "$DNS = $comboBox_SelectSite.SelectedText"
may be in the wrong place or incorrectly syntaxed.

The line "Set-NetworkAdapterDNS "Local Area Connection" -DNS $DNS" will work as a "Run Selection" if ran as is, but needs to be associated with "SelectedItem" or "SelectedIndex" to use the form.

Any help here would be truly appriciated.
The following is the code I have so far.

PowerShell Code
Double-click the code block to select all.
#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 1/24/2013 4:59 PM
# Generated By: Admin
########################################################################

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$richTextBox_ConnectionName = New-Object System.Windows.Forms.RichTextBox
$richTextBox_DefaultGateway = New-Object System.Windows.Forms.RichTextBox
$richTextBox_SubnetMask = New-Object System.Windows.Forms.RichTextBox
$richTextBox_IPAddress = New-Object System.Windows.Forms.RichTextBox
$label_ConnectionName = New-Object System.Windows.Forms.Label
$comboBox_SelectSite = New-Object System.Windows.Forms.ComboBox
$label_SelectSite = New-Object System.Windows.Forms.Label
$label_DefaultGateway = New-Object System.Windows.Forms.Label
$label_SubnetMask = New-Object System.Windows.Forms.Label
$label_IPAddress = New-Object System.Windows.Forms.Label
$button_Commit = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

# DNS Setting for each OU
$NA01 = "10.0.0.1", "10.0.0.2", "10.0.0.3", "10.0.0.4"
$NA02 = "10.0.0.5", "10.0.0.2", "10.0.0.3", "10.0.0.4"
$NA03 = "10.0.0.6", "10.0.0.2", "10.0.0.3", "10.0.0.4"
$NA04 = "10.0.0.7", "10.0.0.2", "10.0.0.3", "10.0.0.4"
$NA05 = "10.0.0.8", "10.0.0.2", "10.0.0.3", "10.0.0.4"
$NA06 = "10.0.0.9", "10.0.0.2", "10.0.0.3", "10.0.0.4"
$NA07 = "10.0.0.10", "10.0.0.2", "10.0.0.3", "10.0.0.4"
$NA08 = "10.0.0.11", "10.0.0.2", "10.0.0.3", "10.0.0.4"
$NA09 = "10.0.0.12", "10.0.0.2", "10.0.0.3", "10.0.0.4"
$NA10 = "10.0.0.13", "10.0.0.2", "10.0.0.3", "10.0.0.4"

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$button_Commit_OnClick= 
{
#TODO: Place custom script here
 
 # Save the Text input to Variables
 $IPAddress = $textBox_IPAddress.Text
 $SubnetMask = $textBox_SubnetMask.Text
 $Gateway = $textBox_DefaultGateway.Text
 $DNS = $comboBox_SelectSite.SelectedItem

 # Apply Settings to the Network Adapters
 Set-NetworkAdapterIPAddress "Local Area Connection" -IPAddress $IPAddress
 Set-NetworkAdapterSubnetMask "Local Area Connection" -Gateway $SubnetMask
 Set-NetworkAdapterGateway "Local Area Connection" -Gateway $Gateway
 Set-NetworkAdapterDNS "Local Area Connection" -DNS $DNS
 Rename-NetworkAdapter -Name "Local Area Connection" -NewName Adapter1
 
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 432
$System_Drawing_Size.Width = 646
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Font = New-Object System.Drawing.Font("Arial",14,0,3,1)
$form1.Name = "form1"
$form1.Text = "Network Adapter Form"

$richTextBox_ConnectionName.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 487
$System_Drawing_Point.Y = 58
$richTextBox_ConnectionName.Location = $System_Drawing_Point
$richTextBox_ConnectionName.Name = "richTextBox_ConnectionName"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 27
$System_Drawing_Size.Width = 147
$richTextBox_ConnectionName.Size = $System_Drawing_Size
$richTextBox_ConnectionName.TabIndex = 19
$richTextBox_ConnectionName.Text = ""

$form1.Controls.Add($richTextBox_ConnectionName)

$richTextBox_DefaultGateway.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 155
$System_Drawing_Point.Y = 100
$richTextBox_DefaultGateway.Location = $System_Drawing_Point
$richTextBox_DefaultGateway.Name = "richTextBox_DefaultGateway"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 28
$System_Drawing_Size.Width = 174
$richTextBox_DefaultGateway.Size = $System_Drawing_Size
$richTextBox_DefaultGateway.TabIndex = 18
$richTextBox_DefaultGateway.Text = ""

$form1.Controls.Add($richTextBox_DefaultGateway)

$richTextBox_SubnetMask.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 155
$System_Drawing_Point.Y = 58
$richTextBox_SubnetMask.Location = $System_Drawing_Point
$richTextBox_SubnetMask.Name = "richTextBox_SubnetMask"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 27
$System_Drawing_Size.Width = 175
$richTextBox_SubnetMask.Size = $System_Drawing_Size
$richTextBox_SubnetMask.TabIndex = 17
$richTextBox_SubnetMask.Text = ""

$form1.Controls.Add($richTextBox_SubnetMask)

$richTextBox_IPAddress.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 154
$System_Drawing_Point.Y = 15
$richTextBox_IPAddress.Location = $System_Drawing_Point
$richTextBox_IPAddress.Name = "richTextBox_IPAddress"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 28
$System_Drawing_Size.Width = 176
$richTextBox_IPAddress.Size = $System_Drawing_Size
$richTextBox_IPAddress.TabIndex = 16
$richTextBox_IPAddress.Text = ""

$form1.Controls.Add($richTextBox_IPAddress)

$label_ConnectionName.DataBindings.DefaultDataSourceUpdateMode = 0
$label_ConnectionName.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 342
$System_Drawing_Point.Y = 56
$label_ConnectionName.Location = $System_Drawing_Point
$label_ConnectionName.Name = "label_ConnectionName"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 132
$label_ConnectionName.Size = $System_Drawing_Size
$label_ConnectionName.TabIndex = 13
$label_ConnectionName.Text = "Connection Name"
$label_ConnectionName.TextAlign = 32

$form1.Controls.Add($label_ConnectionName)

$comboBox_SelectSite.AllowDrop = $True
$comboBox_SelectSite.DataBindings.DefaultDataSourceUpdateMode = 0
$comboBox_SelectSite.FormattingEnabled = $True
$comboBox_SelectSite.Items.Add("OU01")|Out-Null
$comboBox_SelectSite.Items.Add("OU02")|Out-Null
$comboBox_SelectSite.Items.Add("OU03")|Out-Null
$comboBox_SelectSite.Items.Add("OU04")|Out-Null
$comboBox_SelectSite.Items.Add("OU05")|Out-Null
$comboBox_SelectSite.Items.Add("OU06")|Out-Null
$comboBox_SelectSite.Items.Add("OU07")|Out-Null
$comboBox_SelectSite.Items.Add("OU08")|Out-Null
$comboBox_SelectSite.Items.Add("OU09")|Out-Null
$comboBox_SelectSite.Items.Add("OU10")|Out-Null

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 485
$System_Drawing_Point.Y = 15
$comboBox_SelectSite.Location = $System_Drawing_Point
$comboBox_SelectSite.Name = "comboBox_SelectSite"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 150
$comboBox_SelectSite.Size = $System_Drawing_Size
$comboBox_SelectSite.TabIndex = 12

$form1.Controls.Add($comboBox_SelectSite)

$label_SelectSite.DataBindings.DefaultDataSourceUpdateMode = 0
$label_SelectSite.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 342
$System_Drawing_Point.Y = 15
$label_SelectSite.Location = $System_Drawing_Point
$label_SelectSite.Name = "label_SelectSite"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 132
$label_SelectSite.Size = $System_Drawing_Size
$label_SelectSite.TabIndex = 11
$label_SelectSite.Text = "Select Site"
$label_SelectSite.TextAlign = 32

$form1.Controls.Add($label_SelectSite)

$label_DefaultGateway.DataBindings.DefaultDataSourceUpdateMode = 0
$label_DefaultGateway.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 11
$System_Drawing_Point.Y = 99
$label_DefaultGateway.Location = $System_Drawing_Point
$label_DefaultGateway.Name = "label_DefaultGateway"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 132
$label_DefaultGateway.Size = $System_Drawing_Size
$label_DefaultGateway.TabIndex = 9
$label_DefaultGateway.Text = "Default Gateway"
$label_DefaultGateway.TextAlign = 32

$form1.Controls.Add($label_DefaultGateway)

$label_SubnetMask.DataBindings.DefaultDataSourceUpdateMode = 0
$label_SubnetMask.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 11
$System_Drawing_Point.Y = 56
$label_SubnetMask.Location = $System_Drawing_Point
$label_SubnetMask.Name = "label_SubnetMask"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 132
$label_SubnetMask.Size = $System_Drawing_Size
$label_SubnetMask.TabIndex = 8
$label_SubnetMask.Text = "Subnet Mask"
$label_SubnetMask.TextAlign = 32

$form1.Controls.Add($label_SubnetMask)

$label_IPAddress.DataBindings.DefaultDataSourceUpdateMode = 0
$label_IPAddress.Font = New-Object System.Drawing.Font("Arial",12,0,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 11
$System_Drawing_Point.Y = 14
$label_IPAddress.Location = $System_Drawing_Point
$label_IPAddress.Name = "label_IPAddress"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 132
$label_IPAddress.Size = $System_Drawing_Size
$label_IPAddress.TabIndex = 7
$label_IPAddress.Text = "IP Address"
$label_IPAddress.TextAlign = 32

$form1.Controls.Add($label_IPAddress)

$button_Commit.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)

$button_Commit.DataBindings.DefaultDataSourceUpdateMode = 0
$button_Commit.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",14,1,3,1)
$button_Commit.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 270
$System_Drawing_Point.Y = 369
$button_Commit.Location = $System_Drawing_Point
$button_Commit.Name = "button_Commit"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 51
$System_Drawing_Size.Width = 102
$button_Commit.Size = $System_Drawing_Size
$button_Commit.TabIndex = 0
$button_Commit.Text = "Commit"
$button_Commit.UseVisualStyleBackColor = $False
$button_Commit.add_Click($button_Commit_OnClick)

$form1.Controls.Add($button_Commit)

#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
Attachments
Setup_Adapter.txt
File is a PS1. Just change extension
(11.6 KiB) Downloaded 419 times
Last edited by David.Bowman on Sat Mar 23, 2013 7:27 am, edited 2 times in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: COMBOBOX Collection Items that store variables

Post by jvierra »

Hi
Not sure what you are asking and the code is too hard to copy from the screen. Can you attach it as a PFF file?

A combo box can store an object.
User avatar
David.Bowman
Posts: 3
Last visit: Tue Jul 28, 2015 1:45 pm

Re: COMBOBOX Collection Items that store variables

Post by David.Bowman »

jvierra ,

In a nutshell, I need each Item in the Collection of the COMBOBOX to represent the array of DNS IP's corelating to the OU represented, in variable format, to send to the Vexasoft CMDLET:

Set-NetworkAdapterDNS "Local Area Connection" -DNS $somevar

Item OU01 needs to be represented in the COMBOBOX by the variable $OU01, Item OU02 to $OU02 and so on. Then it needs to be piped/passed to Set-NetworkAdapterDNS.

Dave
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: COMBOBOX Collection Items that store variables

Post by jvierra »

Store your $OU objects into the ComboBox as items then add a formatter to display the property you want in the list.
PowerShell Code
Double-click the code block to select all.
$combobox1_Format=[System.Windows.Forms.ListControlConvertEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.ListControlConvertEventArgs]
	#TODO: Place custom script here
	$_.Value=$_.ListItem.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 Collection Items that store variables

Post by jvierra »

Here is a full demo of how to use a COmboBox to work with objects

See attached project file. Remove extra 'txt' extension and open with PowerShell Studio.
Attachments
Demo-ComboFormat.pff.txt
Demo of ComboBox with objects in list
(9.34 KiB) Downloaded 592 times
User avatar
David.Bowman
Posts: 3
Last visit: Tue Jul 28, 2015 1:45 pm

Re: COMBOBOX Collection Items that store variables

Post by David.Bowman »

Still lost. I am very new to Winforms.
Lets take it one step at a time.

Help me to associate the OU01 item with the variable $OU01.

Dave
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: COMBOBOX Collection Items that store variables

Post by jvierra »

Just add them:
$comboBox_SelectSite.Items.Add($ou1)|Out-Null

I posted a complete demo above (file attachment). Download and run it. It shows you exactly how to do it.
This topic is 11 years and 3 weeks 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