Help with refreshing items in a 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.
This topic is 3 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
powershell79
Posts: 5
Last visit: Thu May 14, 2020 8:24 am

Help with refreshing items in a combobox

Post by powershell79 »

good day to you all,

I am currently building a Vsphere VM building tool. I have a datastore dropdownbox that lists the datastores and free space (see attached).

The problem is if the space in the datastores change, this isn't reflected in the combobox unless i re-open the tool.

This is the code. Apologies if the format is incorrect:

Code: Select all

############# gets list of datastores and their free disk space, then adds to datastore dropbox as a list ###################

function list-datastore {

    

    $list=  Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending

 
foreach ($item in $list) {

  

    $displayThis =  "{0} == {1:N2}" -f $item.Name, $item.FreeSpaceGB + " GB Free"  
    
    
   [void]$datastoredropdownbox.Items.Add($displayThis);
   
  
    
    [void]$datastorecomboboxtemplate.items.add($displayThis) # Populates List Of Datastores For Template Datastoredropdownbox
    
}

}
 list-datastore


I have tried using $datastoredropdownbox.items.clear() and refresh in various places in that code and it doesn't seem to make any difference. The free space stays the same.

I am extremely grateful if somebody could help as i am banging my head against a wall. Thank you so much
Attachments
Annotation 2020-05-13 133727.png
Annotation 2020-05-13 133727.png (67.66 KiB) Viewed 2017 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with refreshing items in a combobox

Post by jvierra »

You would have to re-query the datastores to update the values.
The function you have posted must both clear the controls and then add the re4fershed items.

Code: Select all

function list-datastore {
    $datastoredropdownbox.Items.Clear()
    $datastorecomboboxtemplate.items.Clear()
    $list =  Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending
    foreach ($item in $list) {
        $displayThis =  "{0} == {1:N2}" -f $item.Name, $item.FreeSpaceGB + " GB Free"  
        [void]$datastoredropdownbox.Items.Add($displayThis)
        [void]$datastorecomboboxtemplate.items.add($displayThis) # Populates List Of Datastores For Template Datastoredropdownbox
    }
}
list-datastore
Whenever you want to refresh the lists just call the function.
User avatar
powershell79
Posts: 5
Last visit: Thu May 14, 2020 8:24 am

Re: Help with refreshing items in a combobox

Post by powershell79 »

Thank you for your prompt reply jvierra. I will try this once i get home from work tonight and report back.
Fingers crossed this works!.

Again thank you for your time.
User avatar
powershell79
Posts: 5
Last visit: Thu May 14, 2020 8:24 am

Re: Help with refreshing items in a combobox

Post by powershell79 »

I did exactly what you suggested and added the lines at the start of the list-datastore function.
$datastoredropdownbox.Items.Clear()
$datastorecomboboxtemplate.items.Clear()

Then deleted a VM from datastore 3 and clicked the datastore dropdown, but it still shows 13.13GB, even though the space free is now 33GB.

It still doesn't look like the dropdownbox is updating with the latest information unless i restart the app.

Any Ideas?. Thanks again for your input
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with refreshing items in a combobox

Post by jvierra »

I don't think you understand how computers and code wqork. Clicking a drop down does not execute your code., You have to rerun the function any time you want to update the list. It cannot happen without something that causes the function to be executed.
User avatar
powershell79
Posts: 5
Last visit: Thu May 14, 2020 8:24 am

Re: Help with refreshing items in a combobox

Post by powershell79 »

i was thinking previously of adding a refresh button next to datastores and using an addclick which calls list-datastore, which is what i presume you mean along the lines of?

I would rather not have more buttons on the GUI to keep it clean and hoping that i could refresh that combobox using another method. I am stuck currently and trying to think of other ways. You're right i'm not a coder i am a server infrastructure engineer, and although i can do powershell i am currently a bit stuck on this last part.

Again thank you for your time
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with refreshing items in a combobox

Post by jvierra »

If you are an engineer I would think that you have learned that the state of a system can only be chanted by an event. You need an event. What event is it that tells you that the list contents have changed?

Most CmdLets have no way of telling when the data has changed. A few objects returned by a CmdLet may be able to raise a changed event. Do your objects raise any events? Also, when you extract parts of an object using the "Select-Object" command you recreate the objects as custom objects with no methods, events or connections to a source if there is one.

All of this should tell you that you need to create some event that can rerun the function.
Normally we would use events on a control to intercede when the control is activated. The other main method would be to use a timer to periodically provide an event.

In the end you have to see a form as an event driven system and design your solution around these events. This is the hardest thing for experienced non-windows programmers and inexperienced coders to understand. Once this is learned about forms and once any technician learns enough about computer science and engineering to understand how a computer system works then all of this becomes very obvious and easier to work with.

Here is one place to start to get a handle on Windows forms, how they are designed to work by Microsoft and how the forms classes and controls are structured to make coding simple and easy.

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

The following is an example of what you will learn by learning what forms are and how they work. I use the "DropDown" event of the ComboBox to refresh its contends. This would work if the query is fast. It would not be a good solution if the query is slow.

Code: Select all

$datastoredropdownbox_DropDown = {
    [System.Collections.ArrayList]$datastoredropdownbox.DataSource = Get-Datastore | 
        Select-Object Name,FreeSpaceGB,@{n='Items';e={'{0} == {1:N2}GB Free' -f $item.Name, $item.FreeSpaceGB}}
        Sort-Object -Property FreeSpaceGB -Descending
    $datastoredropdownbox.DisplayMember = 'Item'
}
In forms based design in any system starting with early IBM mainframes with text based forms terminal there have been rules on how to use controls to build humanly usable forms. This technology has been around since the beginning and is the foundation of the controls in Microsoft WinForms. Each control relates to a fundamental understanding of how forms need to be deployed and how they need to behave to accomplish data entry and navigation tasks.

If you attempt to design forms by pure guesswork it will put you in conflict with the underlying technology. The technology is not intuitive for non-computer-trained people. YOU must acquire some basic understanding of the intended use of the forms and controls.

A place to start learning is also from the articles written by Sapien people that describe elements of forms design and control usage.

https://info.sapien.com/index.php/guis/ ... -in-events

https://info.sapien.com/index.php/guis/gui-scripting

Explore the whole info center for hundreds of examples and good guidance for building forms.
User avatar
powershell79
Posts: 5
Last visit: Thu May 14, 2020 8:24 am

Re: Help with refreshing items in a combobox

Post by powershell79 »

I went away and created a refresh button underneath the datastore dropdown.

I used an addclick which calls the list-datastore and this works. I did have that in mind previously but was thinking if i could do this using another method automaticallyand avoiding this, but this will have to do and achieves what i need.

Thanks again.
This topic is 3 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked