Add text to Rich Textbox in Powershell - wpf

I am working on a GUI for our office and I have run across an issue, the GUI works wonderfully if you don't have to output text to the GUI. I currently have a Rich Textbox to take the output and display it but it isn't displaying.
$WPFbtnDisk.Add_Click({
start-sleep -Milliseconds 840
write-host "Disk Clicked"
$WPFRichtextbox =gwmi win32_service -ComputerName ($WPFtxtServerName.Text) | sort DisplayName | select-object SystemName,DisplayName,StartMode,State,PathName | Out-String
})
This is the code that I have right now that according to what I read should load the box but it doesn't, if I change it to just a normal textbox it outputs but it just doesn't have any formatting so it is next to impossible to read.
Any help is appreciated.

The RichTextBox control does not have a Text field like a normal textbox, but a Document property which contains a reference to a FlowDocument that makes up the contents of the RichTextBox.
In lieu of creating and maintaining a FlowDocument, you can add text to the existing empty document with the RichTextBox.AppendText() method:
$WmiOutputTable = Get-WmiObject Win32_Service -ComputerName $WPFtxtServerName.Text |Select-Object SystemName,DisplayName,StartMode,State,PathName |Sort-Object DisplayName |Out-String
$WPFRichtextbox.AppendText($WmiOutputTable)

Related

WPF GUI in Powershell - Textbox not being updated until the end of execution

Im creating a simple powershell GUI using WPF like this one...
WPF GUI to powershell
The problem is that the textbox where i show logs is not being updated while the powershell script is executing some commands.
The same function that updates the text of the textbox also logs to standard output, but the messages are shown in the stdout immediately whereas the ones on the textbox are not shown until the end.
$MyTextbox.AddText($LogsLine) -> Not shown until the end
Write-Host ($LogsLine) -> Shown immediately
Is there any way to refresh the textbox after updating its "text" value?
Thanks!
I have tried looking for methods of the textbox or the form itself but i do not find a refresh function.

Add text to TextBox during ShowDialog and Add_ContentRendered

When loading my PowerShell WPF script, I want some information shown to the user that the script is loading multiple things from Active Directory.
As my script is now, the Window is unresponsive during the loading (which takes aprox 20 seconds), and then when everything is done, the TextBox is updated with the four lines that adds text to the TextBox. I want it to do task 1, update TextBox, then Task 2, update the TextBox and so on. How can I accomplish this?
In the Add_ContentRendered block I have four $WPFlogTxtBox.AddText("Some text"), but these are displayed in the $WPFlogTxtBox all at once when the Add_ContentRendered block is finished, not when they show up in the script.
XAML contains:
<TextBox x:Name="logTxtBox" HorizontalAlignment="Left" Height="112" Margin="10,290,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="496" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"/>
Rest of the script condensed:
$Form.Add_ContentRendered({
$WPFlogTxtBox.AddText("Getting OUs")
$OUs = Get-ADOrganizationalUnit -Filter '*' | sort Name
$WPFlogTxtBox.AddText("Getting users")
$users = Get-ADUser -Filter * | sort Name
$WPFlogTxtBox.AddText("Getting groups")
$groups = Get-ADGroup -Filter '*' | sort Name
})
$Form.ShowDialog()
This works, besides that the text is added to TextBox when everything in Add-ContentRendered is done.
I want the AddText-method to add the text when the command is run, not when the whole invoking block (Add_ContentRendered) is done.
Basically, you need to yield the thread to let the window repaint ...
$WPFlogTxtBox.Dispatcher.Yield("ApplicationIdle");
A better solution would be to do the work on a different thread, and call into the control's dispatcher to update it. Show-UI does this sort of thing for you 😉 but you can do it yourself by spinning up another PowerShell runspace -- that's a bit much to get into here. Look at the ShowUI code on github, or at blog posts from Boe Prox or Stephen Owen

How to style Windows Form Labels

I am writing a (purely) PowerShell app to display numbers in a table using Windows Forms
My thought is to create rows of fixed-size labels (or text boxes), which works out just fine. What I am having trouble with is figuring how to make those numbers right-justified within each label field. I see that using XAML there is a "HorizontalContentAlignment" but I can't seem to see the equivalent property in PowerShell. I see there is TextAlign property but that seems to be for Vertical alignment, rather than Horizontal.
Of course, I am open to doing something else (other than table of labels) that'll do the same thing.
Thanks.
Well, You can use the enumerations from System.Drawing.ContentAlignment
$Label = New-Object -TypeName System.Windows.Forms.Label
$Label.Text = 'SomeText'
$Label.TextAlign = [System.Drawing.ContentAlignment]::MiddleRight
How did I find the peroperty ?
$Label | Get-Member -MemberType Property | Where-Object -FilterScript {$_.name -like "*ali*"}
Since you are writing an app only with Powershell, I'd think it would be a little weird using Win Forms just for displaying information.
I'd suggest checking out POSHGUI (https://poshgui.com/Editor) which helps you generate win forms style GUI for you're Powershell code.
In addition, Powershell has Out-Gridview, which could be sufficient to your need if I got the idea of what you're trying to do.

PowerShell Form: Sometimes starts in the background

I wrote a little GUI for a script that I use on daily basis. Sometimes the window starts in the background behind all other windows I have opened. In this case there will even be no icon in the task bar. This phenomenon looks like the script is not started. When I minimize all other windows I can see the script GUI and after clicking into it the icon on the task bar appears.
Here is how I defined the PowerShell form:
[void] [system.reflection.assembly]::loadwithpartialname("system.windows.forms")
[void] [system.reflection.assembly]::loadwithpartialname("system.drawing")
$window = New-Object System.Windows.Forms.Form
$window.StartPosition = "CenterScreen"
$window.AutoSize = $False
$window.MinimizeBox = $False
$window.MaximizeBox = $False
$window.FormBorderStyle = "FixedToolWindow"
[...]
$window.ShowDialog() | Out-Null
Does anyone know what I can do to ensure that the GUI is always started on top?

Non-breaking characters in WinForms labels?

In WinForms, is there a way to add a non-breaking hyphen to a System.Windows.Forms.Label control?
e.g. Don't break like this:
======================
| VB is SO un- |
| cooperative! |
======================
Instead break like this!
======================
| VB is SO |
| un-cooperative! |
======================
If you want to use the Forms Designer you could select the Label's Text property. Click on the dropdown arrow on the far right and type your text. You could break it at the desidered position by pressing SHIFT+ENTER.
In code it is simply
label1.Text = "VB is SO\nun-cooperative!";
or in VB.NET
label1.Text = "VB is SO" & Environemnt.NewLine & "un-cooperative!"
Check this, its working properly
text does not break after or before '-'. i have tested in my testing project and its working properly. even i reduce the width of label it get cut from the right side as displayed in my screenshot.
still getting problem then you have another option is that create custom user control for label and write some code for word wrap there are lot of code exist on internet for wordwrap process. available in VB.net also. btw windows form does not allow any event or override method that you can handle it for wrap text.

Resources