String Formatting to Textbox with param vars

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 4 years and 6 months 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
charliem
Posts: 3
Last visit: Fri Aug 30, 2019 3:27 pm

String Formatting to Textbox with param vars

Post by charliem »

I've been on this for a few hours, I must be missing something. I'd appreciate a push in the right direction if anyone has a minute.

System
Powershell Studio 2019 64 bit
Windows 10

User
Bought Powershell Studio yesterday
New to Powershell
Previous experience with Assembly, Pascal, C, PHP, Python

If I declare variables such as

Code: Select all

$First = "Fred"
$Last = "Flinstone"
$Customer = "Barney"
$Location = "Bedrock"
And display that in a textbox with

Code: Select all

$textboxInfo.Text = "First: $First`nLast: $Last`nCustomer: $Customer`nLocation: $Location"
I get the following output (as expected)
First: Fred
Last: Flinstone
Customer: Barney
Location: Bedrock

If I pass those same values in from a separate form

Code: Select all

param (
   $First,
   $Last,
   $Customer,
   $Location
)
And use the same output command

Code: Select all

$textboxInfo.Text = "First: $First`nLast: $Last`nCustomer: $Customer`nLocation: $Location"
The output to the textbox outputs with
First: Fred Flinstone Barney Bedrock
Last:
Customer:
Location:

I don't know what I am missing. I actually don't need the formatting pretty, I was just testing values were passing between forms correctly, and now I can't move past it because this makes no sense.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: String Formatting to Textbox with param vars

Post by jvierra »

You can't use "=" to concatenate strings. Use "+". This is the same in Almost all languages.
To output multiple lines to a textbox use the multiline property and add the lines:

[b$textbox.Lines += 'some text'[/b]

To learn how to use Forms controls refer to these documents: https://info.sapien.com/index.php/guis/ ... ox-control
charliem
Posts: 3
Last visit: Fri Aug 30, 2019 3:27 pm

Re: String Formatting to Textbox with param vars

Post by charliem »

I am not using = to concatenate the string, but to assign the string to the textbox text method. I appreciate the link, I'd been through that thoroughly already.

The above line works perfectly fine if I assign variables locally in the script. Example below works as expected

Code: Select all

$First = "Fred"
$Last = "Flinstone"
$Customer = "Barney"
$Location = "Bedrock"
$textboxInfo.Text = "First: $First`nLast: $Last`nCustomer: $Customer`nLocation: $Location"
This does not

Code: Select all

param (
   $First,
   $Last,
   $Customer,
   $Location
)
$textboxInfo.Text = "First: $First`nLast: $Last`nCustomer: $Customer`nLocation: $Location"
I believe the problem is a sanitation issue with how param is passing values. I was at the end of a 14 hour day when I posted last night, I imagine I'll find the solution once I turn my attention back to this later today (depending on the number of fires).

If anyone has any insight I'd appreciate it. I'll post the fix later unless someone beats me to it.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: String Formatting to Textbox with param vars

Post by jvierra »

Sorry. The code I looked at was in the email notification and all line breaks were missing.

This is the better way to load a textbox:

Code: Select all

# at top of form script box
    param (
        $First,
        $Last,
        $Customer,
        $Location
    )
    
    # this must be in the Form_Load event.
    $textboxInfo.Lines += 'First:' + $First
    $textboxInfo.Lines += 'Last:' + $Last
    $textboxInfo.Lines += 'Customer:' + $Customer
    $textboxInfo.Lines += 'Location:' + $Location
    

If the values are empty then it is likely you have scope issues.

As posted there is not enough information to determine what you are doing wrong.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: String Formatting to Textbox with param vars

Post by jvierra »

It look to me like you are using commas to call the subform. PowerShell does not use commas on CmdLts.

The following is wrong.
Show_subform_psf 'Fred','Flinstone', 'Barney', 'Bedrock'

This is correct:
Show_subform_psf 'Fred' 'Flinstone' 'Barney' 'Bedrock'

Notice - no commas.
charliem
Posts: 3
Last visit: Fri Aug 30, 2019 3:27 pm

Re: String Formatting to Textbox with param vars

Post by charliem »

Jvierra, thanks for the help! You hit the nail on the head, I was using comma's when calling the child form passing the values over. I knew I was doing something wrong passing the values. Much appreciate the help!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: String Formatting to Textbox with param vars

Post by jvierra »

Yes. It is a common mistake with new user to PowerShell. PS uses commas to declare an array. Other languages won't do that and may display an error when commas are used. PS will just make an array and assign or pass it.
This topic is 4 years and 6 months 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