Part of a script has a line to concatenate strings. The strings are concatenating out of order.
Suggestions to fix greatly appreciated.
This…
$uId = "8"
$uName = "Corbin Dallas"
$uCompany = "The 5th Element"
write-host $uId; $uName; $uCompany
…works as expected and produces this (but of course it isn’t a new variable that can be used elsewhere)
8 Corbin Dallas The 5th Element
However I need to add some additional elements to the strings and have been getting unexpected results. So, I’ve pared down what I’m doing to try and isolate the problem and that’s when I found the strings concatenating out of order.
this…
$cmd = $uId, """", $uName, """", $uCompany -join ""
Write-Host $cmd
produces this…
8 Corbin Dallas The 5th Element""
and this…
$cmd2 = $uId + $uName + """" + $uCompany + """"
Write-Host $cmd2
produces the same thing…
8 Corbin Dallas The 5th Element""
This is what things look like at the moment
And here’s the complete code listing
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function call_browser {
param ( [string]$uId, [string]$uName, [string]$uCompany )
$folder = New-Object System.Windows.Forms.FolderBrowserDialog
$folder.Description = "Select where to save file."
$dest = $folder.ShowDialog()
if ($dest -eq "OK" )
{
write-host "This path was selected - " """" $($folder.SelectedPath) """" -Separator ""
write-host $uId; $uName; $uCompany
$cmd = $uId, """", $uName, """", $uCompany -join ""
Write-Host $cmd
$cmd2 = $uId + $uName + """" + $uCompany + """"
Write-Host $cmd2
}
else
{ write-host "$dest selected" }
}
$form = New-Object System.Windows.Forms.Form
$form.Text = 'My Form'
$form.Size = New-Object System.Drawing.Size(300,270)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,190)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'Select Path'
$okButton.Add_Click({call_browser( $textBoxID.Text, $textBoxName.Text, $textBoxCompany.Text )})
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,190)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Quit'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$labelID = New-Object System.Windows.Forms.Label
$labelID.Location = New-Object System.Drawing.Point(10,20)
$labelID.Size = New-Object System.Drawing.Size(280,20)
$labelID.Text = "ID"
$form.Controls.Add($labelID)
$textBoxID = New-Object System.Windows.Forms.TextBox
$textBoxID.Location = New-Object System.Drawing.Point(10,40)
$textBoxID.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBoxID)
$labelName = New-Object System.Windows.Forms.Label
$labelName.Location = New-Object System.Drawing.Point(10,70)
$labelName.Size = New-Object System.Drawing.Size(280,20)
$labelName.Text = 'Name:'
$form.Controls.Add($labelName)
$textBoxName = New-Object System.Windows.Forms.TextBox
$textBoxName.Location = New-Object System.Drawing.Point(10,90)
$textBoxName.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBoxName)
$labelCompany = New-Object System.Windows.Forms.Label
$labelCompany.Location = New-Object System.Drawing.Point(10,120)
$labelCompany.Size = New-Object System.Drawing.Size(280,20)
$labelCompany.Text = 'Company:'
$form.Controls.Add($labelCompany)
$textBoxCompany = New-Object System.Windows.Forms.TextBox
$textBoxCompany.Location = New-Object System.Drawing.Point(10,140)
$textBoxCompany.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBoxCompany)
$form.Add_Shown({$textBoxID.Select()})
$form.Topmost = $true
$form.ShowDialog()