Variable not displaying correctly in a text Box

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 11 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
User avatar
kilo1548
Posts: 15
Last visit: Tue Oct 18, 2022 2:23 pm

Variable not displaying correctly in a text Box

Post by kilo1548 »

Product, version and build: PowerShell Studio 2017 v5.4.138
32 or 64 bit version of product: 64Bit
Operating system: Windows 10 with WMF 5.1
32 or 64 bit OS: 64 Bit
PowerShell Version:
Name Value
---- -----
PSVersion 5.1.15063.0
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.15063.0
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1



Hey All,

I have a Forms project that I am working on and usually to not have any issues with updating a Label or Textbox's .text property with the contents of a variable but I am running into issues with this one.

Here is what I have on the form_Load event

Tried this as one solution..

Code: Select all

	$Var = Get-ChildItem 'C:|FilePath' | sort LastWriteTime | select -last 1 | Select-Object LastWriteTime | ft -hidetableheaders
	$textBox.Text = "$var"
And this..

Code: Select all

	$Var = Get-ChildItem 'C:|FilePath' | sort LastWriteTime | select -last 1 | Select-Object LastWriteTime | ft -hidetableheaders
	$textBox.Text = "$(Get-ChildItem 'C:|FilePath' | sort LastWriteTime | select -last 1 | Select-Object LastWriteTime | ft -hidetableheaders)"
And this...

Code: Select all

	$Var = Get-ChildItem 'C:|FilePath' | sort LastWriteTime | select -last 1 | Select-Object LastWriteTime | ft -hidetableheaders
	$textBox.Text = $var
All of these are giving me the same wrong output. When run in a standard powershell console I get the expected output which is the last modified time for the most currently modified file in the directory. But, When using any of the above I am getting this out put in the textBox (i have also tried a label and got the same results)

Code: Select all

Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData
Any help would be greatly appreciated
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Variable not displaying correctly in a text Box

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]

Try piping the output to Out-String:
  1. $textBox.Text = Get-ChildItem 'C:|FilePath' | sort LastWriteTime | select -last 1 | Select-Object LastWriteTime | ft -hidetableheaders | Out-String
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Variable not displaying correctly in a text Box

Post by jvierra »

The file path is wrong:

'C:|FilePath' - This is not a valid syntax.


Forget about format table and out string. You are asking for only one property so just use that property.

  1. $file = Get-ChildItem 'C:\FilePath' |
  2.     Sort-Object LastWriteTime |
  3.     Select-Object -last 1  
  4. $textBox.Text = $file.LastWriteTime
This topic is 6 years and 11 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