retrieve the value of a combobox (AD)

Hello !

I have an active directory like this :

-A

--A1

--A2

---A2.1

...

I made a GUI with primal forms :


 

#Generated Form Function
import-module activedirectory
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 05/09/2015 13:48
# Generated By: Jeremy
########################################################################

#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
$Label_ville = New-Object System.Windows.Forms.Label
$comboBox2_ville = New-Object System.Windows.Forms.ComboBox
$comboBox1_BU = New-Object System.Windows.Forms.ComboBox
$Label_BU = New-Object System.Windows.Forms.Label
$textBox_prenom = New-Object System.Windows.Forms.TextBox
$Label_Prenom = New-Object System.Windows.Forms.Label
$textBox_name = New-Object System.Windows.Forms.TextBox
$Label_Nom = New-Object System.Windows.Forms.Label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$handler_label4_Click= 
{
#TODO: Place custom script here

}

$handler_label2_Click= 
{
#TODO: Place custom script here

}

$handler_comboBox1_SelectedIndexChanged= 
{
#TODO: Place custom script here

}

$handler_label1_Click= 
{
#TODO: Place custom script here

}

$handler_form1_Load= 
{
#TODO: Place custom script here

}

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

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 207
$System_Drawing_Size.Width = 387
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Primal Form"
$form1.add_Load($handler_form1_Load)
function Load-ComboBox 
{
    Param (
        [Parameter(Mandatory=$true)]
        [System.Windows.Forms.ComboBox]$ComboBox,
        [Parameter(Mandatory=$true)]
        $Items,
        [Parameter(Mandatory=$false)]
        [string]$DisplayMember,
        [switch]$Append
    )
    
    if(-not $Append)
    {
        $comboBox.Items.Clear()    
    }
    
    if($Items -is [Array])
    {
        $comboBox.Items.AddRange($Items)
    }
    else
    {
        $comboBox.Items.Add($Items)    
    }

    $comboBox.DisplayMember = $DisplayMember    
}

$Label_ville.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 109
$Label_ville.Location = $System_Drawing_Point
$Label_ville.Name = "Label_ville"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 55
$Label_ville.Size = $System_Drawing_Size
$Label_ville.TabIndex = 7
$Label_ville.Text = "Ville"
$Label_ville.add_Click($handler_label4_Click)

$form1.Controls.Add($Label_ville)

$comboBox2_ville.DataBindings.DefaultDataSourceUpdateMode = 0
$comboBox2_ville.DropDownStyle = 2
$comboBox2_ville.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 73
$System_Drawing_Point.Y = 109
$comboBox2_ville.Location = $System_Drawing_Point
$comboBox2_ville.Name = "comboBox2_ville"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 134
$comboBox2_ville.Size = $System_Drawing_Size
$comboBox2_ville.TabIndex = 6
Load-ComboBox $Combobox2_Ville (Get-ADOrganizationalUnit -SearchBase "OU=$OU,DC=SODIAAL,DC=FR" -Searchscope 1 -Filter * ) -DisplayMember "Name"

$form1.Controls.Add($comboBox2_ville)

$comboBox1_BU.DataBindings.DefaultDataSourceUpdateMode = 0
$comboBox1_BU.DropDownStyle = 2
$comboBox1_BU.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 73
$System_Drawing_Point.Y = 80
$comboBox1_BU.Location = $System_Drawing_Point
$comboBox1_BU.Name = "comboBox1_BU"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 134
$comboBox1_BU.Size = $System_Drawing_Size
$comboBox1_BU.TabIndex = 5
$comboBox1_BU.add_SelectedIndexChanged($handler_comboBox1_SelectedIndexChanged)
Load-ComboBox $Combobox1_BU (Get-ADOrganizationalUnit -SearchBase "DC=SODIAAL,DC=FR" -Searchscope 1 -Filter * ) -DisplayMember "Name"
$Combobox1_BU.SelectedItem = $comboBox1_BU.Items[0]
$OU = $comboBox1_BU.Items[0]

$form1.Controls.Add($comboBox1_BU)

$Label_BU.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 83
$Label_BU.Location = $System_Drawing_Point
$Label_BU.Name = "Label_BU"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 64
$Label_BU.Size = $System_Drawing_Size
$Label_BU.TabIndex = 4
$Label_BU.Text = "BU "

$form1.Controls.Add($Label_BU)

$textBox_prenom.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 73
$System_Drawing_Point.Y = 53
$textBox_prenom.Location = $System_Drawing_Point
$textBox_prenom.Name = "textBox_prenom"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 134
$textBox_prenom.Size = $System_Drawing_Size
$textBox_prenom.TabIndex = 3

$form1.Controls.Add($textBox_prenom)

$Label_Prenom.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 60
$Label_Prenom.Location = $System_Drawing_Point
$Label_Prenom.Name = "Label_Prenom"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 64
$Label_Prenom.Size = $System_Drawing_Size
$Label_Prenom.TabIndex = 2
$Label_Prenom.Text = "Prenom "
$Label_Prenom.add_Click($handler_label2_Click)

$form1.Controls.Add($Label_Prenom)

$textBox_name.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 73
$System_Drawing_Point.Y = 24
$textBox_name.Location = $System_Drawing_Point
$textBox_name.Name = "textBox_name"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 134
$textBox_name.Size = $System_Drawing_Size
$textBox_name.TabIndex = 1

$form1.Controls.Add($textBox_name)

$Label_Nom.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 27
$Label_Nom.Location = $System_Drawing_Point
$Label_Nom.Name = "Label_Nom"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 82
$Label_Nom.Size = $System_Drawing_Size
$Label_Nom.TabIndex = 0
$Label_Nom.Text = "Nom"
$Label_Nom.add_Click($handler_label1_Click)

$form1.Controls.Add($Label_Nom)

#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

 

error :

Get-ADOrganizationalUnit : the syntax of the object name is incorrect

Combobox2_ville (Get-ADOrganizationalUnit <<<< -SearchBase "OU=$OU, DC=CONTOSO,DC=FR" - Searchscope 1 -filter * ) -DisplayMember "Name"

I can not seem to have the value of the second combobox

If I put the value manually it works

Can you help me ?

Thank you :)

 


September 5th, 2015 1:58pm

$ous=Get-ADOrganizationalUnit -Searchscope 1 -Filter *
Load-ComboBox $Combobox1_BU $ous  -DisplayMember Name

Free Windows Admin Tool Kit Click here and download it now
September 5th, 2015 2:19pm

You must load the comboboxes in the Load event of the form or in an event.  LOading outside oof an event will produce unreliable results.

September 5th, 2015 2:25pm

Here is a better way to load a combobox.  I have fixed many errors in your code but not all. It is also not possible to figure out what youa re doing as you have just sprinkled odd bits of code into code that was generated by a tool.

This works as far as loading comboboxes:

# ==============================================================================================
# 
# Windows PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2015
# 
# NAME: 
# 
# AUTHOR: Windows User , 
# DATE  : 9/5/2015
# 
# COMMENT: 
# 
# ==============================================================================================

#Generated Form Function
import-module activedirectory
function GenerateForm {
	########################################################################
	# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
	# Generated On: 05/09/2015 13:48
	# Generated By: Jeremy
	########################################################################
	
	#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
	$Label_ville = New-Object System.Windows.Forms.Label
	$comboBox2_ville = New-Object System.Windows.Forms.ComboBox
	$comboBox1_BU = New-Object System.Windows.Forms.ComboBox
	$Label_BU = New-Object System.Windows.Forms.Label
	$textBox_prenom = New-Object System.Windows.Forms.TextBox
	$Label_Prenom = New-Object System.Windows.Forms.Label
	$textBox_name = New-Object System.Windows.Forms.TextBox
	$Label_Nom = New-Object System.Windows.Forms.Label
	$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
	#endregion Generated Form Objects
	
	#----------------------------------------------
	#Generated Event Script Blocks
	#----------------------------------------------
	#Provide Custom Code for events specified in PrimalForms.
	$handler_form1_Load= {
		$comboBox2_ville.DataSource=[collections.arraylist](Get-ADOrganizationalUnit -Searchscope 1 -Filter *)
		$comboBox2_ville.DisplayMember='Name'
		$Combobox1_BU.DataSource=[collections.arraylist](Get-ADOrganizationalUnit -Searchscope 1 -Filter * )
		$Combobox1_BU.DisplayMember='Name'	
	}
	
	$OnLoadForm_StateCorrection=
	{#Correct the initial state of the form to prevent the .Net maximized form issue
		$form1.WindowState = $InitialFormWindowState
	}
	
	#----------------------------------------------
	#region Generated Form Code
	$System_Drawing_Size = New-Object System.Drawing.Size
	$System_Drawing_Size.Height = 207
	$System_Drawing_Size.Width = 387
	$form1.ClientSize = $System_Drawing_Size
	$form1.DataBindings.DefaultDataSourceUpdateMode = 0
	$form1.Name = "form1"
	$form1.Text = "Primal Form"
	$form1.add_Load($handler_form1_Load)
	
	
	$System_Drawing_Point = New-Object System.Drawing.Point
	$System_Drawing_Point.X = 12
	$System_Drawing_Point.Y = 109
	$Label_ville.Location = $System_Drawing_Point
	$Label_ville.Name = "Label_ville"
	$System_Drawing_Size = New-Object System.Drawing.Size
	$System_Drawing_Size.Height = 23
	$System_Drawing_Size.Width = 55
	$Label_ville.Size = $System_Drawing_Size
	$Label_ville.TabIndex = 7
	$Label_ville.Text = "Ville"
	$Label_ville.add_Click($handler_label4_Click)
	
	$form1.Controls.Add($Label_ville)
	
	$comboBox2_ville.DataBindings.DefaultDataSourceUpdateMode = 0
	$comboBox2_ville.DropDownStyle = 2
	$comboBox2_ville.FormattingEnabled = $True
	$System_Drawing_Point = New-Object System.Drawing.Point
	$System_Drawing_Point.X = 73
	$System_Drawing_Point.Y = 109
	$comboBox2_ville.Location = $System_Drawing_Point
	$comboBox2_ville.Name = "comboBox2_ville"
	$System_Drawing_Size = New-Object System.Drawing.Size
	$System_Drawing_Size.Height = 21
	$System_Drawing_Size.Width = 134
	$comboBox2_ville.Size = $System_Drawing_Size
	$comboBox2_ville.TabIndex = 6
	$form1.Controls.Add($comboBox2_ville)
	
	$comboBox1_BU.DataBindings.DefaultDataSourceUpdateMode = 0
	$comboBox1_BU.DropDownStyle = 2
	$comboBox1_BU.FormattingEnabled = $True
	$System_Drawing_Point = New-Object System.Drawing.Point
	$System_Drawing_Point.X = 73
	$System_Drawing_Point.Y = 80
	$comboBox1_BU.Location = $System_Drawing_Point
	$comboBox1_BU.Name = "comboBox1_BU"
	$System_Drawing_Size = New-Object System.Drawing.Size
	$System_Drawing_Size.Height = 21
	$System_Drawing_Size.Width = 134
	$comboBox1_BU.Size = $System_Drawing_Size
	$comboBox1_BU.TabIndex = 5
	$comboBox1_BU.add_SelectedIndexChanged($handler_comboBox1_SelectedIndexChanged)
	$Combobox1_BU.SelectedItem = $comboBox1_BU.Items[0]
	$OU = $comboBox1_BU.Items[0]
	
	$form1.Controls.Add($comboBox1_BU)
		
	$System_Drawing_Point = New-Object System.Drawing.Point
	$System_Drawing_Point.X = 13
	$System_Drawing_Point.Y = 83
	$Label_BU.Location = $System_Drawing_Point
	$Label_BU.Name = "Label_BU"
	$System_Drawing_Size = New-Object System.Drawing.Size
	$System_Drawing_Size.Height = 23
	$System_Drawing_Size.Width = 64
	$Label_BU.Size = $System_Drawing_Size
	$Label_BU.TabIndex = 4
	$Label_BU.Text = "BU "
	
	$form1.Controls.Add($Label_BU)
	
	$textBox_prenom.DataBindings.DefaultDataSourceUpdateMode = 0
	$System_Drawing_Point = New-Object System.Drawing.Point
	$System_Drawing_Point.X = 73
	$System_Drawing_Point.Y = 53
	$textBox_prenom.Location = $System_Drawing_Point
	$textBox_prenom.Name = "textBox_prenom"
	$System_Drawing_Size = New-Object System.Drawing.Size
	$System_Drawing_Size.Height = 20
	$System_Drawing_Size.Width = 134
	$textBox_prenom.Size = $System_Drawing_Size
	$textBox_prenom.TabIndex = 3
	
	$form1.Controls.Add($textBox_prenom)
		
	$System_Drawing_Point = New-Object System.Drawing.Point
	$System_Drawing_Point.X = 13
	$System_Drawing_Point.Y = 60
	$Label_Prenom.Location = $System_Drawing_Point
	$Label_Prenom.Name = "Label_Prenom"
	$System_Drawing_Size = New-Object System.Drawing.Size
	$System_Drawing_Size.Height = 23
	$System_Drawing_Size.Width = 64
	$Label_Prenom.Size = $System_Drawing_Size
	$Label_Prenom.TabIndex = 2
	$Label_Prenom.Text = "Prenom "
	$Label_Prenom.add_Click($handler_label2_Click)
	
	$form1.Controls.Add($Label_Prenom)
	
	$textBox_name.DataBindings.DefaultDataSourceUpdateMode = 0
	$System_Drawing_Point = New-Object System.Drawing.Point
	$System_Drawing_Point.X = 73
	$System_Drawing_Point.Y = 24
	$textBox_name.Location = $System_Drawing_Point
	$textBox_name.Name = "textBox_name"
	$System_Drawing_Size = New-Object System.Drawing.Size
	$System_Drawing_Size.Height = 20
	$System_Drawing_Size.Width = 134
	$textBox_name.Size = $System_Drawing_Size
	$textBox_name.TabIndex = 1
	
	$form1.Controls.Add($textBox_name)
	
	$Label_Nom.DataBindings.DefaultDataSourceUpdateMode = 0
	
	$System_Drawing_Point = New-Object System.Drawing.Point
	$System_Drawing_Point.X = 13
	$System_Drawing_Point.Y = 27
	$Label_Nom.Location = $System_Drawing_Point
	$Label_Nom.Name = "Label_Nom"
	$System_Drawing_Size = New-Object System.Drawing.Size
	$System_Drawing_Size.Height = 23
	$System_Drawing_Size.Width = 82
	$Label_Nom.Size = $System_Drawing_Size
	$Label_Nom.TabIndex = 0
	$Label_Nom.Text = "Nom"
	$Label_Nom.add_Click($handler_label1_Click)
	
	$form1.Controls.Add($Label_Nom)
	
	#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

Free Windows Admin Tool Kit Click here and download it now
September 5th, 2015 2:41pm

It doesn't work

I don't have the value of the first combobox

Thanks :)

September 5th, 2015 3:46pm

I have no idea what you are talking about.  Your issue was that you couldn't get the comobo boxes to load.  Now you are asking a completely different question.
Free Windows Admin Tool Kit Click here and download it now
September 5th, 2015 3:52pm

Change this line: $form1.ShowDialog()| Out-Null

to the following:

$form1.ShowDialog()
$form1

Then call the form like this:

$form=GenerateForm

Now you have all of the values:

$form.Controls['comboBox1_BU'].SelectedItem.Name


September 5th, 2015 4:06pm

Example AD

A

-A1

-A2

B

-B1

...

When i selected A in the first combobox

I want it puts me in the UO A

In the second combobox there will be A1 - A2

You understand ?

I'm french, sorry for my english ^^

Free Windows Admin Tool Kit Click here and download it now
September 5th, 2015 4:08pm

That has nothing to do with your original question.

When the combobox changes value that event is used to load the data into other controls. We use the "SelectionChanged" Event.

Here is how to event the combobox.

$comboBox1_BU.add_SelectedIndexChanged({ ... code .. })

In that event load the second box.

September 5th, 2015 4:17pm

#Generated Form Function import-module activedirectory function GenerateForm { ######################################################################## # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0 # Generated On: 05/09/2015 13:48 # Generated By: Jeremy ######################################################################## #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 $Label_ville = New-Object System.Windows.Forms.Label $comboBox2_ville = New-Object System.Windows.Forms.ComboBox $comboBox1_BU = New-Object System.Windows.Forms.ComboBox $Label_BU = New-Object System.Windows.Forms.Label $textBox_prenom = New-Object System.Windows.Forms.TextBox $Label_Prenom = New-Object System.Windows.Forms.Label $textBox_name = New-Object System.Windows.Forms.TextBox $Label_Nom = New-Object System.Windows.Forms.Label $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState #endregion Generated Form Objects #---------------------------------------------- #Generated Event Script Blocks #---------------------------------------------- #Provide Custom Code for events specified in PrimalForms. $Combobox1_BU_SelectedIndexChanged={ Load-ComboBox $Combobox2_Ville (Get-ADOrganizationalUnit -searchbase "OU=$UO,DC=CONTOSO,DC=FR" -Searchscope 1 -Filter * ) -DisplayMember "Name" } $handler_label4_Click= { #TODO: Place custom script here } $handler_label2_Click= { #TODO: Place custom script here } $handler_comboBox1_SelectedIndexChanged= { #TODO: Place custom script here } $handler_label1_Click= { #TODO: Place custom script here } $handler_form1_Load= { #TODO: Place custom script here } $OnLoadForm_StateCorrection= {#Correct the initial state of the form to prevent the .Net maximized form issue $form1.WindowState = $InitialFormWindowState } #---------------------------------------------- #region Generated Form Code $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 207 $System_Drawing_Size.Width = 387 $form1.ClientSize = $System_Drawing_Size $form1.DataBindings.DefaultDataSourceUpdateMode = 0 $form1.Name = "form1" $form1.Text = "Primal Form" $form1.add_Load($handler_form1_Load) function Load-ComboBox { Param ( [Parameter(Mandatory=$true)] [System.Windows.Forms.ComboBox]$ComboBox, [Parameter(Mandatory=$true)] $Items, [Parameter(Mandatory=$false)] [string]$DisplayMember, [switch]$Append ) if(-not $Append) { $comboBox.Items.Clear() } if($Items -is [Array]) { $comboBox.Items.AddRange($Items) } else { $comboBox.Items.Add($Items) } $comboBox.DisplayMember = $DisplayMember } $Label_ville.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 12 $System_Drawing_Point.Y = 109 $Label_ville.Location = $System_Drawing_Point $Label_ville.Name = "Label_ville" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 23 $System_Drawing_Size.Width = 55 $Label_ville.Size = $System_Drawing_Size $Label_ville.TabIndex = 7 $Label_ville.Text = "Ville" $Label_ville.add_Click($handler_label4_Click) $form1.Controls.Add($Label_ville) $comboBox2_ville.DataBindings.DefaultDataSourceUpdateMode = 0 $comboBox2_ville.DropDownStyle = 2 $comboBox2_ville.FormattingEnabled = $True $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 73 $System_Drawing_Point.Y = 109 $comboBox2_ville.Location = $System_Drawing_Point $comboBox2_ville.Name = "comboBox2_ville" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 21 $System_Drawing_Size.Width = 134 $comboBox2_ville.Size = $System_Drawing_Size $comboBox2_ville.TabIndex = 6 Load-ComboBox $Combobox2_Ville (Get-ADOrganizationalUnit -searchbase "OU=$UO,DC=CONTOSO,DC=FR" -Searchscope 1 -Filter * ) -DisplayMember "Name" $comboBox1_BU.add_SelectedIndexChanged($comboBox1_BU_SelectedIndexChanged) $form1.Controls.Add($comboBox2_ville) $comboBox1_BU.DataBindings.DefaultDataSourceUpdateMode = 0 $comboBox1_BU.DropDownStyle = 2 $comboBox1_BU.FormattingEnabled = $True $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 73 $System_Drawing_Point.Y = 80 $comboBox1_BU.Location = $System_Drawing_Point $comboBox1_BU.Name = "comboBox1_BU" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 21 $System_Drawing_Size.Width = 134 $comboBox1_BU.Size = $System_Drawing_Size $comboBox1_BU.TabIndex = 5 Load-ComboBox $Combobox1_BU (Get-ADOrganizationalUnit -Searchscope 1 -Filter * ) -DisplayMember "Name" $Combobox1_BU.SelectedItem = $comboBox1_BU.Items[0]

$UO = $combobox1_BU.selectemItem $form1.Controls.Add($comboBox1_BU) $Label_BU.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 13 $System_Drawing_Point.Y = 83 $Label_BU.Location = $System_Drawing_Point $Label_BU.Name = "Label_BU" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 23 $System_Drawing_Size.Width = 64 $Label_BU.Size = $System_Drawing_Size $Label_BU.TabIndex = 4 $Label_BU.Text = "BU " $form1.Controls.Add($Label_BU) $textBox_prenom.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 73 $System_Drawing_Point.Y = 53 $textBox_prenom.Location = $System_Drawing_Point $textBox_prenom.Name = "textBox_prenom" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 20 $System_Drawing_Size.Width = 134 $textBox_prenom.Size = $System_Drawing_Size $textBox_prenom.TabIndex = 3 $form1.Controls.Add($textBox_prenom) $Label_Prenom.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 13 $System_Drawing_Point.Y = 60 $Label_Prenom.Location = $System_Drawing_Point $Label_Prenom.Name = "Label_Prenom" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 23 $System_Drawing_Size.Width = 64 $Label_Prenom.Size = $System_Drawing_Size $Label_Prenom.TabIndex = 2 $Label_Prenom.Text = "Prenom " $Label_Prenom.add_Click($handler_label2_Click) $form1.Controls.Add($Label_Prenom) $textBox_name.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 73 $System_Drawing_Point.Y = 24 $textBox_name.Location = $System_Drawing_Point $textBox_name.Name = "textBox_name" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 20 $System_Drawing_Size.Width = 134 $textBox_name.Size = $System_Drawing_Size $textBox_name.TabIndex = 1 $form1.Controls.Add($textBox_name) $Label_Nom.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 13 $System_Drawing_Point.Y = 27 $Label_Nom.Location = $System_Drawing_Point $Label_Nom.Name = "Label_Nom" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 23 $System_Drawing_Size.Width = 82 $Label_Nom.Size = $System_Drawing_Size $Label_Nom.TabIndex = 0 $Label_Nom.Text = "Nom" $Label_Nom.add_Click($handler_label1_Click) $form1.Controls.Add($Label_Nom) #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

I advance

Now, when i chose BU in the list, i get an error for each value selected

How to recover the value of the first combobox ?

For recover value :
$UO = $Combobox1_BU.Selecteditem

if i do :
Load-ComboBox $Combobox2_Ville (Get-ADOrganizationalUnit -searchbase "OU=$UO,DC=SODIAAL,DC=FR" -Searchscope 1 -Filter * ) -DisplayMember "Name"

error : Syntax incorrect

Thanks


Free Windows Admin Tool Kit Click here and download it now
September 5th, 2015 5:19pm

Sorry. You keep changing the question and what yo are doing. I suggest you start by learning PowerShell before trying to understand how to use PowerShell with forms.

If you don't understand the posts and why your code is badly broken then  I cannot help you.

September 5th, 2015 5:25pm

My last offer. Here is a complete and working demo of how to use ComboBoxes.

#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-Demo-ComboRelation_pff {
	
	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	#endregion Import Assemblies
	
	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$formComboDemoWMIBrowser = New-Object 'System.Windows.Forms.Form'
	$combobox3 = New-Object 'System.Windows.Forms.ComboBox'
	$textbox1 = New-Object 'System.Windows.Forms.TextBox'
	$combobox2 = New-Object 'System.Windows.Forms.ComboBox'
	$combobox1 = New-Object 'System.Windows.Forms.ComboBox'
	$buttonOK = New-Object 'System.Windows.Forms.Button'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects
	
	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	
	
	$FormEvent_Load = {
		$p = Get-WmiObject -List Win32_* | sort Name
		Load-ComboBox $combobox3 -Items $p -DisplayMember Name
	}
	
	#region Control Helper Functions
	function Load-ComboBox {
	<#
		.SYNOPSIS
			This functions helps you load items into a ComboBox.
	
		.DESCRIPTION
			Use this function to dynamically load items into the ComboBox control.
	
		.PARAMETER  ComboBox
			The ComboBox control you want to add items to.
	
		.PARAMETER  Items
			The object or objects you wish to load into the ComboBox's Items collection.
	
		.PARAMETER  DisplayMember
			Indicates the property to display for the items in this control.
		
		.PARAMETER  Append
			Adds the item(s) to the ComboBox without clearing the Items collection.
		
		.EXAMPLE
			Load-ComboBox $combobox1 "Red", "White", "Blue"
		
		.EXAMPLE
			Load-ComboBox $combobox1 "Red" -Append
			Load-ComboBox $combobox1 "White" -Append
			Load-ComboBox $combobox1 "Blue" -Append
		
		.EXAMPLE
			Load-ComboBox $combobox1 (Get-Process) "ProcessName"
	#>
		Param (
			[ValidateNotNull()]
			[Parameter(Mandatory = $true)]
			[System.Windows.Forms.ComboBox]$ComboBox,
			[ValidateNotNull()]
			[Parameter(Mandatory = $true)]
			$Items,
			[Parameter(Mandatory = $false)]
			[string]$DisplayMember,
			[switch]$Append
		)
		
		if (-not $Append) {
			$ComboBox.Items.Clear()
		}
		
		if ($Items -is [Object[]]) {
			$ComboBox.Items.AddRange($Items)
		} elseif ($Items -is [Array]) {
			$ComboBox.BeginUpdate()
			foreach ($obj in $Items) {
				$ComboBox.Items.Add($obj)
			}
			$ComboBox.EndUpdate()
		} else {
			$ComboBox.Items.Add($Items)
		}
		
		$ComboBox.DisplayMember = $DisplayMember
	}
	#endregion
	
	$combobox1_SelectedIndexChanged = {
		$items = ($combobox1.SelectedItem).GetRelated() | select __class
		Load-ComboBox $combobox2 -Items $items -DisplayMember '__CLASS'
	}
	
	$combobox2_SelectedIndexChanged = {
		$textbox1.Text = $combobox1.SelectedItem.GetRelated($combobox2.SelectedItem.'__CLASS') |
		Format-List | Out-String
	}
	
	$combobox3_SelectedIndexChanged = {
		$p = Get-WmiObject "$($combobox3.SelectedItem.Name)"
		Load-ComboBox $combobox1 -Items $p -DisplayMember Caption
	}
	
	# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load =
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$formComboDemoWMIBrowser.WindowState = $InitialFormWindowState
	}
	
	$Form_Cleanup_FormClosed =
	{
		#Remove all event handlers from the controls
		try {
			$combobox3.remove_SelectedIndexChanged($combobox3_SelectedIndexChanged)
			$combobox2.remove_SelectedIndexChanged($combobox2_SelectedIndexChanged)
			$combobox1.remove_SelectedIndexChanged($combobox1_SelectedIndexChanged)
			$formComboDemoWMIBrowser.remove_Load($FormEvent_Load)
			$formComboDemoWMIBrowser.remove_Load($Form_StateCorrection_Load)
			$formComboDemoWMIBrowser.remove_FormClosed($Form_Cleanup_FormClosed)
		} catch [Exception]
		{ }
	}
	#endregion Generated Events
	
	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	$formComboDemoWMIBrowser.SuspendLayout()
	#
	# formComboDemoWMIBrowser
	#
	$formComboDemoWMIBrowser.Controls.Add($combobox3)
	$formComboDemoWMIBrowser.Controls.Add($textbox1)
	$formComboDemoWMIBrowser.Controls.Add($combobox2)
	$formComboDemoWMIBrowser.Controls.Add($combobox1)
	$formComboDemoWMIBrowser.Controls.Add($buttonOK)
	$formComboDemoWMIBrowser.AcceptButton = $buttonOK
	$formComboDemoWMIBrowser.ClientSize = '795, 503'
	$formComboDemoWMIBrowser.FormBorderStyle = 'FixedDialog'
	$formComboDemoWMIBrowser.MaximizeBox = $False
	$formComboDemoWMIBrowser.MinimizeBox = $False
	$formComboDemoWMIBrowser.Name = "formComboDemoWMIBrowser"
	$formComboDemoWMIBrowser.StartPosition = 'CenterScreen'
	$formComboDemoWMIBrowser.Text = "Combo Demo WMI Browser"
	$formComboDemoWMIBrowser.add_Load($FormEvent_Load)
	#
	# combobox3
	#
	$combobox3.FormattingEnabled = $True
	$combobox3.Location = '36, 13'
	$combobox3.Name = "combobox3"
	$combobox3.Size = '435, 21'
	$combobox3.TabIndex = 4
	$combobox3.Text = "Win32_Printer"
	$combobox3.add_SelectedIndexChanged($combobox3_SelectedIndexChanged)
	#
	# textbox1
	#
	$textbox1.Location = '36, 75'
	$textbox1.Multiline = $True
	$textbox1.Name = "textbox1"
	$textbox1.ScrollBars = 'Both'
	$textbox1.Size = '638, 403'
	$textbox1.TabIndex = 3
	#
	# combobox2
	#
	$combobox2.FormattingEnabled = $True
	$combobox2.Location = '368, 48'
	$combobox2.Name = "combobox2"
	$combobox2.Size = '306, 21'
	$combobox2.TabIndex = 2
	$combobox2.add_SelectedIndexChanged($combobox2_SelectedIndexChanged)
	#
	# combobox1
	#
	$combobox1.FormattingEnabled = $True
	$combobox1.Location = '36, 48'
	$combobox1.Name = "combobox1"
	$combobox1.Size = '298, 21'
	$combobox1.TabIndex = 1
	$combobox1.add_SelectedIndexChanged($combobox1_SelectedIndexChanged)
	#
	# buttonOK
	#
	$buttonOK.Anchor = 'Bottom, Right'
	$buttonOK.DialogResult = 'OK'
	$buttonOK.Location = '697, 75'
	$buttonOK.Name = "buttonOK"
	$buttonOK.Size = '75, 23'
	$buttonOK.TabIndex = 0
	$buttonOK.Text = "OK"
	$buttonOK.UseVisualStyleBackColor = $True
	$formComboDemoWMIBrowser.ResumeLayout()
	#endregion Generated Form Code
	
	#----------------------------------------------
	
	#Save the initial state of the form
	$InitialFormWindowState = $formComboDemoWMIBrowser.WindowState
	#Init the OnLoad event to correct the initial state of the form
	$formComboDemoWMIBrowser.add_Load($Form_StateCorrection_Load)
	#Clean up the control events
	$formComboDemoWMIBrowser.add_FormClosed($Form_Cleanup_FormClosed)
	#Show the Form
	return $formComboDemoWMIBrowser.ShowDialog()
	
} #End Function

Call-Demo-ComboRelation_pff

Free Windows Admin Tool Kit Click here and download it now
September 5th, 2015 5:57pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics