Multiple Comboboxes with One XML

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 6 years and 1 week 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
CI_Netops
Posts: 13
Last visit: Tue Nov 10, 2020 9:24 am

Multiple Comboboxes with One XML

Post by CI_Netops »

Hi there

Ive trawled through the support and I cant seem to find an answer that either helps me or that I can understand so I'm hoping by asking in my way I might get an answer I can use.

Ive created an XML file
  1. <categories>
  2.     <category>
  3.         <name>Chest</name>
  4.         <exercise>BB Bench Press</exercise>
  5.         <exercise>BB Bench Press Incline</exercise>
  6.     </category>
  7.     <category>
  8.         <name>Back</name>
  9.         <exercise>BB Bent Over Row</exercise>
  10.         <exercise>DB Bent Over Row</exercise>
  11.     </category>
  12. </categories>
And I can get one combobox to populate fine with the name.

Code: Select all

$test = [xml](Get-Content D:\exercise.xml)
	$categorys = $test.SelectNodes('//category') | %{ $_.Name}
	$combobox1.Items.AddRange($categorys)
What I cant then do is get the second CB to populate with the relevant Exercises once the name has been picked in the first CB
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple Comboboxes with One XML

Post by jvierra »

You will have to use the "SelectedIndexChanged" event to populate the second combo.
User avatar
CI_Netops
Posts: 13
Last visit: Tue Nov 10, 2020 9:24 am

Re: Multiple Comboboxes with One XML

Post by CI_Netops »

jvierra wrote: Thu Mar 15, 2018 9:56 am You will have to use the "SelectedIndexChanged" event to populate the second combo.
Hi Jvierra, yep fully understand that, but have no idea what the code is I need to do, as I cant work out how to make it see the selection in CB1 to then populate CB2
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple Comboboxes with One XML

Post by jvierra »

The value of CB1 is $cb1.Text
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple Comboboxes with One XML

Post by jvierra »

Here is a trivial example:

Code: Select all

$combobox1_SelectedIndexChanged={
    $item = $combobox1.Text
    $items = $xml.SelectNodes("//category/name[text()='$item']/../exercise/text()").InnerText
    $combobox2.Items.Clear()
    $combobox2.Items.AddRange($items)
}
User avatar
CI_Netops
Posts: 13
Last visit: Tue Nov 10, 2020 9:24 am

Re: Multiple Comboboxes with One XML

Post by CI_Netops »

Again using that example I simply get CB2 is null error
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple Comboboxes with One XML

Post by jvierra »

What is CB2? You have to use a real variable. Each ComboBox has a variable that is unique and is created automatically.
User avatar
CI_Netops
Posts: 13
Last visit: Tue Nov 10, 2020 9:24 am

Re: Multiple Comboboxes with One XML

Post by CI_Netops »

$Combobox2

mainly because I'm testing and haven't named either boxes yet from their default

the error I think is to do with it no seeing anything in the xml?

ERROR: + $items = $xml.SelectNodes("//category/name[text()='$item' ...
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (:) [], RuntimeException
ERROR: + FullyQualifiedErrorId : InvokeMethodOnNull
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple Comboboxes with One XML

Post by jvierra »

Works fine for me.
See attached example:
Attachments
TestForm.psf
(15.04 KiB) Downloaded 102 times
User avatar
CI_Netops
Posts: 13
Last visit: Tue Nov 10, 2020 9:24 am

Re: Multiple Comboboxes with One XML

Post by CI_Netops »

Using that example Combobox2 reads "System.data.datarowview"
This topic is 6 years and 1 week 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