Hi guys, first post here
I'm running on PSS 2021
5.8.187
I have a question about whether or not it's possible to perform word wrap with a listbox. I've seen people explain with other languages using draw mode, but I can't quite figure out how to do this in Sapien.
Any help would be amazing, I really don't want to switch to richtext nor do I want to deal with a scroll bar.
Thanks!
Listbox with word wrap
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 forum is a space to discuss coding involving Graphical User Interfaces (GUI) in PowerShell or using WinForms controls, and technical issues related to development.
- Question about a licensed SAPIEN product? Post here: Product Support for Registered Users
- Question about a trial SAPIEN product? Post here: Former and Future Customers - Questions
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 forum is a space to discuss coding involving Graphical User Interfaces (GUI) in PowerShell or using WinForms controls, and technical issues related to development.
- Question about a licensed SAPIEN product? Post here: Product Support for Registered Users
- Question about a trial SAPIEN product? Post here: Former and Future Customers - Questions
Listbox with word wrap
First, set the listbox's DrawMode to OwnerDrawVariable. Then use the MeasureItem event to calculate the height of the items and the DrawItem event to actually draw the item.
Here is a rough example to get you started:
Go to full postHere is a rough example to get you started:
Code: Select all
$listbox1_MeasureItem=[System.Windows.Forms.MeasureItemEventHandler]{
if ($listbox1.Items -lt 1 -or $_.Index -lt 0)
{
return
}
$_.ItemHeight = [int]$_.Graphics.MeasureString($listbox1.Items[$_.Index].ToString(), $listbox1.Font, $listbox1.Width).Height
}
$listbox1_DrawItem = [System.Windows.Forms.DrawItemEventHandler]{
if ($listbox1.Items -lt 1 -or $_.Index -lt 0)
{
return
}
$_.DrawBackground()
$_.DrawFocusRectangle()
$textBrush = New-Object System.Drawing.SolidBrush $_.ForeColor
$_.Graphics.DrawString($listbox1.Items[$_.Index], $_.Font, $textBrush, [System.Drawing.RectangleF]$_.Bounds)
}
Re: Listbox with word wrap
[Topic was moved by moderator to PowerShell GUIs forum]
Brittney
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Listbox with word wrap
First, set the listbox's DrawMode to OwnerDrawVariable. Then use the MeasureItem event to calculate the height of the items and the DrawItem event to actually draw the item.
Here is a rough example to get you started:
Here is a rough example to get you started:
- $listbox1_MeasureItem=[System.Windows.Forms.MeasureItemEventHandler]{
- if ($listbox1.Items -lt 1 -or $_.Index -lt 0)
- {
- return
- }
- $_.ItemHeight = [int]$_.Graphics.MeasureString($listbox1.Items[$_.Index].ToString(), $listbox1.Font, $listbox1.Width).Height
- }
- $listbox1_DrawItem = [System.Windows.Forms.DrawItemEventHandler]{
- if ($listbox1.Items -lt 1 -or $_.Index -lt 0)
- {
- return
- }
- $_.DrawBackground()
- $_.DrawFocusRectangle()
- $textBrush = New-Object System.Drawing.SolidBrush $_.ForeColor
- $_.Graphics.DrawString($listbox1.Items[$_.Index], $_.Font, $textBrush, [System.Drawing.RectangleF]$_.Bounds)
- }
Brittney
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Listbox with word wrap
Thank you Brittney! You're awesome for that! It's exactly what I was trying to do, but correct
Re: Listbox with word wrap
Is there any reason the text would be invisible on some apps? This works fine on a vanilla app, but when I incorporate this into my more complex apps, the text is now invisible (though still selectable and actionable).
Re: Listbox with word wrap
Without seeing all the code, I can only make guesses as to what it going wrong, but it sounds like the issue is with DrawString. The example I gave most likely needs to be modified to work in your app.
Brittney
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Listbox with word wrap
I was able to get the initial code to show up (I forgot to update one of the listbox names), but once next phase (selecting one of the items), this runs...It will then provide all of the information about what I just clicked on (such as the name of the group I chose, as well as applications associated with that group. Nothing overly complex...this is the same issue I'm running into with other parts. The first section displays text, but once we get to stage 2,3,4,5 things stop working with the listbox (only an issue with the draw functions).
*some parts of the script have been removed for security reasons, but would not affect this post*
Any idea?
*some parts of the script have been removed for security reasons, but would not affect this post*
Any idea?
Code: Select all
$groupLookup = Invoke-RestMethod -Method Get -URI $SearchURL -ContentType "application/json" -UseBasicParsing -headers @{ "Authorization" = "SSWS $Token" }
foreach ($groupLookup in $groupLookup)
{
if ($groupLookup.profile.name -eq $Groupname)
{
$listbox7.Items.Clear()
Update-ListBox -ListBox $listbox7 -Items "Group name: $($groupLookup.profile.name)" -Append
Update-ListBox -ListBox $listbox7 -Items "Group ID: $($groupLookup.id)" -Append
Update-ListBox -ListBox $listbox7 -Items "" -Append
Update-ListBox -ListBox $listbox7 -Items "" -Append
$appLookup = Invoke-RestMethod -Method Get -URI $SearchURL -ContentType "application/json" -UseBasicParsing -headers @{ "Authorization" = "SSWS $Token" }
foreach ($app in $appLookup)
{
Update-ListBox -ListBox $listbox7 -Items "Associated Application: $($app.label)" -Append
}
$textbox_GroupSearch.text = "ID: $($groupLookup.id)"
}
}
Re: Listbox with word wrap
The issue might have to do with the empty strings you are appending to the listbox. I recommend setting a breakpoint in the DrawItem event and stepping through your script.
Brittney
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Listbox with word wrap
That's 100% the issue and something I do a lot! Is there a better way to put a blank line in a listbox?
Re: Listbox with word wrap
Fixed the issue by modifying your code.
Where you have listbox1.items -lt 1....
Updated that to listbox1.items.count -lt 1
That little bit fixed the issues I was having! Thanks again for all your help!
Where you have listbox1.items -lt 1....
Updated that to listbox1.items.count -lt 1
That little bit fixed the issues I was having! Thanks again for all your help!