WPF ComboBox Selection Action

This topic contains 2 replies, has 2 voices, and was last updated by  Pj 1 month ago.

  • Author
    Posts
  • #102566

    Pj
    Participant

    Short story is I am adding a function tab to my WPF app and am having an issue with getting comboboxitem.add_selected to set a single number to a variable. It will change the label foreground color on selection but without the variable number im not able to track what the user selected with the drop menu. script as follows...

    $ScreenOn = $win.Findname("ScreenOn")
    $ScreenOff = $win.Findname("ScreenOff")
    $ScreenNone = $win.FindName("ScreenNone")
    $SecondScreen = $win.Findname("SecondScreenLabel")
    $ApplyChanges = $win.Findname("ApplyChanges")
    
    $path = "C:\Program Files (x86)\MiTek\Blade\Config\Config.mdb"
    $adOpenStatic = 3
    $adLockOptimistic = 3
    $cn = new-object -comobject ADODB.Connection
    $rs = new-object -comobject ADODB.Recordset
    
    $ScreenNone.add_selected({
    $SecondScr = 0
    $SecondScreen.Foreground = "White"
    })
    
    $ScreenOff.add_selected({
    $SecondScr = 1
    $SecondScreen.Foreground = "Yellow"
    })
    
    $ScreenOn.add_selected({
    $SecondScr = 2
    $SecondScreen.Foreground = "Yellow"
    })
    
    $ApplyChanges.add_click({
    If ($SecondScr -gt "0") {Second-Scr}
    $ScreenNone.IsSelected = "True"
    })
    
    Function Second-Scr () {
    $OptionName = "Eng.HasSecondScreen"
    If ($SecondScr -eq 1) {$OptionData = "False"}
    If ($SecondScr -eq 2) {$OptionData = "True"}
    $cn.Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = $path")
    $rs.open("UPDATE Options SET OptionData = '$OptionData' WHERE OptionName='$OptionName'",$cn,$adOpenStatic,$adLockOptimistic)
    $cn.Close()
    }
    
    $Win.Showdialog()
    
  • #102584

    James Crompton
    Participant

    If I understand the description of your problem, when the add_selected is called you are getting the $SecondScreen.Foreground color applied but when you set the value of $SecondScr the set value is not retrievable later in other methods?

    I am not sure what the issue is as I cannot easily run your code but I would guess you are hitting a scoping problem where SecondScreen is defined in the outer scope so reading that variable value gives you a reference to it and you can update a sub property Foreground, but SecondScr ends up being created as a new variable in a new scope for the callback and the value is lost when you come back out.

    To test the theory you could try setting a script scope variable instead and see if that works.

    $ScreenOn = $win.Findname("ScreenOn")
    $ScreenOff = $win.Findname("ScreenOff")
    $ScreenNone = $win.FindName("ScreenNone")
    $SecondScreen = $win.Findname("SecondScreenLabel")
    $ApplyChanges = $win.Findname("ApplyChanges")
    $script:SecondScr = 0
    
    $path = "C:\Program Files (x86)\MiTek\Blade\Config\Config.mdb"
    $adOpenStatic = 3
    $adLockOptimistic = 3
    $cn = new-object -comobject ADODB.Connection
    $rs = new-object -comobject ADODB.Recordset
    
    $ScreenNone.add_selected({
    $script:SecondScr = 0
    $SecondScreen.Foreground = "White"
    })
    
    $ScreenOff.add_selected({
    $script:SecondScr = 1
    $SecondScreen.Foreground = "Yellow"
    })
    
    $ScreenOn.add_selected({
    $script:SecondScr = 2
    $SecondScreen.Foreground = "Yellow"
    })
    
    $ApplyChanges.add_click({
    If ($script:SecondScr -gt "0") {Second-Scr}
    $ScreenNone.IsSelected = "True"
    })
    
    Function Second-Scr () {
    $OptionName = "Eng.HasSecondScreen"
    If ($script:SecondScr -eq 1) {$OptionData = "False"}
    If ($script:SecondScr -eq 2) {$OptionData = "True"}
    $cn.Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = $path")
    $rs.open("UPDATE Options SET OptionData = '$OptionData' WHERE OptionName='$OptionName'",$cn,$adOpenStatic,$adLockOptimistic)
    $cn.Close()
    }
    
    $Win.Showdialog()
    
  • #102703

    Pj
    Participant

    So for the most part James' answer about scope addresses the issue ( I don't fully understand why the scope is different in this scenario but ). The code example provided was a simplified single setting I had been trying to use to isolate the problem, and this answer worked for majority of scenarios where this issue existed. I do have a section I am still working to resolve this issue for, I will post when I find the issue with that part. The part causing an issue still is the same as above but in addition to foreground color there are visibility controls coupled with isselected changes to prevent invalid setting combinations.

    $FrontNone.add_selected({
    $Script:FrontPrt = "0"
    $FrontPrinter.Foreground = "White"
    })
    
    $Front16.add_selected({
    $Script:FrontPrt = "1"
    $RearNo.Visibility = "Visible"
    $RearNone.Visibility = "Visible"
    $FrontPrinter.Foreground = "Yellow"
    })
    
    $Front32.add_selected({
    $Script:FrontPrt = "2"
    If($EdgeYes.IsSelected -eq "True") {$Rear7.IsSelected = "True"}
    If($EdgeYes.IsSelected -eq "True") {$RearNone.Visibility = "Collapsed"}
    If($EdgeYes.IsSelected -eq "True") {$RearNo.Visibility = "Collapsed"}
    $FrontPrinter.Foreground = "Yellow"
    })
    
    $RearNone.add_selected({
    $Script:RearPrt = "0"
    $FrontNone.Visibility = "Visible"
    $RearPrinter.Foreground = "White"
    })
    
    $RearNo.add_selected({
    $Script:RearPrt = "1"
    $FrontNone.Visibility = "Visible"
    $RearPrinter.Foreground = "Yellow"
    })
    
    $Rear7.add_selected({
    $Script:RearPrt = "2"
    If ($FrontNone.IsSelected -eq "True") {$Front16.IsSelected = "True"}
    $FrontNone.Visibility = "Collapsed"
    $RearPrinter.Foreground = "Yellow"
    })
    
    $Rear32.add_selected({
    $Script:RearPrt = "3"
    If ($FrontNone.IsSelected -eq "True") {$Front16.IsSelected = "True"}
    $FrontNone.Visibility = "Collapsed"
    $RearPrinter.Foreground = "Yellow"
    })
    
    $EdgeNone.add_selected({
    $Script:EdgePrt = "0"
    $RearNo.Visibility = "Visible"
    $RearNone.Visibility = "Visible"
    $FrontNone.Visibility = "Visible"
    $EdgePrinter.Foreground = "White"
    })
    
    $EdgeNo.add_selected({
    $Script:EdgePrt = "1"
    $RearNo.Visibility = "Visible"
    $RearNone.Visibility = "Visible"
    $FrontNone.Visibility = "Visible"
    $EdgePrinter.Foreground = "Yellow"
    })
    
    $EdgeYes.add_selected({
    $Script:EdgePrt = "2"
    If ($FrontNone.IsSelected -eq "True") {$Front16.IsSelected = "True"}
    $FrontNone.Visibility = "Collapsed"
    If ($Front32.IsSelected -eq "True") {$Rear7.IsSelected = "True"}
    If ($Front32.IsSelected -eq "True") {$RearNone.Visibility = "Collapsed"}
    If ($Front32.IsSelected -eq "True") {$RearNo.Visibility = "Collapsed"}
    $EdgePrinter.Foreground = "Yellow"
    })
    
    
    

You must be logged in to reply to this topic.