How to connect 3 comboboxes and Textbox? POWERSHELL and WPF

How to connect 3 comboboxes and Textbox? POWERSHELL and WPF

L00K
Advocate Advocate
4,578 Views
17 Replies
Message 1 of 18

How to connect 3 comboboxes and Textbox? POWERSHELL and WPF

L00K
Advocate
Advocate


Hi everybody,

here is my problem. I have 3 comboboxes that read data from mdf file. Selected value from the first one will affect on what shows in the second one, and selected value from the second one will affect on what shows in the third one. Result from the third one will populate in the textbox.
See attached picture.

Here is a code from PowerShell.combobox.jpg

 






function GetData() { $objOleDbConnection1= New-Object "System.Data.OleDb.OleDbConnection" $objOleDbCommand = New-Object "System.Data.OleDb.OleDbCommand" $objOleDbAdapter = New-Object "System.Data.OleDb.OleDbDataAdapter" $objDataTable = New-Object "System.Data.DataTable" $objOleDbConnection1.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Script\test_1.mdb;" $objOleDbConnection1.Open() $objOleDbCommand.Connection = $objOleDbConnection1 $objOleDbCommand.CommandText = "SELECT SORT + ' ' + '-' + ' ' + Tekst, SAPstruktur FROM SAPstruktur where SAPstruktur like 'A__' ORDER BY ID ASC" #set the Adapter object $objOleDbAdapter.SelectCommand = $objOleDbCommand #fill the objDataTable object with the results $objOleDbAdapter.Fill($objDataTable) return $objDataTable.DefaultView $objOleDbConnection1.Close() }

 And here is code from Combobox no 1 i WPF

<ComboBox x:Name="mdb" HorizontalAlignment="Left" VerticalAlignment="Top" Width="275" Grid.Column="1"  DisplayMemberPath="Expr1000" SelectedValuePath="SAPstruktur" ItemsSource="{Binding PsList[GetData]}" SelectedIndex="0"/>


QUESTION IS: How to connect all these 3 comboboxes and Textbox? Of cource i need to create 2 more comboboxes 🙂

Greetings
Lukas

 

 

 

 

0 Likes
4,579 Views
17 Replies
Replies (17)
Message 2 of 18

L00K
Advocate
Advocate
Anyone?:(
0 Likes
Message 3 of 18

L00K
Advocate
Advocate

Can anyone help me out here? I'm really stacked, and can't find any way to break it through...I managed to trigger seperate comboboxes and return a value to combobox in Data Standard Dialog window. I was sure that by this i can send value of

$Prop["Tegningstype"].Value back to Powershell, and put it into the new query that is in the functions triggered by combobox no.2.

Unfortunately it does not work, value from $prop is not returned to Powershell.

 

Is it wrong way to do this or i forgot about something here?

0 Likes
Message 4 of 18

L00K
Advocate
Advocate
Come on people:)

So many experts er here... No one knows how to do it???

0 Likes
Message 5 of 18

wangdu
Collaborator
Collaborator

Hi,

 

you could do the following.

 

  • Hook an event handler to first combobox selectionchanged event. 
    $firstcombobox = $dsWindow.FindName("mdb")
    $firstcombobox.add_SelectionChanged(
    {
    param($sender,$args)
    })
  • Inside the handler you can collect the selected value which you can use to get the values for the second combobox
    $selectedValue = $sender.SelectedValue
    $itemsForSecondCombobox = #collect the items
    $secondcombobox = $dsWindow.FindName("SecondCombobox")
    $secondcombobox.ItemsSource = $itemsForSecondCombobox
  • Repeat the same for the second combobox and third combobox
  • For the third combobox use the selectedvalue to populate the textbox

Hope it helps!

 

 

Wangdu

 

coolOrange

www.coolOrange.com

0 Likes
Message 6 of 18

L00K
Advocate
Advocate

Thanks for responce. I'm a newbe in this kind of programming...and might ask stupid questions, but what you mean by "hooking to"?

0 Likes
Message 7 of 18

L00K
Advocate
Advocate

could you please walk me through???

0 Likes
Message 8 of 18

wangdu
Collaborator
Collaborator

Hi,

 

In the InitializeWindow function of the Default.ps1 file, Add the following:

 

 

$firstcombobox = $dsWindow.FindName("mdb")
$secondcombobox = $dsWindow.FindName("SecondCombobox")
$thirdcombobox= $dsWindow.FindName("thirdcombobox")

$firstcombobox.add_SelectionChanged(
{
param($sender,$args)
FeedCombobox($sender.SelectedValue, $secondCombobox)
})

$secondcombobox .add_SelectionChanged(
{
param($sender,$args)
FeedCombobox($sender.SelectedValue, $thirdcombobox)
})

$thirdcombobox.add_SelectionChanged(
{
param($sender,$args)
$textbox= $dsWindow.FindName("textbox1")
$textbox.Text = $send.SelectedValue
})

Now define the FeedCombobox function like this in the Default.ps1. This function can be called for both from first and second combobox selectionchanged event handler.

 

 

function FeedCombobox($selectedValue, $cmbbx)
{
     $itemsForCombobox = #collect the items using the selectedValue by making sql queries here
     $cmbbx.ItemsSource = $itemsForCombobox 
}

Please make sure that you have defined three comboboxes and textbox with the name used in the script. Note also that the SelectedValuePath is used and defined correctly.

 

Hope it helps!

 

Wangdu

 

coolOrange

www.coolOrange.com

 

0 Likes
Message 9 of 18

L00K
Advocate
Advocate

Thanks you very much for all your help.

Here is what i have now in WPF:

<ComboBox x:Name="mdb" HorizontalAlignment="Left" VerticalAlignment="Top" Width="275" Grid.Column="1"  DisplayMemberPath="Expr1000" SelectedValuePath="SAPstruktur" ItemsSource="{Binding PsList[GetData]}" SelectedIndex="0" />
            <ComboBox x:Name="SecondCombobox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="275"  DisplayMemberPath="Expr1000" SelectedValuePath="SAPstruktur" SelectedIndex="0" Margin="0,43,0,0" Grid.Column="1" />
            <ComboBox x:Name="thirdcombobox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="275" Grid.Column="1"  DisplayMemberPath="Expr1000" SelectedValuePath="SAPstruktur"  SelectedIndex="0" Margin="0,91,0,0"/>

 

 

And here is content of the ps.1

 

function InitializeWindow
{
	$dsWindow.Width = 600
	$dsWindow.Height = 400
	$dsDiag.ShowLog()
	$dsDiag.Clear() 
	$firstcombobox = $dsWindow.FindName("mdb")
	$secondcombobox = $dsWindow.FindName("SecondCombobox")
	$thirdcombobox= $dsWindow.FindName("thirdcombobox")

	$firstcombobox.add_SelectionChanged(
	{
	param($sender,$args)
	FeedCombobox($sender.SelectedValue, $secondCombobox)
	})

	$secondcombobox.add_SelectionChanged(
	{	
	param($sender,$args)
	FeedCombobox($sender.SelectedValue, $thirdcombobox)
	})

	$thirdcombobox.add_SelectionChanged(
{	
	param($sender,$args)
	$textbox= $dsWindow.FindName("text_combobox")
	$textbox.Text = $send.SelectedValue
	})
function FeedCombobox($selectedValue, $cmbbx)
{
    $objOleDbConnection1= New-Object "System.Data.OleDb.OleDbConnection"
	$objOleDbCommand = New-Object "System.Data.OleDb.OleDbCommand"
	$objOleDbAdapter = New-Object "System.Data.OleDb.OleDbDataAdapter"
	$objDataTable = New-Object "System.Data.DataTable"
	$objOleDbConnection1.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Script\test_1.mdb;"
	$objOleDbConnection1.Open()
	$objOleDbCommand.Connection = $objOleDbConnection1
    $objOleDbCommand.CommandText = "SELECT SAPstruktur + ' ' + '-' + ' ' + Tekst, SAPstruktur FROM SAPstruktur where SAPstruktur like 'NK' ORDER BY ID ASC" 
	$itemsForCombobox = "SELECT SAPstruktur + ' ' + '-' + ' ' + Tekst, SAPstruktur FROM SAPstruktur where SAPstruktur = '$selectedValue' ORDER BY ID ASC" 
    $cmbbx.ItemsSource = $itemsForCombobox 
	#set the Adapter object
	$objOleDbAdapter.SelectCommand = $objOleDbCommand
	#fill the objDataTable object with the results
	$objOleDbAdapter.Fill($objDataTable)
	#$mdb.ItemsSource = @($objDataTable)
	#$mdb.DisplayMemberPath = "Expr1000"
	#$mdb.SelectedValuePath = "SAPstruktur"
	#To display the "raw" contents, just enter
	return $objDataTable.DefaultView
	$objOleDbConnection1.Close()
	
}

Content of combobox "mdb"

 

 

function GetData
{
	$objOleDbConnection1= New-Object "System.Data.OleDb.OleDbConnection"
	$Tier2ClassificationCategory = New-Object System.Windows.Forms.ComboBox
	$objOleDbCommand = New-Object "System.Data.OleDb.OleDbCommand"
	$objOleDbAdapter = New-Object "System.Data.OleDb.OleDbDataAdapter"
	$objDataTable = New-Object "System.Data.DataTable"
	$objOleDbConnection1.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Script\test_1.mdb;"
	$objOleDbConnection1.Open()
	$objOleDbCommand.Connection = $objOleDbConnection1
    $objOleDbCommand.CommandText = "SELECT SORT + ' ' + '-' + ' ' + Tekst, SAPstruktur FROM SAPstruktur where SAPstruktur like 'A__' ORDER BY ID ASC"
	#set the Adapter object
	$objOleDbAdapter.SelectCommand = $objOleDbCommand
	$Tier2ClassificationCategory.SelectedIndex = -1
	#fill the objDataTable object with the results
	$objOleDbAdapter.Fill($objDataTable)
	#To display the "raw" contents, just enter
	return $objDataTable.DefaultView
	$objOleDbConnection1.Close()
}

Before each of  comboboxes had its own "GetData" function. Now logically only the first one will have it.

 

 

I'm not sure if i do all correct, because i do not get any results in Secondcombobox.

 

 

Do i need to do any changes to binding between comboboxes and ps1? What should be binded to ItemsSource i WPF?

 

 

Thanks a lot!

0 Likes
Message 10 of 18

wangdu
Collaborator
Collaborator

Hi,

 

For filling up the second combobox, you have to do it similar to how you have done in the GetData function. The GetData function returns the $objDataTable.GetDefaultView(). So, then, instead of now returning it in FeedCombobox function, it assigns it directly to the $cmbbx.ItemsSource. Here's an example below:

#trimmed 
$objOleDbCommand.CommandText = "SELECT SORT + ' ' + '-' + ' ' + Tekst, SAPstruktur FROM SAPstruktur where SAPstruktur like 'A__' ORDER BY ID ASC"
#set the Adapter object
$objOleDbAdapter.SelectCommand = $objOleDbCommand
$objOleDbAdapter.Fill($objDataTable)
$cmbbx.ItemsSource = $objDataTable.DefaultView

 Hope it helps!

 

Wangdu

 

coolOrange

www.coolOrange.com

0 Likes
Message 11 of 18

L00K
Advocate
Advocate
Thanks for responce...Could you please show me "literally" how to do this? f.e. linking cmb1 and cmb2???
It does not work for me her .(
0 Likes
Message 12 of 18

L00K
Advocate
Advocate
hmm i have looked at this problem once again. And i if get $cmbbx.ItemsSource. from the first cmbx how to use this in order to read from database new values. As i tried to explan, the thing is to get in the first cmbx full lists of values (this works). Then with picked up value form the first cmbx use this value to selec values for the second cmbx. And similar action for the third one. So to picture this a bit better imagine that you er i a library and there is a choise: 1. Type of books (drama, crime aso). - reader picked the type 2. Then he get a choice of "language" - reader picked "english" 3. Then the third choise of "writer" - reader picked "tom Clancy". This is how i want to organize my comboboxes. I make 3 selects from the database, one for each cmbx... Step 1 is done :)) Step 2 and 3...does not work.....:)
0 Likes
Message 13 of 18

wangdu
Collaborator
Collaborator

Hi again,

 

Lets say this is your xaml

<ComboBox x:Name="mdb" HorizontalAlignment="Left" VerticalAlignment="Top" Width="275" Grid.Column="1"  DisplayMemberPath="Expr1000" SelectedValuePath="SAPstruktur" ItemsSource="{Binding PsList[GetData]}" SelectedIndex="0" />
            <ComboBox x:Name="SecondCombobox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="275"  DisplayMemberPath="Expr1000" SelectedValuePath="SAPstruktur" SelectedIndex="0" Margin="0,43,0,0" Grid.Column="1" />
            <ComboBox x:Name="thirdcombobox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="275" Grid.Column="1"  DisplayMemberPath="Expr1000" SelectedValuePath="SAPstruktur"  SelectedIndex="0" Margin="0,91,0,0"/>

And the content of the ps1

 

function InitializeWindow
{
	$dsWindow.Width = 600
	$dsWindow.Height = 400
	$dsDiag.ShowLog()
	$dsDiag.Clear() 
	$firstcombobox = $dsWindow.FindName("mdb")
	$secondcombobox = $dsWindow.FindName("SecondCombobox")
	$thirdcombobox= $dsWindow.FindName("thirdcombobox")

	$firstcombobox.add_SelectionChanged(
	{
	param($sender,$args)
        $commandText = "SELECT SAPstruktur + ' ' + '-' + ' ' + Tekst, SAPstruktur FROM SAPstruktur where SAPstruktur = '$sender.SelectedValue' ORDER BY ID ASC"
	FeedCombobox($secondCombobox,, $commandText)
	})

	$secondcombobox.add_SelectionChanged(
	{	
	param($sender,$args)
         $commandText = "SELECT SAPstruktur + ' ' + '-' + ' ' + Tekst, SAPstruktur FROM SAPstruktur where SAPstruktur = '$sender.SelectedValue' ORDER BY ID ASC"
	FeedCombobox($thirdcombobox, $commandText)
	})

	$thirdcombobox.add_SelectionChanged(
	{	
	param($sender,$args)
	$textbox= $dsWindow.FindName("text_combobox")
	$textbox.Text = $send.SelectedValue
	})
}
function FeedCombobox($cmbbx, $commandText)
{
    $objOleDbConnection1= New-Object "System.Data.OleDb.OleDbConnection"
	$objOleDbCommand = New-Object "System.Data.OleDb.OleDbCommand"
	$objOleDbAdapter = New-Object "System.Data.OleDb.OleDbDataAdapter"
	$objDataTable = New-Object "System.Data.DataTable"
	$objOleDbConnection1.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Script\test_1.mdb;"
	$objOleDbConnection1.Open()
	$objOleDbCommand.Connection = $objOleDbConnection1
	#set the Adapter object
	$objOleDbAdapter.SelectCommand = $objOleDbCommand
	#fill the objDataTable object with the results
	$$objOleDbCommand.CommandText = $commandText
        #set the Adapter object
        $objOleDbAdapter.SelectCommand = $objOleDbCommand
        $objOleDbAdapter.Fill($objDataTable)
        $cmbbx.ItemsSource = $objDataTable.DefaultView
        $objOleDbConnection1.Close()	
}
function GetData
{
	$objOleDbConnection1= New-Object "System.Data.OleDb.OleDbConnection"
	$Tier2ClassificationCategory = New-Object System.Windows.Forms.ComboBox
	$objOleDbCommand = New-Object "System.Data.OleDb.OleDbCommand"
	$objOleDbAdapter = New-Object "System.Data.OleDb.OleDbDataAdapter"
	$objDataTable = New-Object "System.Data.DataTable"
	$objOleDbConnection1.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Script\test_1.mdb;"
	$objOleDbConnection1.Open()
	$objOleDbCommand.Connection = $objOleDbConnection1
    $objOleDbCommand.CommandText = "SELECT SORT + ' ' + '-' + ' ' + Tekst, SAPstruktur FROM SAPstruktur where SAPstruktur like 'A__' ORDER BY ID ASC"
	#set the Adapter object
	$objOleDbAdapter.SelectCommand = $objOleDbCommand
	$Tier2ClassificationCategory.SelectedIndex = -1
	#fill the objDataTable object with the results
	$objOleDbAdapter.Fill($objDataTable)
	#To display the "raw" contents, just enter
	return $objDataTable.DefaultView
	$objOleDbConnection1.Close()
}

 

 

 

Make sure that the sql query for the thirdcombobox should be replaced with correct query. And also to make sure that a TextBox control in the xaml with the name text_combobox is defined.

 

Hope now it works!

 

Wangdu

0 Likes
Message 14 of 18

L00K
Advocate
Advocate
Mane thanks for your help! I so appreciate it! 🙂

I have done copy/paste the code shown above. It seems to be problem with finding second combobox. If i define fixed query (without dynamic value, - SAPstruktur = "fixed value") there is nothing shown i second combobox (and it should show one value selected from the database). What might be missing?? some dll or something i the code?? I do not get it...how to check that combobox has be found in the window. I have checked all names and all seems to be ok.
Can i send you ps1 and xaml on e-mail so you can look at it??
0 Likes
Message 15 of 18

L00K
Advocate
Advocate

the same here...textbox er not filled up. I have added textbox line to check of there any value sent from the combobox.

Or, there something wrong with sent value (=0 ?)

Do i need to make any changed to Inventor.cfg?? ( register some variable or something?)

 

$firstcombobox.add_SelectionChanged(
	{
	param($sender,$args)
        $commandText = "SELECT SAPstruktur + ' ' + '-' + ' ' + Tekst, SAPstruktur FROM SAPstruktur where SAPstruktur = '$sender.SelectedValue' ORDER BY ID ASC"
	FeedCombobox($secondCombobox, $commandText)
	$textbox= $dsWindow.FindName("text_combobox")
	$textbox.Text = $send.SelectedValue
	})
0 Likes
Message 16 of 18

L00K
Advocate
Advocate

There is something wrong..i cannot even hide/show controls from Powershell script. Nothing works, $dsDiag.inspect() - no reaction...script cannot find eny controls in xaml..what might be wrong here??? Any clue?

0 Likes
Message 17 of 18

wangdu
Collaborator
Collaborator

Hi,

 

I am seeing a typo in the code you posted ($send instead of $sender).

 

As for finding out the issues with the empty combobox, writing out the intermediate states to the DataStandard Log window could help you debug your code. 

e.g.

$firstcombobox.add_SelectionChanged(
{
	param($sender,$args)
$dsDiag.ShowLog()#shows the log window $commandText = "SELECT SAPstruktur + ' ' + '-' + ' ' + Tekst, SAPstruktur FROM SAPstruktur where SAPstruktur = '$sender.SelectedValue' ORDER BY ID ASC" FeedCombobox($secondCombobox, $commandText) $textbox= $dsWindow.FindName("text_combobox") $selectedValue = $sender.SelectedValue $dsDiag.Trace(">> $($selectedValue)")#writes to the log window })

Hope it helps!

 

 

Wangdu

0 Likes
Message 18 of 18

L00K
Advocate
Advocate
Hi, thanks for replay. I have made a new set of files: default.ps1 and inventor.xaml. And used only code for comboboxes. $dsWindow.Name works fine and selected value from firstcombobox is nicely send to textbox. Now, i tried to go further with code you send me and it looks like DataStandard does not like: FeedCombobox($secondcombobox, $commandText). If i "comment" this line then DataStadard window appears, but if i remove "#" then it crashes...I guess there is some little issue that Powershell Script does not like. Any idea what might be wrong with this line? Best Regard Lukas
0 Likes