J
J
Jordan_H2019-08-01 11:18:28
PowerShell
Jordan_H, 2019-08-01 11:18:28

How to organize a cycle of dynamically formed elements?

How to organize a cycle of disabling comboboxes from dynamically formed combobox name elements?

Source code without optimization

function ClearOuList($count)
{
  #$comboboxOuNext1.DataSource = $null
  switch ($count) {
    1 {
      $comboboxOuNext1.visible = $false
      $comboboxOuNext2.visible = $false
      $comboboxOuNext3.visible = $false
      $comboboxOuNext4.visible = $false
      $comboboxOuNext5.visible = $false
      $comboboxOuNext6.visible = $false
      $comboboxOuNext7.visible = $false
      $comboboxOuNext8.visible = $false
      $comboboxOuNext9.visible = $false
      $comboboxOuNext10.visible = $false
    }
    2 {
      $comboboxOuNext2.visible = $false
      $comboboxOuNext3.visible = $false
      $comboboxOuNext4.visible = $false
      $comboboxOuNext5.visible = $false
      $comboboxOuNext6.visible = $false
      $comboboxOuNext7.visible = $false
      $comboboxOuNext8.visible = $false
      $comboboxOuNext9.visible = $false
      $comboboxOuNext10.visible = $false
    }
    3 {
      $comboboxOuNext3.visible = $false
      $comboboxOuNext4.visible = $false
      $comboboxOuNext5.visible = $false
      $comboboxOuNext6.visible = $false
      $comboboxOuNext7.visible = $false
      $comboboxOuNext8.visible = $false
      $comboboxOuNext9.visible = $false
      $comboboxOuNext10.visible = $false
    }
    4 {
      $comboboxOuNext4.visible = $false
      $comboboxOuNext5.visible = $false
      $comboboxOuNext6.visible = $false
      $comboboxOuNext7.visible = $false
      $comboboxOuNext8.visible = $false
      $comboboxOuNext9.visible = $false
      $comboboxOuNext10.visible = $false
    }
    5 {
      $comboboxOuNext5.visible = $false
      $comboboxOuNext6.visible = $false
      $comboboxOuNext7.visible = $false
      $comboboxOuNext8.visible = $false
      $comboboxOuNext9.visible = $false
      $comboboxOuNext10.visible = $false
    }
    6 {
      $comboboxOuNext6.visible = $false
      $comboboxOuNext7.visible = $false
      $comboboxOuNext8.visible = $false
      $comboboxOuNext9.visible = $false
      $comboboxOuNext10.visible = $false
    }
    7 {
      $comboboxOuNext7.visible = $false
      $comboboxOuNext8.visible = $false
      $comboboxOuNext9.visible = $false
      $comboboxOuNext10.visible = $false
    }
    8 {
      $comboboxOuNext8.visible = $false
      $comboboxOuNext9.visible = $false
      $comboboxOuNext10.visible = $false
    }
    9 {
      $comboboxOuNext9.visible = $false
      $comboboxOuNext10.visible = $false
    }
    10 {
      $comboboxOuNext10.visible = $false
    }
  }

The misunderstanding lies in the formation of the name of the combobox in a loop for its subsequent disabling. If you form a dynamic name in a cycle, from the part of the name of the necessary variable + cycle variable (for example: $comboboxOuNext$i, where $i=8), then you lose control over the real name of the combobox, since the compound variable is already a string variable and it has there are no properties of type visible, e.g. Such a variable is not a reference to the required combobox object and is not its real name, although visually and identically. If you create a variable of the combobox type, then this construction still does not work, since it turns out that this, apparently, is another intra-procedural object ... Maybe there are some pointers to the necessary objects that can be dynamically formed in cycles and which will be correctly on them indicate?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dartar, 2019-08-02
@Jordan_H

Try like this:

function ClearOuList($count)
{
  #$comboboxOuNext1.DataSource = $null
  switch ($count) {
    1 {
         Get-Variable -Name "comboboxOuNext[1-9]" | %{$_.Value.visible=$false
         Get-Variable -Name "comboboxOuNext10" | %{$_.Value.visible=$false
        }
    2 {
         Get-Variable -Name "comboboxOuNext[2-9]" | %{$_.Value.visible=$false
         Get-Variable -Name "comboboxOuNext10" | %{$_.Value.visible=$false
        }
    3 {
         Get-Variable -Name "comboboxOuNext[3-9]" | %{$_.Value.visible=$false
         Get-Variable -Name "comboboxOuNext10" | %{$_.Value.visible=$false
        }
    4 {
         Get-Variable -Name "comboboxOuNext[4-9]" | %{$_.Value.visible=$false
         Get-Variable -Name "comboboxOuNext10" | %{$_.Value.visible=$false
        }
    5{
         Get-Variable -Name "comboboxOuNext[5-9]" | %{$_.Value.visible=$false
         Get-Variable -Name "comboboxOuNext10" | %{$_.Value.visible=$false
        }
    6{
         Get-Variable -Name "comboboxOuNext[6-9]" | %{$_.Value.visible=$false
         Get-Variable -Name "comboboxOuNext10" | %{$_.Value.visible=$false
        }
    7 {
         Get-Variable -Name "comboboxOuNext[7-9]" | %{$_.Value.visible=$false
         Get-Variable -Name "comboboxOuNext10" | %{$_.Value.visible=$false
        }
    8 {
         Get-Variable -Name "comboboxOuNext[89]" | %{$_.Value.visible=$false
         Get-Variable -Name "comboboxOuNext10" | %{$_.Value.visible=$false
        }
    9 {
         Get-Variable -Name "comboboxOuNext9" | %{$_.Value.visible=$false
         Get-Variable -Name "comboboxOuNext10" | %{$_.Value.visible=$false
        }
    10 {
          Get-Variable -Name "comboboxOuNext10" | %{$_.Value.visible=$false
        }
  }
}
}
You can also consider quantifiers:
Get-Variable * | ? {$_.name -match "comboboxOuNext[0-9]{1,2}"}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question