Page 1 of 1

Listbox with word wrap

Posted: Fri Mar 19, 2021 10:01 am
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!

Re: Listbox with word wrap

Posted: Fri Mar 19, 2021 10:20 am
by brittneyr
[Topic was moved by moderator to PowerShell GUIs forum]

Re: Listbox with word wrap

Posted: Fri Mar 19, 2021 11:14 am
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. }

Re: Listbox with word wrap

Posted: Fri Mar 19, 2021 1:52 pm
by zsirkin
Thank you Brittney! You're awesome for that! It's exactly what I was trying to do, but correct :D

Re: Listbox with word wrap

Posted: Mon Mar 22, 2021 9:02 am
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).

Re: Listbox with word wrap

Posted: Mon Mar 22, 2021 9:33 am
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.

Re: Listbox with word wrap

Posted: Mon Mar 22, 2021 11:46 am
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)"
				}
			}

Re: Listbox with word wrap

Posted: Mon Mar 22, 2021 12:34 pm
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.

Re: Listbox with word wrap

Posted: Tue Mar 23, 2021 6:05 am
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?

Re: Listbox with word wrap

Posted: Tue Mar 23, 2021 8:11 am
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!