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.
Locked
charliem
Posts: 3
Joined: Tue Aug 27, 2019 4:18 pm

String Formatting to Textbox with param vars

Post by charliem » Thu Aug 29, 2019 8:08 pm

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: 13901
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Formatting to Textbox with param vars

Post by jvierra » Thu Aug 29, 2019 8:23 pm

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
Joined: Tue Aug 27, 2019 4:18 pm

Re: String Formatting to Textbox with param vars

Post by charliem » Fri Aug 30, 2019 10:25 am

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: 13901
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Formatting to Textbox with param vars

Post by jvierra » Fri Aug 30, 2019 11:15 am

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: 13901
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Formatting to Textbox with param vars

Post by jvierra » Fri Aug 30, 2019 11:37 am

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
Joined: Tue Aug 27, 2019 4:18 pm

Re: String Formatting to Textbox with param vars

Post by charliem » Fri Aug 30, 2019 11:47 am

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: 13901
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: String Formatting to Textbox with param vars

Post by jvierra » Fri Aug 30, 2019 12:04 pm

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.

Locked