Home > Blockchain >  Powershell and XAML checkboxes problem while executing
Powershell and XAML checkboxes problem while executing

Time:09-30

I made a XAML form from visual studio 2012, the XAML form have checkboxes that you select to get the information.

Every checkbox have a powershell function behind.

When executing the form, if I select one checkbox and unselect it, when I press the button to execute the function, even if the checkbox is unchecked at the moment I press the execution button its continuing thinking that the checkbox is selected. But I unchecked the checkbox before clicking the execution button. So it's continuing executing other functions that I don't want it to execute.

Do we have a kind of code that force PowerShell to think the checkbox is $false while the form is in execution ?

Here an example of code:

 $apps_checkbox = $Window.FindName("apps_checkbox")
    $apps_checkbox.Add_Click({ 
    if($apps_checkbox.isChecked -eq $true){
       $retrievebutton.add_click({
       IF(!(GCI ".\$FOLDERNAME\$COMP" | WHERE-OBJECT NAME -EQ "APPS")){ 
        NEW-ITEM -ITEMTYPE DIRECTORY -NAME APPS -PATH ".\$FOLDERNAME\$COMP" 
       }
       NEW-ITEM -ITEMTYPE FILE -PATH 
       ".\$FOLDERNAME\$COMP\APPS\APPS-$COMP.TXT" 
       LIST-APPS 
        }) 
       } 
      })

CodePudding user response:

Still trying to figure out what you are trying to do, because we can't see the rest of your code, but from your latest comment, I gather you are defining the function in the wrong event handler.

Instead of $apps_checkbox.Add_Click({..}), you need to check if that checkbox is checked or not inside the click event of the $retrievebutton:

$retrievebutton = $Window.FindName("retrievebutton")
$retrievebutton.Add_Click({ 
    $apps_checkbox = $Window.FindName("apps_checkbox")
    if($apps_checkbox.IsChecked){
        # No need to test using -Force. Also throw away the diretoryInfo object it returns
        # You may need to use 'Script-scoping' here on the variables $FOLDERNAME and $COMP
        # like $script:FOLDERNAME and $script:COMP
        $null = New-Item -Path ".\$FOLDERNAME\$COMP\APPS" -ItemType Directory -Force
        $null = New-Item -Path ".\$FOLDERNAME\$COMP\APPS\APPS-$COMP.TXT" -ItemType File -Force
        # call your function 
        LIST-APPS 
    }
})

Another option would be to disable the $retrievebutton button the moment the checkbox gets unchecked. That way you don't need to test if the checkbox is checked or not, because the user can't click the button if it is unchecked.

CodePudding user response:

Theo is right: the code below resolve the problem:

$retrievebutton = $Window.FindName("retrievebutton")
$retrievebutton.Add_Click({ 
    $apps_checkbox = $Window.FindName("apps_checkbox")
    if($apps_checkbox.IsChecked){
        # No need to test using -Force. Also throw away the diretoryInfo object it returns
        # You may need to use 'Script-scoping' here on the variables $FOLDERNAME and $COMP
        # like $script:FOLDERNAME and $script:COMP
        $null = New-Item -Path ".\$FOLDERNAME\$COMP\APPS" -ItemType Directory -Force
        $null = New-Item -Path ".\$FOLDERNAME\$COMP\APPS\APPS-$COMP.TXT" -ItemType File -Force
        # call your function 
        LIST-APPS 
    }
})

thank you

  • Related