Hide/Show Certain Text Fields in a form

I am using software that scripts with VB .NET and I am trying to have a textbox hidden unless a combo box above it has "other" selected. So the user will select something from the combo box and if it is "other" then I want a certain textbox to become visible to type information into. If they pick something other than "other" then I want the textbox to remain hidden. Thanks!

Answer this question

Hide/Show Certain Text Fields in a form

  • B_Lilith

    sorry but i have never really used VB before so where would i put that code. Do I put it in a form load or what I appreciate your help.
  • Herbert Lehner

    Well when the selectedindex has been changed, you can then check to see the value of the item chosen, and if it matches your condition then make the textbox visible:

    if me.theComboBox.SelectedItem.ToString().Equals("other") then

    me.theTextBox.Visible = true

    else

    me.theTextBox.Visible = false

    end if

     

    On the combo box, you may implement the SelectedIndexChange event and when you change the value item chosen from the combo box, this event gets fired and from here, you can then make the textbox appear/disappear depending on the condition of your choice.

     

    hope it helps!



  • seco

    I did what you said and when I open the form it displays both the combo box and text box and then when i select any text from the combo box the text box will disappear. any suggestions
  • chris441962

    Well, you would put the code on the selectedindexchanged event.

    so, in your Design View, click on the combobox, then in the properties, select the events button (the one that looks like a lightning symbol)

    somewhere in there, there will be a property called something like "selectedindexchanged" - just doubleclick on it to make an event.

     

    once done, you should be automatically be taken into the code and the event should have been created.

     

    here is where you enter that code.



  • TommieJ

    this is because of the code. if the text selected from the combo box is NOT "other" then it will hide the textbox, otherwise if the text "other" is selected in the combo box, the textbox will appear.

     

    the code given was an example, you will have to modify it to your needs



  • gooon

    it goes away no matter what I select. I want the textbox to not be visible when i enter the form and the only way it will become visible is if i select "other" from the combo box. thanks!
  • Noral

    change SelectedText to SelectedItem.ToString() then....

     

    on the form, select the textbox and make the visible property to false. this will not show the textbox at all.

    in your code, the textbox will become visible when you select an item from the combobox on the selectedindexchange event.

    example:

     


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    If Me.ComboBox1.SelectedItem.ToString().Equals("other") Then

    Me.TextBox1.Visible = True

    Me.TextBox1.Text = "hi"

    Else

    Me.TextBox1.Visible = False

    End If

    End Sub

     

     



  • Hide/Show Certain Text Fields in a form