Author Posts

January 17, 2018 at 2:54 pm

Ok, I have a form that has a text box and a label on it 🙂 Now I know that I could just hard code the label and text box text, but what I would like to do is use an XML config file to do this for me. This way when the form is reused only the XML will need changing and not the ps script.

I am currently using :-

[xml]$display = Get-Content C:\test\config.xml

To read the contents of the XML into $display and I can reference the text for the text box with

$display.Settings.FormSettings.TextBody

But this just puts the text on one line so the formatting is not very easy to read and also does not look very nice. What I would like to do is keep the carriage returns and tabbing as in the XML file I have tried in the XML but it is not working. Any ideas ??

This is the form label

What I would like is for this text to be
on Multilines as I have typed them
here

January 17, 2018 at 2:56 pm

This is the form label

What I would like is for this text to be
on Multilines as I have typed them
here

January 17, 2018 at 2:58 pm

This is a pretty standard challenge with GUIs, and you mainly run across it when creating a GUI that will display strings in different languages. There's no easy solution. You either need to do some wildly complex math to calculate label size, or size the label to hold the largest possible string. Labels also do not support rich formatting – they will not display carriage returns, tabs, or other white space; that's not what they're designed to do. Your closest approximation would be a multi-line, non-editable (read-only) text box, but that's a pretty nonstandard use of that control so it won't provide a consistent GUI experience. You could probably set the border style to make the text box look more like a label, I suppose. This just isn't a use case that the Windows GUI standards cover.

An XML file also isn't the only way to do this; PowerShell's native "data sections" was designed for this use, particularly in localization. See https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_data_sections?view=powershell-5.1; they're easier to read than XML, if that's useful for you.

January 17, 2018 at 2:59 pm

Also, avoid being the first person to type a reply to a thread you started. It removes your post from the "posts with no replies" list, which a lot of us use to find questions that need answering. 🙂

January 18, 2018 at 2:42 pm

Instead of using Winforms I would suggest using WPF instead. With WPF you usually data-bind items from the view to code (this makes translating parts of the GUI with ease since you bind the GUI text or label to a property : by changing the property value you change the value in the GUI.).
I've written multiple GUIs in the past using WPF with translation features.

see https://smsagent.wordpress.com/2017/02/03/powershell-deepdive-wpf-data-binding-and-inotifypropertychanged/ for more info.

January 19, 2018 at 8:37 am

Thanks for the link data binding looks like a good way to go 🙂