locked
ComboBox - button update on form start ? RRS feed

  • Question

  • Hello to all,
    i have started to learn Forms to be able to build scripts with GUI. But i am just beginning.
    People here where really helpful in the past, so i am coming back with an issue, that i am not able to wrap my head around.
    When a value is selected in combobox, it correctly tigers the button text, however first after i select something and not when the form loads.
    Is it possible to load the button text from the first entry on loading of the form, or should i create a first entry saying Select language and a unique value, that i IF around?

    Suggestions are highly appreciated.

    Here is my script:

    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()
    
    $Form                            = New-Object system.Windows.Forms.Form
    $Form.ClientSize                 = '400,400'
    $Form.text                       = "Form"
    $Form.TopMost                    = $false
    
    $SelectLanguage                  = New-Object system.Windows.Forms.ComboBox
    $SelectLanguage.DropDownStyle    = [System.Windows.Forms.ComboBoxStyle]::DropDownList
    $SelectLanguage.text             = "Select Language"
    $SelectLanguage.width            = 280
    $SelectLanguage.height           = 20
    $items=[collections.arraylist]@(
    #    [pscustomobject]@{Name = 'Select LanguagePack to install'; Value = 'novalue'}
    	[pscustomobject]@{Name = 'Finish'; Value = 'fi-fi'}
    	[pscustomobject]@{Name = 'Slovak'; Value = 'sk-sk'}
        [pscustomobject]@{Name = 'German'; Value = 'de-de'}
        [pscustomobject]@{Name = 'English'; Value = 'en-us'}
    )
        $SelectLanguage.DataSource=$items
        $SelectLanguage.DisplayMember='Name'
        $SelectLanguage.location         = New-Object System.Drawing.Point(63,136)
        $SelectLanguage.Font             = 'Microsoft Sans Serif,10'
    
    $close                           = New-Object system.Windows.Forms.Button
    $close.text                      = "Install / Uninstall"
    $close.width                     = 120
    $close.height                    = 30
    $close.location                  = New-Object System.Drawing.Point(155,192)
    $close.Font                      = 'Microsoft Sans Serif,10'
    $close.Add_Click({
        $option = $SelectLanguage.SelectedItem.value
        $optionpath = 'O365ProPlusRetail - '+$option
        $regpath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
        if(Test-Path $(join-path $regpath $optionpath)) {
        $addrem = 'Remove'
        } else {
        $addrem = 'Add'
        }
    #    write-host $addrem
        write-host $regpath"\"$optionpath
    $xmltemplate = @"
       <Configuration>
          <$addrem OfficeClientEdition="64">
              <Product ID="LanguagePack">
                <Language ID="$option" />
              </Product>
          </$addrem>
    	  <Display Level="Full" AcceptEULA="True" />
       </Configuration>
    "@
        Write-Host $xmltemplate
    #   $form.Close()
    }
    )
    
    $SelectLanguage_SelectedIndexChanged=
    {$option = $SelectLanguage.SelectedItem.value
     $optionpath = 'O365ProPlusRetail - '+$option
     $regpath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
        if(Test-Path $(join-path $regpath $optionpath)) {$close.text = 'Remove'} else {$close.text = 'Add'}
    }
    ################ MUST CREATE BEFORE ASSIGN ################
    $SelectLanguage.add_SelectedIndexChanged($SelectLanguage_SelectedIndexChanged)
    
    $Form.controls.AddRange(@($SelectLanguage,$close))
    [void]$form.ShowDialog()

      
    • Edited by OndrejSK Thursday, December 5, 2019 12:29 PM
    Thursday, December 5, 2019 12:25 PM

Answers

  • Just use the "Load" event to assign values or assign the value when the item is selected in the combo.


    \_(ツ)_/

    • Marked as answer by OndrejSK Friday, December 6, 2019 7:57 AM
    Thursday, December 5, 2019 4:02 PM

All replies

  • Just use the "Load" event to assign values or assign the value when the item is selected in the combo.


    \_(ツ)_/

    • Marked as answer by OndrejSK Friday, December 6, 2019 7:57 AM
    Thursday, December 5, 2019 4:02 PM
  • Hello jrv and thank you for responding! I totally forgot about the form it self and the event Load.
    I was looking on the combo and button events only - stupid me.
    Adding following code changes the button text:

    $Form.Add_Load({
    $option = $SelectLanguage.SelectedItem.value
    $optionpath = 'O365ProPlusRetail - '+$option
    $regpath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    if(Test-Path $(join-path $regpath $optionpath)) {$close.text = 'Remove'} else {$close.text = 'Add'}
    })
    As always thank you sir! you have been very helpful.

    Friday, December 6, 2019 7:57 AM