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.
Locked
zsirkin
Posts: 11
Last visit: Fri Sep 10, 2021 6:36 am

Listbox with word wrap

Post by zsirkin »

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:
  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. }
Go to full post

User avatar
brittneyr
Site Admin
Posts: 853
Last visit: Fri Sep 17, 2021 12:31 pm
Answers: 17
Been upvoted: 15 times

Re: Listbox with word wrap

Post by brittneyr »

[Topic was moved by moderator to PowerShell GUIs forum]
Brittney Ryn
SAPIEN Technologies, Inc.

User avatar
brittneyr
Site Admin
Posts: 853
Last visit: Fri Sep 17, 2021 12:31 pm
Answers: 17
Been upvoted: 15 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 Ryn
SAPIEN Technologies, Inc.

zsirkin
Posts: 11
Last visit: Fri Sep 10, 2021 6:36 am

Re: Listbox with word wrap

Post by zsirkin »

Thank you Brittney! You're awesome for that! It's exactly what I was trying to do, but correct :D

zsirkin
Posts: 11
Last visit: Fri Sep 10, 2021 6:36 am

Re: Listbox with word wrap

Post by zsirkin »

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: 853
Last visit: Fri Sep 17, 2021 12:31 pm
Answers: 17
Been upvoted: 15 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 Ryn
SAPIEN Technologies, Inc.

zsirkin
Posts: 11
Last visit: Fri Sep 10, 2021 6:36 am

Re: Listbox with word wrap

Post by zsirkin »

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: 853
Last visit: Fri Sep 17, 2021 12:31 pm
Answers: 17
Been upvoted: 15 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 Ryn
SAPIEN Technologies, Inc.

zsirkin
Posts: 11
Last visit: Fri Sep 10, 2021 6:36 am

Re: Listbox with word wrap

Post by zsirkin »

That's 100% the issue and something I do a lot! Is there a better way to put a blank line in a listbox?

zsirkin
Posts: 11
Last visit: Fri Sep 10, 2021 6:36 am

Re: Listbox with word wrap

Post by zsirkin »

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!

Locked