Help getting output from ForEach loop

I am in the process of creating a form in order to make it easier for me to update a .ini file. I have been able to get the form to generate correctly but have been unable to figure out how to output the values from the comboboxes and textboxes created by the ForEach loop after I have made changes. Is there a way to have the loop assign a variable or some other way to refer back to each of them as they are created? Below is the loop I am currently working with.

Any help would be greatly appreciated.

Thank you

ForEach($value in $valuearraylist){
    if( $_ -eq "TRUE" -or $_ -eq "FALSE"){
        $Valuecombobox          = New-Object System.Windows.Forms.combobox
        $Valuecombobox.Location = "325,$(60+23*$j)"
        $Valuecombobox.Size     = New-Object System.Drawing.Size(300,20)
        $Valuecombobox.Items.Add('True')
        $Valuecombobox.Items.Add('False')
        $Valuecombobox.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList
        $Valuecombobox.Font    = New-Object System.Drawing.Font('Times New Roman',10)
        $Valuecombobox.SelectedIndex = $Valuecombobox.FindString($_)
        }
        
    else{
        $ValueList          = New-Object System.Windows.Forms.textbox
        $ValueList.Location = "325,$(60+23*$j)"
        $ValueList.Text     = $valuearraylist[$j]
        $ValueList.Height   = 20
        $ValueList.Width    = 300
        $ValueList.Font     = New-Object System.Drawing.Font('Times New Roman',10)
        }

    $j++
    $form.Controls.Add($ValueList)
    $form.Controls.Add($Valuecombobox)
}

Morgan,
Welcome to the forum. :wave:t4:

Without having any experience with GUIs the following code looks - at least - ununsual to me.

ForEach ($value in $valuearraylist) {
    if ( $_ -eq "TRUE" -or $_ -eq "FALSE") {

Why is it $_ and not $Value in your if statement?
And why do you check for $_ to be true OR false? Shouldn’t that be only one of it? And shouldn’t it be $true instead of TRUE or $false instead of FALSE?
And why don’t you reference the loop variable $value at leaset once in your loop?

As Olaf stated, you might try this (changing $_ to $Value):

if ( $Value -eq "TRUE" -or $Value -eq "FALSE") {

Kind of odd checking for both true and false, but if those are the only possible values, you could simply say:

if ($Value) {

You might get better help if you detail what is in the valueArrayList

Previously, I was using

$valuearraylist | foreach-object{...}

which is why I was using $_ but yes, I will change it to $value now.

As for what the $valuearraylist contains, it is all plain text. For the lines that contain TRUE or FALSE, I wanted it to create a combobox, so I had one block check for both values and create a TRUE or FALSE combobox. The rest of the lines are plain text that would be easiest to change using a textbox which is what this loop does.

I just have not been able to find any way to make each individual combobox and textbox callable outside the loop so that I can output all the values after I have made changes.

Thank you for your input!

That sounds like a weird way of providing values for different purposses. You may consider using another file type for your configuration such as CSV, XML, PSD1, YAML or JSON.

Regardless of that you may share all or at least the relevant part of your code. I’m pretty sure we have some forum members with enough experiences with GUIs to be able to help you further.

Current script is 217 lines. I wasn’t sure if that was too much to upload in one go, but I have no problem doing so if that would be helpful.

In general it is better to limit the code you share to a “minimal reproducable example” but if it’s needed to understand your issue you may post it completely anyway. :wink:

I am satisfied with my data input, it is just being able to call back to each box the loop creates that I am having trouble figuring out. I honestly don’t even know if it is possible to do.

Not sure I understand what you mean by callable, but I have a script that loads a windows form, does a bunch of stuff via user interaction on the form, then when they close the form, the main script continues. I did that by putting the form creation and load in a function. Then, in the main body of the script, I call the function, the user does their thing on the form, they close the form and the script moves on. Have you tried something like that?

Here is a picture of the form I am currently making in the hopes that visualization will clear up some of what I can’t say clearly.

The code I shared creates the Value column and after I make changes to those boxes, I want to be able to get the new values to output to file. If there is a better way to do this, I am all ears and happy to try it.

Thank you!