Listbox with word wrap

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 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
This topic is 3 years and 8 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
pauladams
Posts: 11
Last visit: Tue Oct 22, 2024 5:36 am

Listbox with word wrap

Post by pauladams »

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!
by brittneyr » Fri Mar 19, 2021 11:14 am
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:

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)
}
Go to full post
User avatar
brittneyr
Site Admin
Posts: 1817
Last visit: Sat Nov 30, 2024 3:25 pm
Answers: 44
Been upvoted: 34 times

Re: Listbox with word wrap

Post by brittneyr »

[Topic was moved by moderator to PowerShell GUIs forum]
Brittney
SAPIEN Technologies, Inc.
User avatar
brittneyr
Site Admin
Posts: 1817
Last visit: Sat Nov 30, 2024 3:25 pm
Answers: 44
Been upvoted: 34 times

Re: Listbox with word wrap

Post by brittneyr »

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:
  1. $listbox1_MeasureItem=[System.Windows.Forms.MeasureItemEventHandler]{
  2.     if ($listbox1.Items -lt 1 -or $_.Index -lt 0)
  3.     {
  4.         return
  5.     }
  6.     $_.ItemHeight = [int]$_.Graphics.MeasureString($listbox1.Items[$_.Index].ToString(), $listbox1.Font, $listbox1.Width).Height
  7. }
  8.  
  9. $listbox1_DrawItem = [System.Windows.Forms.DrawItemEventHandler]{
  10.     if ($listbox1.Items -lt 1 -or $_.Index -lt 0)
  11.     {
  12.         return
  13.     }
  14.     $_.DrawBackground()
  15.     $_.DrawFocusRectangle()
  16.     $textBrush = New-Object System.Drawing.SolidBrush $_.ForeColor
  17.     $_.Graphics.DrawString($listbox1.Items[$_.Index], $_.Font, $textBrush, [System.Drawing.RectangleF]$_.Bounds)   
  18. }
Brittney
SAPIEN Technologies, Inc.
pauladams
Posts: 11
Last visit: Tue Oct 22, 2024 5:36 am

Re: Listbox with word wrap

Post by pauladams »

Thank you Brittney! You're awesome for that! It's exactly what I was trying to do, but correct :D
pauladams
Posts: 11
Last visit: Tue Oct 22, 2024 5:36 am

Re: Listbox with word wrap

Post by pauladams »

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).
User avatar
brittneyr
Site Admin
Posts: 1817
Last visit: Sat Nov 30, 2024 3:25 pm
Answers: 44
Been upvoted: 34 times

Re: Listbox with word wrap

Post by brittneyr »

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.
pauladams
Posts: 11
Last visit: Tue Oct 22, 2024 5:36 am

Re: Listbox with word wrap

Post by pauladams »

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?

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)"
				}
			}
User avatar
brittneyr
Site Admin
Posts: 1817
Last visit: Sat Nov 30, 2024 3:25 pm
Answers: 44
Been upvoted: 34 times

Re: Listbox with word wrap

Post by brittneyr »

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.
pauladams
Posts: 11
Last visit: Tue Oct 22, 2024 5:36 am

Re: Listbox with word wrap

Post by pauladams »

That's 100% the issue and something I do a lot! Is there a better way to put a blank line in a listbox?
pauladams
Posts: 11
Last visit: Tue Oct 22, 2024 5:36 am

Re: Listbox with word wrap

Post by pauladams »

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!
This topic is 3 years and 8 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