+1  A: 

You will want to use the ComboBox_SelectedIndexChanged() event to capture that the combo box item has been changed. At that point, you will need to check to see which combo box item has been selected and make a decision as to whether the TextBox should be enabled or not. Here is an example. Note: This example assumes that "Alive" is the first item in your combobox at the 0 index.


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If ComboBox1.SelectedIndex = 0 Then 'Alive 
            TextBox1.Enabled = True
        Else
            TextBox1.Enabled = False
        End If
    End Sub

Dynamically generate the combobox and add handler.

Dim cmb as New ComboBox
AddHandler cmb.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
Me.Controls.Add(cmb)
Josaph
Yes and I understand how that would be possible if the ComboBox was created at Design Time but in my scenario there are 10 people objects so there are 10 rows of controls created at runtime. Each row of controls are dynamically created so how would I deal with the "Handles"?Sorry if I wasn't clear in my description but thank you for your help.
AdamBeck
Use the .Text property to see if it equals "Alive".
Josaph
If ComboBox1.SelectedItem.Text = "Alive"
Josaph
But where would that go? Either I'm so new to events or I am not explaining this correctly. The form populates - Boom! The user then has all the time in the world to adjust the appropriate controls. Now as soon as the user DOES change the dynamic ComboBox I want the textbox to enable/disable accordingly. I can use your method if I have a static ComboBox because the controls have events with them. That's no problem - it's creating events with controls that the IDE doesn't even know exist yet.
AdamBeck
You need to use an AddHandler at that point. See updated code.
Josaph
Thank you for your responses. I did learn one thing from this though. In your updated code you have the AddHandler before you add the time itself. I kept running into the problem since I was creating them and adding values to the ComboBox at runtime that it would error out. I learned you need to add the Handler AFTER the control has been created, populated, and set to the default value. But again, thank you.
AdamBeck
+1  A: 

From what I gather from the comments, you want to add events to a form object that is created at runtime. Use the AddHandler command to the object. Something to the effect of:

AddHandler NameOfFormObject.TypeOfAction, AddressOf HowToHandle

Private Sub HowToHandle(ByVal sender as System.Object, ByVal e As System.EventArgs)
   DropDownMenu.enabled = True
End Sub

Doing it this way, you will be able to modify the events of an object created at runtime. In your case, it sounds like you'll want to use the action that Josaph recommended, and end up incorporating both solutions offered, like so

AddHandler ComboBox1.SelectedIndexChanged, AddressOf HowToHandle

Private Sub HowToHandle(ByVal sender as System.Object, ByVal e As System.EventArgs)

    If DirectCast(sender, ComboBox).SelectedIndex = 0 'Alive 
        DirectCast(DirectCast(sender, ComboBox).Tag, TextBox).enabled = True
    Else
        DirectCast(DirectCast(sender, ComboBox).Tag, TextBox).enabled = False
    End If
End Sub
AndyPerfect
I think I'm getting somwhere because this looks familiar with what I read on MSDN. But in your code you have the name already in place (e.g. ComboBox1). In my code I will create these controls based on how many "People" objects are in a file that the user loads in so I don't know if I will know the name. I know it will follow a basic structure such as: ComboBox1, ComboBox2, etc. And I will know how many but not until runtime.
AdamBeck
edited to reflect your requests =)Sender is, in reality, the comboBox that sent the action. Just cast it to a comboBox, and you'll have whichever one was acted upon
AndyPerfect
I'm really getting somewhere with this! I have my form of 10 ComboBoxes and when I select index 0 the STATIC TextBox1 becomes enabled. Now I need to figure out how to make this select the appropriate textbox.
AdamBeck
Code updated. When you create the comboBox and TextBox at runtime, have their tags act as pointers to each other. So do:comboBox.tag = textBoxAll ComboBoxes will now have pointers to their corresponding textBox. All is well in the world.
AndyPerfect
I get what you are saying but you can't DirectCast a String into a TextBox.
AdamBeck
A: 

I suppose that as you will have 10 comboboxes... in the same way you'll have 10 textboxes.

In that case... once you attach and handle the event as AndyPerfect and Joseph... in that method you will need code something to know which of the Textboxs you need to enable/disable.

First you need to know which combobox triggered the event: this is done using the "sender" parameter. ctype(sender, Combobox) to access the methods and properties of the ComboBox.

Once you know which combo, you need to activate/deactivate the correct textbox. To accomplish this, you'll need to add a reference to the TextBox in the "TAG" property of the Combobox at the moment of creating it.

Dim txt as new TextBox
Dim cmb as new ComboBox
cmb.Tag = txt

Then... you simple use:

ctype(ctype(sender, Combobox).Tag, TextBox).Enable = true
Romias
A: 

Here is how I ended up writing it in the end. I appreciate all the help! Thank you!

    If DirectCast(sender, ComboBox).SelectedIndex = 2 Then
        DirectCast(Me.Controls.Item(DirectCast(sender, ComboBox).Tag), TextBox).Enabled = True
    Else
        DirectCast(Me.Controls.Item(DirectCast(sender, ComboBox).Tag), TextBox).Enabled = False
    End If
AdamBeck