Output data to 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.
Locked
User avatar
fair64
Posts: 5
Last visit: Tue Jul 29, 2014 10:27 am
Been upvoted: 1 time

Output data to listbox

Post by fair64 »

I am having problems trying to display data in a listbox (data populates in the richtextbox correctly). This is the code I am using, I am sure its an easy fix, thanks in advance :

$roles = Get-ADDomain -Server dc2 | select InfrastructureMaster, PDCEmulator, RIDMaster | Out-String

$listbox1.Items.Add($roles)
$richtextbox1.Text = $roles

jvierra
Posts: 14781
Last visit: Sat May 08, 2021 8:07 am
Answers: 9
Has voted: 3 times
Been upvoted: 9 times

Re: Output data to listbox

Post by jvierra »

A listbox takes multiple items. You are trying to put one object into the listbox. That doesn't make any sense.

What is it you are trying to display?

User avatar
fair64
Posts: 5
Last visit: Tue Jul 29, 2014 10:27 am
Been upvoted: 1 time

Re: Output data to listbox

Post by fair64 »

Nothing in particular just testing to see how and what types of data can be output to to different objects. Learning...

User avatar
fair64
Posts: 5
Last visit: Tue Jul 29, 2014 10:27 am
Been upvoted: 1 time

Re: Output data to listbox

Post by fair64 »

So for example if I am building an AD tool with multiple functions like listing all expired AD accounts and am using the listbox to display the results I should be able to or would like to be able to display the results of other queries like the one in question in the same listbox or would i need to utilize a RichTextBox for that?

jvierra
Posts: 14781
Last visit: Sat May 08, 2021 8:07 am
Answers: 9
Has voted: 3 times
Been upvoted: 9 times

Re: Output data to listbox

Post by jvierra »

You will never successfully guess you way through both PowerShell and Windows Forms. It requires concentrated study.

Start by reading the blog posts on forms:

http://www.sapien.com/blog/topics/user- ... istrators/

There are numerous other sets of blog posts on controls and design.

User avatar
fair64
Posts: 5
Last visit: Tue Jul 29, 2014 10:27 am
Been upvoted: 1 time

Re: Output data to listbox

Post by fair64 »

I appreciate your feedback but thats a little presumptuous that I am just guessing my way through PS. Actually I have read many tuorials and have learned alot. I have searched for an answer before asking the question since I was unable to find the answer I figured I would ask here. A better response would have been that it can not or should not be done this way and than show the correct way. Was just curious if it could be done.

Thanks

jvierra
Posts: 14781
Last visit: Sat May 08, 2021 8:07 am
Answers: 9
Has voted: 3 times
Been upvoted: 9 times

Re: Output data to listbox

Post by jvierra »

No - you have not read about how to use Windows Controls. If you had you would know that a listbox is a collection of objects saved as items in a list control. You would also know that your AD query does not return a list but a single object that cannot be displayed as list.

I highly recommend reading the posts on how to use Forms to design Admin forms. It is exactly what you are asking to know. I cannot explain it any better than the blog post and the blog post has many examples of how to code it.

The simple answer to your question is that you can add any collection of objects that support the IList interface to a listbox. In PowerShell Studio this loosely equates to anything that can be produces as a dataset or as an System.Collections.Arraylist.

You can also just add strings one at a time.
PowerShell Code
Double-click the code block to select all.
$listbox1.Items.Add('some string')

$files=dir $env:userprofile\Documents -file
$listbox1.Items.AddRange([System.Collections.Arraylist]$files)
Not having any idea what you are trying to do makes it nearly impossible to know what to show you. Please do the blog posts until you understand what the issues are.

User avatar
fair64
Posts: 5
Last visit: Tue Jul 29, 2014 10:27 am
Been upvoted: 1 time

Re: Output data to listbox

Post by fair64 »

Whatever jvierra, if i read everything and knew everything i wouldn't have to ask questions. Its responses like yours that turn new users off. I'm glad I don't react that way when someone asks about VMWare, I try to spread the knowledge that I have in my field of expertise. In my way of thinking there is no stupid question if you are trying to learn, obviously you don't see it the same way. Like I said I have gone through many tuts but haven't done it all. Life is about questions and if you stop asking questions your not learning. Thanks Again. No hard feelings, I will keep reading , learning and asking questions.

jvierra
Posts: 14781
Last visit: Sat May 08, 2021 8:07 am
Answers: 9
Has voted: 3 times
Been upvoted: 9 times

Re: Output data to listbox

Post by jvierra »

Like I posted. I have no idea what you are asking. What is it that you need to know that I haven't covered or that is not covered in the blog posts.

Locked