Create list from items in combobox

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
User avatar
Chris.Ediger
Posts: 42
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Tue Oct 24, 2017 9:33 am

Create list from items in combobox

Post by Chris.Ediger »

I want to take items selected from a combobox, in this case active directory users, and put them into a list. I then want to take the items in that list (of users) and then do something with them, in this case add them to an AD group.
I'm able to build the form and get the basics up and running. I've even been able to add my users to a richtextbox but the user names end up all bunched together and so I can't do anything with the list. I think I'd like to be able to take every user in my list and do a foreach($user in $richtextboxUser){Add-ADGroupMember $user} something like that.
How can I take content from the combobox and make a usable list?

Thanks!

jvierra
Posts: 14438
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Create list from items in combobox

Post by jvierra »

Don't use an RTB. Place users in a ListBox.

A list box can be multi-selected and you can just enumerate the selected items.

To learn the basics of designing with forms the following articles will help.

https://info.sapien.com/index.php/guis/ ... nistrators

https://info.sapien.com/index.php/guis

User avatar
Chris.Ediger
Posts: 42
Joined: Tue Oct 24, 2017 9:33 am

Re: Create list from items in combobox

Post by Chris.Ediger »

Ok, I see that. How do I get the name in the combobox to the listbox? I have an "Add" button that does this $listbox.Text = $combobox.Text but nothing gets copied to the list box.

jvierra
Posts: 14438
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Create list from items in combobox

Post by jvierra »

$listbox.Items.Add(<item to add>)

User avatar
Chris.Ediger
Posts: 42
Joined: Tue Oct 24, 2017 9:33 am

Re: Create list from items in combobox

Post by Chris.Ediger »

Cool. Didn't know I could do that. Thanks!

User avatar
Chris.Ediger
Posts: 42
Joined: Tue Oct 24, 2017 9:33 am

Re: Create list from items in combobox

Post by Chris.Ediger »

Is there a way to remove specific items from the listbox? Say I wanted to remove a user from the list. Can I select that user and right click/remove?

jvierra
Posts: 14438
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Create list from items in combobox

Post by jvierra »

Chris.Ediger wrote:
Fri Apr 03, 2020 2:09 pm
Is there a way to remove specific items from the listbox? Say I wanted to remove a user from the list. Can I select that user and right click/remove?
Yes. You can write code that will add an event for the mouse click events.

Take some time to review the links I posted. Most of these questions are answered in the articles in the info center.

User avatar
Chris.Ediger
Posts: 42
Joined: Tue Oct 24, 2017 9:33 am

Re: Create list from items in combobox

Post by Chris.Ediger »

Ok, I've mentioned this before but I'm pretty new to this programming thing and I'm really learning as I go. I'm not trying to have someone write code for me but I really don't even know what question to ask or what to search for. I appreciate that you gave me a couple links to information but that doesn't really tell me what to look for.

jvierra
Posts: 14438
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Create list from items in combobox

Post by jvierra »

I understand your frustration. I can try to give you a context from which to work but you will still have to put in the effort to learn what you are missing.

Without a good understanding of PowerShell working with forms will be nearly impossible. Even for trained and experiences programmers learning forms is a challenge.

First take teh time to learn PowerShell basics until you are completely secure in the syntax and behavior of PS and the basics of programming. Her eis a link to a book (free) that will get you there quickly. Just read each chapter and do the examples and exercises until you understand the chapter then move on. The book is very well written and is designed to teach Windows admins and techs the basics of programming with PowerShell. It is written by two senior admins who have extensive PS experience.

https://www.sapien.com/books_training/W ... werShell-4

After you have a good basic understanding of programming with PowerShell then you can learn how forms work and how they are designed to be used from this link.

https://docs.microsoft.com/en-us/dotnet ... /winforms/

You can refer to the articles on this site that will help you to understand how to work with controls using PS and PowerShell Studio.

I understand that knowing what to ask is a challenge. There is no simple answer. Without some basic understanding you will not know what to ask. We cannot guess at what you are trying to do. To make matters worse nearly all who come to forms with littel PowerShell experience and no knowledge of what forms are get lost almost immediately. There is no easy answer.

Forms are not things like you find in Word or Excel. They are objects that have been designed to help programmers build standardized forms. Forms design standards have been around since before Windows. WinForms were designed to support the industry standards and to simplify a programmers ability to build forms.

The following links may help you to discover why we have forms.

https://en.wikipedia.org/wiki/IBM_Common_User_Access
http://www.susandoreydesigns.com/softwa ... Design.pdf

WinForms were designed to support CUA because companies wanted and understood that the research done into this improved data entry efficiency. Microsoft committed to this and has actually done much to expand the CUA concepts. The latest surfacing of this is in WPF (Windows Presentation Foundation) which you will see referred to as XAML. It continues this thinking and adds even more depth and flexibility while allowing support for CUA principles.

At a simple and more practical level you will have to learn what a form is and then what a control is. All controls are inherited from "Control" which is where the adventure begins:

See: https://docs.microsoft.com/en-us/dotnet ... mework-4.8

To understand how to interact with controls you will need to learn about events and about how to use control properties. You will need to learn to look up the properties in the documentation. To that end PowerShell Studio provides a great set of tools to help find the controls documentation and articles written to show how to use the controls with PowerShell. YOU wil lalso have to learn what an event is and how it is implemented in forms and how events are used.

I would suggest that you start learning forms by doing teh examples in the articles linked. As you work with the examples you will gain and things will get better.

There are also many videos here: https://www.youtube.com/user/SAPIENTech/videos

User avatar
Chris.Ediger
Posts: 42
Joined: Tue Oct 24, 2017 9:33 am

Re: Create list from items in combobox

Post by Chris.Ediger »

Awesome. Thanks for all that. I have a pretty good grasp of PowerShell it's the forms stuff that I'm out of my depth with.

Locked