Reputation: 51
I am trying to set automatically the $MyTextBox value ($MyTextBox.text) based on the selected $MyComboBox
He is what I tried, but it doesn't work when I change the selected MyComboBox Item
$MyComboBox = New-Object system.Windows.Forms.ComboBox
$MyComboBox.text = "My_ComboBox"
$MyComboBox.width = 204
$MyComboBox.height = 20
$MyComboBox.location = New-Object System.Drawing.Point(36, 275)
$MyComboBox.Font = 'Microsoft Sans Serif,10,style=Bold'
$MyComboBox.ForeColor = "#9b9b9b"
$MyComboBox.Items.add("A")
$MyComboBox.Items.add("B")
$MyComboBox.Items.add("Other")
$MyComboBox.SelectedIndex = 0
$MyTextBox = New-Object system.Windows.Forms.TextBox
#What I tried but doesn't work
if( $MyComboBox.SelectedItem -eq "A") {$MyTextBox.text = "1" }
if( $MyComboBox.SelectedItem -eq "B") {$MyTextBox.text = "2" }
$InboxTextBox.multiline = $false
$InboxTextBox.width = 100
$InboxTextBox.height = 20
$InboxTextBox.location = New-Object System.Drawing.Point(133,307)
$InboxTextBox.Font = 'Microsoft Sans Serif,10'
Do I need a trigger/function to do it ?
Upvotes: 0
Views: 502
Reputation: 2929
The SelectedItem Property is an object, which you compare to a string object, and these objects are likley not to be the same.
Use
if( $MyComboBox.SelectedItem.ToString() -eq "A") {$MyTextBox.text = "1" }
or
if( $MyComboBox.SelectedIndex -eq 0) {$MyTextBox.text = "1" }
Update:
You should also add an Event Handler via add_SelectedIndexChanged
$MyComboBox_SelectedIndexChanged =
{
if( $MyComboBox.SelectedItem.ToString() -eq "A") {$MyTextBox.text = "1" }
# other test cases...
}
$MyComboBox.add_SelectedIndexChanged($MyComboBox_SelectedIndexChanged)
Upvotes: 1