[SOLVED]Select multiple lines by code in a listbox

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.
Post Reply
User avatar
zztemp
Posts: 31
Joined: Mon Apr 03, 2017 6:58 am
Answers: 1
Has voted: 1 time

[SOLVED]Select multiple lines by code in a listbox

Post by zztemp »

So i have a listbox that has a set of AD-group objects.
Then i have a "memberof" list comming from a computer object.
When rendering the listbox, i'd like to select the groups that match.

I've build myself an example.
  1. # build list with groups into listbox1
  2.  
  3. $groups = get-adgroup -LDAPFilter "(name=gc08*)" | Select name
  4.  
  5. foreach ($g in $groups){
  6.  
  7.         $listbox1.Items.Add($g.Name)
  8.  
  9. }
  10.  
  11.  
  12.  
  13. #mock memberof groups
  14.  
  15. $searchstrings = @(
  16.  
  17.     "*informatica*",
  18.  
  19.     "*HR*",
  20.  
  21.     "*Marketing*"
  22.  
  23. )
  24.  
  25.  
  26.  
  27. #Pre-select current groups
  28.  
  29. foreach ($searchstring in $searchstrings){
  30.  
  31.     for ($i = 0; $i -lt $listbox1.Items.Count; $i++)    {
  32.  
  33.         $p = $listBox1.Items[$i].ToString()
  34.  
  35.         if ($p -like $searchstring)     {
  36.  
  37.             Write-Host "Match found with filter $($searchstring):" $listBox1.Items[$i]
  38.  
  39.             $listBox1.SetSelected($i, $true);
  40.  
  41.         }
  42.  
  43.     }
  44.  
  45. }
This sorta works, in the console it prints out my matches, but it only sets one value as selected.
I have set the SelectionMode to MultiExtended but that made no difference.
Anyone can help me out?
by zztemp » Thu Jan 07, 2021 12:33 am
Ok that's my bad, i'll try to clarify.

I have computer1 and i retrieve it's groupmembership. It is member of Group 1 and Group 2
I retrieve these groups by selecting the computer from a list.
Upon selecting that computer, a second listbox gets filled, it holds all possible groups.

What i want to do is, when that listbox gets filled, it should already select Group 1 and Group 2.

Edit, i found a solution, i've edited my code, maybe it might help someone some day.
Go to full post
Last edited by zztemp on Thu Jan 07, 2021 1:03 am, edited 3 times in total.

jvierra
Posts: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: Select multiple lines by code in a listbox

Post by jvierra »

I am sorry but your request is not very clear. I cannot see or understand what the purpose of your code is or what you mean by your question.

To find select3ed items in a ListBox you need to look at the items that are in the "SelectedItems" property of the ListBox.

Here is a good article to help you learn the basics of using controls in a form.

https://info.sapien.com/index.php/guis/ ... nistrators
https://info.sapien.com/index.php/guis/ ... ox-control

User avatar
zztemp
Posts: 31
Joined: Mon Apr 03, 2017 6:58 am
Answers: 1
Has voted: 1 time

Re: Select multiple lines by code in a listbox

Post by zztemp »

Ok that's my bad, i'll try to clarify.

I have computer1 and i retrieve it's groupmembership. It is member of Group 1 and Group 2
I retrieve these groups by selecting the computer from a list.
Upon selecting that computer, a second listbox gets filled, it holds all possible groups.

What i want to do is, when that listbox gets filled, it should already select Group 1 and Group 2.

Edit, i found a solution, i've edited my code, maybe it might help someone some day.

Post Reply