# Loading ComboBox

**URL:** <https://forums.powershell.org/t/loading-combobox/6754>\
**Category:** PowerShell Help\
**Created:** [July 5, 2016, 6:30am UTC](https://forums.powershell.org/t/loading-combobox/6754 "2016-07-05T06:30:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amit-aman1](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@amit-aman1](https://forums.powershell.org/u/amit-aman1)\
**Post date:** [July 5, 2016, 6:30am UTC](https://forums.powershell.org/t/loading-combobox/6754/1 "2016-07-05T06:30:27Z")

</div>

Hi Team,

I am trying to populate the value of 2nd combobox as per the value selected in 1st combobox. Not able to do that.  
Kindly assist. Following is my code;

[reflection.assembly]::loadwithpartialname(“System.Windows.Forms”) | Out-Null  
[reflection.assembly]::loadwithpartialname(“System.Drawing”) | Out-Null

$form1 = New-Object System.Windows.Forms.Form  
$button1 = New-Object System.Windows.Forms.Button  
$button1 = New-Object System.Windows.Forms.Button  
$TextBox1 = New-Object System.Windows.Forms.TextBox  
$TextBox2 = New-Object System.Windows.Forms.TextBox  
$comboBox1 = New-Object System.Windows.Forms.ComboBox  
$comboBox2 = New-Object System.Windows.Forms.ComboBox  
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

function fill\_dclist  
{  
$comboBox1.Items.Clear()  
$dclist = Invoke-Sqlcmd -Query “select distinct \* from master” -ServerInstance dc2012\sqlexpress -Database final  
$dclist = $dclist.vertical | select -Unique  
foreach ($dc in $dclist)  
{  
$comboBox1.Items.add($dc.toString())  
}  
}

function fill\_dclist1  
{  
if ($comboBox1.SelectedIndex -ge -1)  
{

```
$ClientSelected = ($combobox1.Items)

```

$dclist1 = Invoke-Sqlcmd -Query “SELECT [Cust Name] FROM master WHERE [vertical]=‘$ClientSelected’” -ServerInstance dc2012\sqlexpress -Database final  
$dclist1 = $dclist1 | select -Unique

foreach ($dc1 in $dclist1)  
{  
$comboBox2.Items.add($dc1.ToString())  
}

}  
}

$handler\_comboBox1\_SelectedIndexChanged=  
{  
try{  
if ($comboBox1.Text.Length -gt 0)  
{  
$form1.Text = $comboBox1.Text;  
}  
}catch{}  
}  
$handler\_comboBox2\_SelectedIndexChanged=  
{  
try{  
if ($comboBox2.Text.Length -gt 0)  
{  
$form1.Text = $comboBox2.Text;  
}  
}catch{}  
}  
$handler\_button1\_Click=  
{

```
try{
if ($comboBox1.Text.Length -gt 0)
{
    $comboBox1.Items.Add($comboBox1.Text);
    $comboBox2.Items.Add($comboBox2.Text);
    $a = $comboBox1.Text;
    $b = $comboBox2.Text;
    Invoke-Sqlcmd -Query "SELECT * FROM master WHERE [vertical]='$a' AND [Cust Name]='$b'" -ServerInstance dc2012\sqlexpress -Database final | Out-GridView;
}
}catch{} 

```

}  
$handler\_button2\_Click=  
{  
try{  
$comboBox1.Items.Clear();  
$comboBox1.Text = “”;  
$comboBox2.Items.Clear();  
$comboBox2.Text = “”;  
}catch{}  
}  
$OnLoadForm\_StateCorrection=  
{  
$form1.WindowState = $InitialFormWindowState  
}

$form1.Text = “DATABASE INFORMATION”  
$form1.Name = “form1”  
$form1.DataBindings.DefaultDataSourceUpdateMode = 0  
$System\_Drawing\_Size = New-Object System.Drawing.Size  
$System\_Drawing\_Size.Width = 380  
$System\_Drawing\_Size.Height = 210  
$form1.ClientSize = $System\_Drawing\_Size

$TextBox1.Name = “TextBox1”  
$System\_Drawing\_Size = New-Object System.Drawing.Size  
$System\_Drawing\_Size.Width = 150  
$System\_Drawing\_Size.Height = 30  
$TextBox1.Size = $System\_Drawing\_Size  
$TextBox1.Text = “Select the VERTICAL”  
$System\_Drawing\_Point = New-Object System.Drawing.Point  
$System\_Drawing\_Point.X = 10  
$System\_Drawing\_Point.Y = 10  
$TextBox1.Location = $System\_Drawing\_Point  
$TextBox1.DataBindings.DefaultDataSourceUpdateMode = 0  
$form1.Controls.Add($TextBox1)

$TextBox2.Name = “TextBox2”  
$System\_Drawing\_Size = New-Object System.Drawing.Size  
$System\_Drawing\_Size.Width = 150  
$System\_Drawing\_Size.Height = 30  
$TextBox2.Size = $System\_Drawing\_Size  
$TextBox2.Text = “Select your CUSTOMER”  
$System\_Drawing\_Point = New-Object System.Drawing.Point  
$System\_Drawing\_Point.X = 10  
$System\_Drawing\_Point.Y = 90  
$TextBox2.Location = $System\_Drawing\_Point  
$TextBox2.DataBindings.DefaultDataSourceUpdateMode = 0  
$form1.Controls.Add($TextBox2)

$button1.TabIndex = 5  
$button1.Name = “button1”  
$System\_Drawing\_Size = New-Object System.Drawing.Size  
$System\_Drawing\_Size.Width = 70  
$System\_Drawing\_Size.Height = 30  
$button1.Size = $System\_Drawing\_Size  
$button1.UseVisualStyleBackColor = $True  
$button1.Text = “SUBMIT”  
$System\_Drawing\_Point = New-Object System.Drawing.Point  
$System\_Drawing\_Point.X = 240  
$System\_Drawing\_Point.Y = 170  
$button1.Location = $System\_Drawing\_Point  
$button1.DataBindings.DefaultDataSourceUpdateMode = 0  
$button1.add\_Click($handler\_button1\_Click)  
$form1.Controls.Add($button1)

$button1.TabIndex = 6  
$button2.Name = “button2”  
$System\_Drawing\_Size = New-Object System.Drawing.Size  
$System\_Drawing\_Size.Width = 70  
$System\_Drawing\_Size.Height = 30  
$button2.Size = $System\_Drawing\_Size  
$button2.UseVisualStyleBackColor = $True  
$button2.Text = “CLEAR”  
$System\_Drawing\_Point = New-Object System.Drawing.Point  
$System\_Drawing\_Point.X = 60  
$System\_Drawing\_Point.Y = 170  
$button2.Location = $System\_Drawing\_Point  
$button2.DataBindings.DefaultDataSourceUpdateMode = 0  
$button2.add\_Click($handler\_button2\_Click)  
$form1.Controls.Add($button2)

$comboBox1.FormattingEnabled = $True  
$System\_Drawing\_Size = New-Object System.Drawing.Size  
$System\_Drawing\_Size.Width = 250  
$System\_Drawing\_Size.Height = 21  
$comboBox1.Size = $System\_Drawing\_Size  
$comboBox1.DataBindings.DefaultDataSourceUpdateMode = 0  
$comboBox1.Name = “comboBox1”  
$System\_Drawing\_Point = New-Object System.Drawing.Point  
$System\_Drawing\_Point.X = 60  
$System\_Drawing\_Point.Y = 50  
$comboBox1.Location = $System\_Drawing\_Point  
$comboBox1.TabIndex = 0  
$comboBox1.add\_SelectedIndexChanged($handler\_comboBox1\_SelectedIndexChanged)

$comboBox1.DropDownStyle =  
[System.Windows.Forms.ComboBoxStyle]::DropDown;  
$comboBox1.Sorted = $true;

$comboBox2.FormattingEnabled = $True  
$System\_Drawing\_Size = New-Object System.Drawing.Size  
$System\_Drawing\_Size.Width = 250  
$System\_Drawing\_Size.Height = 21  
$comboBox2.Size = $System\_Drawing\_Size  
$comboBox2.DataBindings.DefaultDataSourceUpdateMode = 0  
$comboBox2.Name = “comboBox2”  
$System\_Drawing\_Point = New-Object System.Drawing.Point  
$System\_Drawing\_Point.X = 60  
$System\_Drawing\_Point.Y = 130  
$comboBox2.Location = $System\_Drawing\_Point  
$comboBox2.TabIndex = 0  
$comboBox2.add\_SelectedIndexChanged($handler\_comboBox2\_SelectedIndexChanged)

$comboBox2.DropDownStyle =  
[System.Windows.Forms.ComboBoxStyle]::DropDown;  
$comboBox2.Sorted = $true;

$comboBox1.add\_Click({

fill\_dclist

})

$comboBox2.add\_Click({

fill\_dclist1

})  
$comboBox1.AutoCompleteMode =  
[System.Windows.Forms.AutoCompleteMode]::SuggestAppend;  
$comboBox1.AutoCompleteSource =  
[System.Windows.Forms.AutoCompleteSource]::CustomSource;

$comboBox2.AutoCompleteMode =  
[System.Windows.Forms.AutoCompleteMode]::SuggestAppend;  
$comboBox2.AutoCompleteSource =  
[System.Windows.Forms.AutoCompleteSource]::CustomSource;

$form1.Controls.Add($comboBox1)

$form1.Controls.Add($comboBox2)

$InitialFormWindowState = $form1.WindowState  
$form1.add\_Load($OnLoadForm\_StateCorrection)  
$form1.ShowDialog()| Out-Null

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 5, 2016, 9:10am UTC](https://forums.powershell.org/t/loading-combobox/6754/2 "2016-07-05T09:10:29Z")

</div>

No need to loop through the array, just add it.

$combobox1.Items.AddRange((1…10))

---

<div class="post-metadata">

**Author:** ![amit-aman1](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@amit-aman1](https://forums.powershell.org/u/amit-aman1)\
**Post date:** [July 6, 2016, 1:36am UTC](https://forums.powershell.org/t/loading-combobox/6754/3 "2016-07-06T01:36:49Z")

</div>

Where exactly I need to add it? I want to populate combobox2 with the selected value in combobox1.

---

<div class="post-metadata">

**Author:** ![amit-aman1](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@amit-aman1](https://forums.powershell.org/u/amit-aman1)\
**Post date:** [July 6, 2016, 2:48am UTC](https://forums.powershell.org/t/loading-combobox/6754/4 "2016-07-06T02:48:31Z")

</div>

made a few changes and now it’s working… following is my changes. Thanks a ton for your support 🙂

function fill\_dclist  
{  
$comboBox1.Items.Clear()  
$dclist = Invoke-Sqlcmd -Query “select distinct \* from master” -ServerInstance dc2012\sqlexpress -Database final  
$dclist = $dclist.vertical | select -Unique  
foreach ($dc in $dclist)  
{  
$comboBox1.Items.add($dc.toString())  
}  
}

function fill\_dclist1  
{

```
$comboBox2.Items.Clear()
$d = $comboBox1.Text;
$dclist1 = Invoke-Sqlcmd -Query "SELECT * FROM master WHERE [vertical]='$d'" -ServerInstance dc2012\sqlexpress -Database final
$dclist1 = $dclist1.'Cust Name' | select -Unique
foreach ($dc1 in $dclist1)
{
  $comboBox2.Items.add($dc1.toString())  
}

```

}

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/loading-combobox/6754/5 "2024-05-16T20:40:54Z")

</div>

Closed on May 16, 2024, 8:40pm UTC


