Answered by:
Powershell Combobox populate from xml and ComboBox 2 based on ComboBox 1 Selection

Question
-
This is the script I am using for my drop down box.
the script created to rename computers based in location and department
so it ask users to choose their location (building and the floor) and the department, every building and department has a code and then users enter their ext number
Headquarter Building = "HQ"
Airport Office = "AIR"
Laboratories = "LAB"
Information Technology = "IT"
Human Resources = "HR"
Finance = "FIN"
After choosing location and department and entering ext number the computer named is created like this 'HQ-HR-3456"
SO what I'm stock in is this
1- need to load data or populate Combobox from xml file
2- when they selecting building in first ComboBox then the second ComboBox shows the floors
Is there a way to make these 2 things happen?
i do my research last two weeks but i ended empty handed so sad
so i try my best to do so.
i write my xml file but not sure
<?xml version="1.0" encoding="utf-8" ?> <root> <campuses> <campus uniquecode="LDN" label="London"> <buildings> <building uniquecode="HQ" label="Headquarter Building"> <floors> <floor uniquecode="G" label="Ground Floor" /> <floor uniquecode="F" label="First Floor" /> <floor uniquecode="S" label="Second Floor" /> <floor uniquecode="T" label="Third Floor" /> </floors> </building> <building uniquecode="LHRO" label="London Airport Office"> <floors> <floor uniquecode="G" label="Ground Floor" /> <floor uniquecode="F" label="First Floor" /> <floor uniquecode="S" label="Second Floor" /> <floor uniquecode="T" label="Third Floor" /> </floors> </building> <building uniquecode="LAP" label="Lap Building"> <floors> <floor uniquecode="G" label="Ground Floor" /> <floor uniquecode="F" label="First Floor" /> <floor uniquecode="S" label="Second Floor" /> <floor uniquecode="T" label="Third Floor" /> </floors> </building> </buildings> </campus> </campuses> <departments> <department uniquecode="CEO" label="CEO" /> <department uniquecode="LABS" label="Laboratory" /> <department uniquecode="IT" label="Information Technology" /> <department uniquecode="HR" label="Human Resources" /> <department uniquecode="FIN" label="Finnance" /> </departments> </root>
this is my scripts
#Loading Assemnlies Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $Global:ErrorProvider = New-Object System.Windows.Forms.ErrorProvider function Set-ComputerName { $ErrorProvider.Clear() if ($DDBBuilding.Text.Length -eq 0) { $ErrorProvider.SetError($GBBuilding, "Please Choose Your Building") } elseif ($DDBFloornumber.Text.Length -eq 0) { $ErrorProvider.SetError($GBFloornumber, "Please Choose Your Floor") } elseif ($DDBSector.Text.Length -eq 0) { $ErrorProvider.SetError($GBSector, "Please Choose Your Sector") } #Validation Rule for computer names. elseif ($TBExtnumber.Text.Length -gt 4 ) { $ErrorProvider.SetError($GBExtnumber, "Your Extension Number invalid, Please Enter Your Extension Number") } elseif ($TBExtnumber.Text.Length -eq 0 ) { $ErrorProvider.SetError($GBExtnumber, "Your Extension Number invalid, Please Enter Your Extension Number") } else { $OutputBox.Text = $DDBBuilding.SelectedItem.ToSTRING() + $DDBFloornumber.SelectedItem.ToSTRING() + "-" + $DDBSector.SelectedItem.ToSTRING() + "-" + $TBExtnumber.Text.ToUpper() $ComputerName = $DDBBuilding.SelectedItem.ToSTRING() + $DDBFloornumber.SelectedItem.ToSTRING() + "-" + $DDBSector.SelectedItem.ToSTRING() + "-" + $TBExtnumber.Text.ToUpper() Rename-Computer -NewName "$ComputerName" Restart-Computer } } #Start To Creat Form $Form = New-Object System.Windows.Forms.Form $Form.Size = New-Object System.Drawing.Size(400,500) $Form.StartPosition = "CenterScreen" $Form.Text = "change Computer name" #Creating DropDownBox $DDBBuilding = New-Object System.Windows.Forms.ComboBox $DDBBuilding.Location = New-Object System.Drawing.Size(85,30) $DDBBuilding.Size = New-Object System.Drawing.Size(200,50) $DDBBuilding.Height = 200 $Form.Controls.Add($DDBBuilding) $Details = @("HQ","airport","Lab") foreach($Detail in $Details){ $DDBBuilding.Items.Add($Detail) } #Add a Label to input box $GBBuilding = New-Object System.Windows.Forms.GroupBox $GBBuilding.Location = New-Object System.Drawing.Size(80,10) $GBBuilding.Size = New-Object System.Drawing.Size(210,50) $GBBuilding.Text = "Choose Your Building" $GBBuilding.BackColor = "Transparent" $GBBuilding.AutoSize = $true $Form.Controls.Add($GBBuilding) #Creating DropDownBox $DDBFloornumber = New-Object System.Windows.Forms.ComboBox $DDBFloornumber.Location = New-Object System.Drawing.Size(85,90) $DDBFloornumber.Size = New-Object System.Drawing.Size(200,50) $DDBFloornumber.Height = 200 $Form.Controls.Add($DDBFloornumber) $Details = @("G","F","S") foreach($Detail in $Details){ $DDBFloornumber.Items.Add($Detail) } #Add a Label to input box $GBFloornumber = New-Object System.Windows.Forms.GroupBox $GBFloornumber.Location = New-Object System.Drawing.Size(80,70) $GBFloornumber.Size = New-Object System.Drawing.Size(210,50) $GBFloornumber.Text = "Choose Your Floor" $GBFloornumber.BackColor = "Transparent" $GBFloornumber.AutoSize = $true $Form.Controls.Add($GBFloornumber) #Creating DropDownBox $DDBSector = New-Object System.Windows.Forms.ComboBox $DDBSector.Location = New-Object System.Drawing.Size(85,150) $DDBSector.Size = New-Object System.Drawing.Size(200,50) $DDBSector.Height = 200 $Form.Controls.Add($DDBSector) $Details = @("FIN","HR","IT") foreach($Detail in $Details){ $DDBSector.Items.Add($Detail) } #Add a Label to input box $GBSector = New-Object System.Windows.Forms.GroupBox $GBSector.Location = New-Object System.Drawing.Size(80,130) $GBSector.Size = New-Object System.Drawing.Size(210,50) $GBSector.Text = "Choose Your Sector" $GBSector.BackColor = "Transparent" $GBSector.AutoSize = $true $Form.Controls.Add($GBSector) #Add an Input box $TBExtnumber = New-Object System.Windows.Forms.TextBox $TBExtnumber.Location = New-Object System.Drawing.Size(85,210) $TBExtnumber.Size = New-Object System.Drawing.Size(200,50) $TBExtnumber.TabIndex = "1" $Form.Controls.Add($TBExtnumber) #Add a Label to input box $GBExtnumber = New-Object System.Windows.Forms.GroupBox $GBExtnumber.Location = New-Object System.Drawing.Size(80,190) $GBExtnumber.Size = New-Object System.Drawing.Size(210,50) $GBExtnumber.Text = "Enter Your Extension Number:" $GBExtnumber.BackColor = "Transparent" $GBExtnumber.AutoSize = $true $Form.Controls.Add($GBExtnumber) #Creat a outputbox $OutputBox = New-Object System.Windows.Forms.RichTextBox $OutputBox.Location = New-Object System.Drawing.Size(85,270) $OutputBox.Size = New-Object System.Drawing.Size(200,50) $OutputBox.Multiline = $true $Form.Controls.Add($OutputBox) #Add a Label to input box $GBOutputBox = New-Object System.Windows.Forms.GroupBox $GBOutputBox.Location = New-Object System.Drawing.Size(80,250) $GBOutputBox.Size = New-Object System.Drawing.Size(210,80) $GBOutputBox.Text = "Your New Computer Name is:" $GBOutputBox.BackColor = "Transparent" $GBOutputBox.AutoSize = $true $Form.Controls.Add($GBOutputBox) #Create a Button $ButtonValidate = New-Object System.Windows.Forms.Button $ButtonValidate.Location = New-Object System.Drawing.Size(60,350) $ButtonValidate.Size = New-Object System.Drawing.Size(60,30) $ButtonValidate.Text = "Validate" $ButtonValidate.TabIndex = "2" $ButtonValidate.Add_Click({Set-ComputerName}) $Form.Controls.Add($ButtonValidate) $form.ShowDialog()
Big thanks to help in advanced :)
Sunday, July 26, 2020 3:59 PM
Answers
-
Exam[le of loading XML into a form.
#Creating DropDownBox $DDBBuilding = New-Object System.Windows.Forms.ComboBox $Form.Controls.Add($DDBBuilding) $DDBBuilding.Location = New-Object System.Drawing.Size(85,30) $DDBBuilding.Size = New-Object System.Drawing.Size(200,50) $DDBBuilding.Height = 200 $buildings = $xml.SelectNodes('//building') $DDBBuilding.DataSource = [system.collections.arraylist]($buildings | select *) $DDBBuilding.DisplayMember = 'label' # load next combo on change $DDBBuilding.add_SelectedIndexChanged({ $DDBFloornumber.DataSource = [system.collections.arraylist]($DDBBuilding.SelectedItem.floors.floor | select *) $DDBFloornumber.DisplayMember = 'label' })
\_(ツ)_/
Sunday, July 26, 2020 7:58 PM
All replies
-
Load the data into a data table as a set of records then use a relational extraction with the ComboBox events.
You can also just use XPATH queries to extract the associated relations elements as each combo value is selected.
\_(ツ)_/
Sunday, July 26, 2020 6:17 PM -
Exam[le of loading XML into a form.
#Creating DropDownBox $DDBBuilding = New-Object System.Windows.Forms.ComboBox $Form.Controls.Add($DDBBuilding) $DDBBuilding.Location = New-Object System.Drawing.Size(85,30) $DDBBuilding.Size = New-Object System.Drawing.Size(200,50) $DDBBuilding.Height = 200 $buildings = $xml.SelectNodes('//building') $DDBBuilding.DataSource = [system.collections.arraylist]($buildings | select *) $DDBBuilding.DisplayMember = 'label' # load next combo on change $DDBBuilding.add_SelectedIndexChanged({ $DDBFloornumber.DataSource = [system.collections.arraylist]($DDBBuilding.SelectedItem.floors.floor | select *) $DDBFloornumber.DisplayMember = 'label' })
\_(ツ)_/
Sunday, July 26, 2020 7:58 PM -
thanks bro that's solve the hard pert of my problem
but still there is a small problem
how I can related that building selected in building dropdownbox with its code
like if select headquarter Building will take its code "HQ"
that's all thanks
Sunday, July 26, 2020 9:26 PM -
Here is a simple example of how to do a relational set of ComboBoxes from XML.
Here is the PS1 file: Demo-FormRenameComputer.ps1
\_(ツ)_/
Sunday, July 26, 2020 10:44 PM -
I really appreciate your endless help :)
I went through your script, I'm so amazed with your efforts I'm so thankful to you, that was really helpful.
I only have question in your script when i click rename button it shows me a massage with yes and no but when click on yes there is nothing happens, any short hint ...
Can you refer me to topic or any help when click on button (rename) the massage box shows to user the selected info to review and confirm.
Monday, July 27, 2020 6:44 AM -
When you don't understand a CmdLet then look it up in the help. Before using forms you must be skilled at PowerShell. Without PowerShell skills everything will be like your first day in kindergarten. You must learn PS before anything else::
Start here: Windows PowerShell™: TFM
\_(ツ)_/
Monday, July 27, 2020 6:49 AM -
I really thank you I solved all my problems, all credits goes to you
I thank you again, I've learned so much from you
only one question if you can help me with:
what the best practice to show user a review and confirm their input ?
is it by message box or by new form ?
I use message box but want to review their input so I Couldn't show the variables in lines?
also I want best way to check all the combobox and message alert tells what missing and not filled
use if with foreach or what?
- Edited by iDr_Tech Monday, August 3, 2020 7:20 PM
Monday, August 3, 2020 7:19 PM -
You can format the message in the box into lines or you can create a form that displays what you want. The choice depends on what you think is needed. There is no one rule for this.
\_(ツ)_/
Monday, August 3, 2020 7:54 PM -
Ok
can you give me an example for message box with multi variables in lines...
Monday, August 3, 2020 8:24 PM -
It is just a string with line breaks where needed. Just build the string and display it.
\_(ツ)_/
Monday, August 3, 2020 9:06 PM -
i tried so hard i couldn't do it
i mean i couldn't show what item was selected
can you help please
$msgBoxInput = [System.Windows.Forms.MessageBox]::Show( "Your Old Computer Name $OldComputerName","`nyour department $SelectedDepartment", 'Rename Computer', 'YesNo')
Tuesday, August 4, 2020 8:07 AM -
Create the string then add it to the method. You are using two strings. You can only have one.
\_(ツ)_/
Tuesday, August 4, 2020 8:14 AM -
i tried all the method nothing works for me
$msgBoxoutpout = "$OldComputerName","$txtComputerName","$cbDepartment.SelectedItem.label","$cbCampus.SelectedItem.label" $msgBoxInput = [System.Windows.Forms.MessageBox]::Show( "$msgBoxoutpout", 'Rename Computer', 'YesNo')
i cant add pic to show you the result.
Tuesday, August 4, 2020 8:34 AM -
I suggest that if you learn PowerShell you would be able to solve the beginner issues by yourself. You also need to correctly format and post your code. What you have posted is unreadable. How can you expect anyone to understand what you are doing? How can you understand what you are doing?
When using any command always read the documentation first.
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox.show?view=netcore-3.1
\_(ツ)_/
- Edited by jrv Tuesday, August 4, 2020 8:39 AM
Tuesday, August 4, 2020 8:37 AM