none
Winform application Add data to list view after certian variable contains data RRS feed

  • Question

  • I'm just learning Winform application, is it possible to add data to a list view after a certain variable contains data ?

    I have a function, which gets info from ConfigMgr when I press on a button.

    After I press on that button, some info will be stored in a variable called $Results.
    I want list view to wait until the variable contains data, and then load my function, that will add data to my list view - is that possible ?

    It's working if I don't clear the variable, and run it a second time, because then the variable is not empty
    by doing

    $Form.Add_Shown( { $Form.Activate(); Results })

    Is there an equivalent method to achieve what I want ?

    Monday, August 26, 2019 7:13 PM

Answers

  • It was just too see if it was working or not, because I don't know what event to use - that's why i'm asking for help.

    Until you know what you want just place the code in a button.

    The first time "Shown" executes the variable will not exisit.


    \_(ツ)_/

    • Marked as answer by B.R1984 Tuesday, August 27, 2019 3:12 PM
    Monday, August 26, 2019 8:59 PM
  • No.  Put move the 'Shown" code to a button.


    \_(ツ)_/

    • Marked as answer by B.R1984 Tuesday, August 27, 2019 3:12 PM
    Monday, August 26, 2019 9:07 PM

All replies

  • To add items to a ListView use ListView.Items.Add($item).

    You question is far too vague to understand what you are asking.  Please try to ask a specific question. 

    For how to use a ListView see: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.items?view=netframework-4.8

    See: https://info.sapien.com/index.php/guis/gui-controls/spotlight-on-the-listview-control

    Search for blogs that show how to use the ListView control.


    \_(ツ)_/

    Monday, August 26, 2019 7:29 PM
  • I know how to add to the list view, I got that part working.

    What I want to know, is how to run my function that add to list view, once a variable contains any data.
    That variable Will feed my function with items to add to my list view.

    currently i have it working by doing:

    $Form.Add_Shown( { $Form.Activate(); Results })
    But it's only working because the $result variable is not empty from the last run

    Monday, August 26, 2019 8:22 PM
  • Again - your question is far too vague.  What does "once a variable contains any data." mean?  What variable?  What does it have to do with a ListView? Why do you need a variable when the ListView is what you are trying to update.

    There is just no way to understand what you are trying to ask.


    \_(ツ)_/

    Monday, August 26, 2019 8:36 PM
  • $Results contains the data, that I put in my listview, $Results is invoked when I press on a button on my winform, that button press activate a function, that gets some info about driverpackages from a ConfigMgr server.

    I want the listview control to wait until $Results contains data, before it will start building it

    this is my function: 

    Function Results {
    
        #Get objects stored in $Results
        $listview_NotExists_SKU_Results = $Results | Select-Object Name,ID
        
        
    
        # Compile a list of the properties 
        $listview_NotExists_SKU_Properties = $listview_NotExists_SKU_Results[0].psObject.Properties
     
        # Create a column in the listView for each property
        $listview_NotExists_SKU_Properties | ForEach-Object {
            $listview_NotExists_SKU.Columns.Add("$($_.Name)") 
        }
     
        # Looping through each object in the array, and add a row for each
        ForEach ($listview_NotExists_SKU_Result in $listview_NotExists_SKU_Results) {
     
            # Create a listViewItem, and assign it it's first value
            $listview_NotExists_SKU_Item = New-Object System.Windows.Forms.ListViewItem($listview_NotExists_SKU_Result.Name)
     
            # For each properties, except for 'Id' that we've already used to create the ListViewItem,
            # find the column name, and extract the data for that property on the current object/Tasksequence
            $listview_NotExists_SKU_Result.psObject.Properties | Where-Object { $_.Name -ne "ID" } | ForEach-Object {
                $listview_NotExists_SKU_Item.SubItems.Add("$($listview_NotExists_SKU_Result.$($_.Name)") 
            }
     
            # Add the created listViewItem to the ListView control
            # (not adding 'Out-Null' at the end of the line will result in numbers outputred to the console)
            $listview_NotExists_SKU.items.Add($listview_NotExists_SKU_Item) 
        }
     
        # Resize all columns of the listView to fit their contents
        $listview_NotExists_SKU.AutoResizeColumns("HeaderSize")
    }




    • Edited by B.R1984 Monday, August 26, 2019 8:50 PM
    Monday, August 26, 2019 8:44 PM
  • Is $results in scope or is it null?


    \_(ツ)_/

    Monday, August 26, 2019 8:53 PM
  • Also - why are you using "Shown" to load the listbox?  That is not a good event for what you re trying to do.


    \_(ツ)_/

    Monday, August 26, 2019 8:54 PM
  • Its called with $Script:Result on the button that activate it , but null to begin with
    Monday, August 26, 2019 8:55 PM
  • It was just too see if it was working or not, because I don't know what event to use - that's why i'm asking for help.
    Monday, August 26, 2019 8:57 PM
  • Its called with $Script:Result on the button that activate it , but null to begin with

    What does "activate it" mean? 


    \_(ツ)_/

    Monday, August 26, 2019 8:58 PM
  • Its called with $Script:Result on the button that activate it , but null to begin with

    What does "activate it" mean? 


    \_(ツ)_/

    # Adding another button control to Form
    $button_whatif = New-Object System.Windows.Forms.Button
    $button_whatif.Location = New-Object System.Drawing.Size(352, 954)
    $button_whatif.Size = New-Object System.Drawing.Size(320, 32)
    $button_whatif.TextAlign = "MiddleCenter"
    $button_whatif.Text = “WhatIf”
    $button_whatif.Add_Click( { $script:Results = Set-DynamicVariables -Manufacturer "$($listview_Vendor.SelectedItems)" -TSPackageID "$($ListView_Tasksequences.SelectedItems.SubItems[1].Text)" -WhatIf })
    $Form.Controls.Add($button_whatif)


    Monday, August 26, 2019 8:59 PM
  • It was just too see if it was working or not, because I don't know what event to use - that's why i'm asking for help.

    Until you know what you want just place the code in a button.

    The first time "Shown" executes the variable will not exisit.


    \_(ツ)_/

    • Marked as answer by B.R1984 Tuesday, August 27, 2019 3:12 PM
    Monday, August 26, 2019 8:59 PM
  • No.  Put move the 'Shown" code to a button.


    \_(ツ)_/

    • Marked as answer by B.R1984 Tuesday, August 27, 2019 3:12 PM
    Monday, August 26, 2019 9:07 PM
  • What is better, I have it working now both ways ?
    Monday, August 26, 2019 9:14 PM