Author Posts

July 5, 2016 at 12:00 pm

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

July 5, 2016 at 2:40 pm

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

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

July 6, 2016 at 7:06 am

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

July 6, 2016 at 8:18 am

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())
}
}