Skip to content
Fix Code Error

ComboBox.SelectedItem giving Null value

July 12, 2021 by Code Error
Posted By: Anonymous

I have been stuck in this for whole day. I am creating a UI using PowerShell forms. In that:

  • user select an option from 1st combobox. click button Go
  • Based on the selection, a panel will appear having another combobox.
  • if user select another option in 1st combobox then another panel appears with another combobox
  • After selecting options from panel comboboxes, user clicks on start button.
  • This leads to a function which stores the selected options to a variable.

Problem

  • Now when user selects the options from the comboboxes of panels, I am using $combobox.selecteditem.Tostring to get the values.
    But it gives me NULL result.
    Here is my code..

    _x000D_

    _x000D_

    $global:Button1Clicked = 0;
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    [System.Windows.Forms.Application]::EnableVisualStyles()
    [System.Windows.Forms.FormClosingEventHandler]
    
    $Form = New-Object system.Windows.Forms.Form
    $Form.ClientSize = '1500,800'
    $Form.text = "NextUI "
    $Form.BackColor = 'White'
    $Form.TopMost = $true
    $Form.add_closing(
    {
    if($global:Button2Clicked)
    {
    $global:Button2Clicked = 0;
    
    $_.cancel = $true;
    }
    })
    
    
    $panel1 = New-Object system.Windows.Forms.Panel
    $panel1.AutoSize = $true
    $panel1.Width = 1200
    $panel1.Height = 200
    $panel1.location = New-Object System.Drawing.Point(50,100)
    $panel1.Visible = $false
    $panel1.Controls.Add($label3)
    $panel1.Controls.Add($comboBox2)
    $panel1.BorderStyle = 1
    
    
    $panel2 = New-Object system.Windows.Forms.Panel
    $panel2.AutoSize = $true
    $panel2.Width = 1200
    $panel2.Height =200
    $panel2.location = New-Object system.Drawing.Point(50,100)
    $panel2.Visible = $false
    $panel2.Controls.Add($label4)
    $panel2.Controls.Add($ComboBox3)
    $panel2.BorderStyle = 1
    
    $Label1 = New-Object system.Windows.Forms.Label
    $Label1.text = "Select Module"
    $Label1.AutoSize = $true
    $Label1.location = New-Object System.Drawing.Point(35,50)
    $Label1.Font = 'segoe ui,9.5'
    
    $Label2 = New-Object system.Windows.Forms.Label
    $Label2.text = "SharePoint Settings"
    $Label2.AutoSize = $true
    $Label2.location = New-Object System.Drawing.Point(35,15)
    $Label2.Font = 'Segoe UI Semibold,9.5'
    
    $Label3 = New-Object system.Windows.Forms.Label
    $Label3.text = "Choose file and folder permission option"
    $Label3.AutoSize = $true
    $Label3.location = New-Object System.Drawing.Point(100,200)
    $Label3.Font = 'segoe ui,9.5'
    
    
    $Label4 = New-Object system.Windows.Forms.Label
    $Label4.AutoSize = $True
    $Label4.text = "File Permission"
    $Label4.location = New-Object System.Drawing.Point(100,200)
    $Label4.Font = 'Segoe UI ,9.5'
    
    
    ################ Module combo box ################
    
    $ComboBox1 = New-Object system.Windows.Forms.ComboBox
    $ComboBox1.text = "Select Module"
    $ComboBox1.width = 200
    $ComboBox1.height = 20
    $ComboBox1.location = New-Object System.Drawing.Point(310, 45)
    $ComboBox1.Font = 'Microsoft Sans Serif,10'
    $combobox1.items.Add("ControlSettings")
    $combobox1.items.Add("NextSettings")
    
    
    ######## file and folder permission #############
    $ComboBox2 = New-Object system.Windows.Forms.ComboBox
    $ComboBox2.text = "select an option"
    $ComboBox2.width = 200
    $ComboBox2.height = 20
    $ComboBox2.location = New-Object System.Drawing.Point(450, 200)
    $ComboBox2.Font = 'Microsoft Sans Serif,10'
    $ComboBox2.items.Add("View") 
    $ComboBox2.items.Add("Edit") 
    
    $ComboBox3 = New-Object system.Windows.Forms.ComboBox
    $ComboBox3.text = "select an option"
    $ComboBox3.width = 200
    $ComboBox3.height = 20
    $ComboBox3.location = New-Object System.Drawing.Point(450, 200)
    $ComboBox3.Font = 'Microsoft Sans Serif,10'
    $ComboBox3.items.Add("View") 
    $ComboBox3.items.Add("Edit")
    
    
    Function Button2_Click()
    {
    
    if ($ComboBox1.SelectedIndex -eq 0)
    {
    $panel2.Visible = $true
    $panel1.Visible = $false
    }
    
    if ($ComboBox1.SelectedIndex -eq 1)
    {
    $panel1.Visible = $true
    $panel2.Visible = $false
    
    }
    
    
    }
    
    $Button1 = New-Object system.Windows.Forms.Button
    $Button1.text = "Start"
    $Button1.width = 150
    $Button1.height = 30
    $Button1.BackColor = '#F6CEE3'
    $Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $Button1.location = New-Object System.Drawing.Point(500, 700)
    $Button1.Font = 'segoe ui,10'
    $Button1.Add_Click({
    Button1_Click;
    $global:Button1Clicked = 1;
    })
    
    ############# Button 'Go' #############
    
    $Button2 = New-Object system.Windows.Forms.Button
    $Button2.text = "Go"
    $Button2.width = 100
    $Button2.height = 30
    $Button2.BackColor = '#F6CEE3'
    $Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $Button2.location = New-Object System.Drawing.Point(680, 43)
    $Button2.Font = 'segoe ui,10'
    $Button2.Add_Click({
    Button2_Click;
    $global:Button2Clicked = 1;
    })
    
    
    ######### code Starts ###########
    
    function Button1_Click()
    {
        $link = $comboBox2.SelectedItem.ToString();
        $linktype = $comboBox3.SelectedItem.ToString();
    
    
    }
    ####### end of code ######
    
    $form.Controls.Add($Panel1)
    $form.Controls.Add($Panel2)
    $form.Controls.Add($button1)
    $form.Controls.Add($comboBox1)
    $form.Controls.Add($button2)
    $form.Controls.Add($pictureBox1)
    $form.Controls.Add($label1)
    $form.Controls.Add($label2)
    
    [void]$Form.Add_Shown({ $Form.Activate() })
    [void]$Form.ShowDialog()

    _x000D_

    _x000D_

    _x000D_

Solution

The issue is that your variables are being set in a function, so they are scoped to that function. There’s really no reason to put that in a function, put it directly in the .add_click() instead. Or if you feel that you need to keep it in the function you can scope the variable like you did with $global:ButtonClicked and set them to $global:Link and $global:LinkType.

Edit: Order does make a difference in PowerShell, so I did move some stuff around in your script to make it work right, but I was able to get values to reflect fine when I put them in the global scope.

$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]

######### code Starts ###########

function Button1_Click()
{
    write-host "Link = $global:link"
    write-host "Link Type = $global:linktype"

}

Function Button2_Click()
{

if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}

if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false

}


}

####### end of code ######

$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;

$_.cancel = $true;
}
})

################ Module combo box ################

$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")


######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.AddRange(@("View","Edit") )
#$ComboBox2.items.Add("Edit") 

$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View") 
$ComboBox3.items.Add("Edit")

$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1


$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1

$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'

$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'

$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'


$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'


$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
$global:link = $comboBox2.SelectedItem.ToString();
$global:linktype = $comboBox3.SelectedItem.ToString();
Button1_Click;
$global:Button1Clicked = 1;
})

############# Button 'Go' #############

$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})

$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)

[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()

When I ran that, set the two combobox values, and clicked Start I got text in the ISE saying what I set the Link and LinkType variables to, and was able to echo $global:link and $global:linktype to see the values correctly assigned.

Answered By: Anonymous

Related Articles

  • Form field border-radius is not working only on the last…
  • Trouble with boxes appearing/hiding based on selection
  • Changing PowerShell's default output encoding to UTF-8
  • Polymer 1.x: Observers
  • Generating a drop down list of timezones with PHP
  • Changing Icon Color behind ListTile in an…
  • How to dynamically add and remove views with Ember.js
  • Bootstrap 3 collapse accordion: collapse all works but then…
  • why 'if not' is not applying in multiple 'or' statement
  • How can I show and hide regions in Marionette.js?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

SFX (Self-extracted) file extract without UI

Next Post:

organization chart with Z.Blazor.Diagrams package

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error