Results 1 to 5 of 5

Thread: Making button invisible if nothing from Combobox is selected

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2014
    Posts
    32

    Making button invisible if nothing from Combobox is selected

    Can someone help me generate a code so that when nothing from a Combobox (Who's drop down style is: DropDownList) is selected then a certain button becomes invisible until something is selected.

    Code: If cboEnvironment.Text.Length = 0 Then
    btnSubmit.Visible = False
    End If

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Making button invisible if nothing from Combobox is selected

    When nothing is selected the selected index will equal -1

    try,
    Code:
    Private Sub cboEnvironment_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboEnvironment.SelectedIndexChanged
        If cboEnvironment.SelectedIndex = -1 Then
            btnSubmit.Visible = False
        Else
            btnSubmit.Visible = True
        End If
    End Sub
    Last edited by Edgemeal; Jun 25th, 2014 at 09:27 AM. Reason: chenge code to .Visible

  3. #3
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Making button invisible if nothing from Combobox is selected

    Hi,

    Edgemeal is correct but:-

    Other than on Form Load where this ComboBox may not have anything selected, using the DropDownStyle, DropDownList, does NOT allow you to deselect an option that has already been selected so your question is a mute point at this time??

    I guess there is something more to this question?

    Cheers,

    Ian

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Making button invisible if nothing from Combobox is selected

    Not sure I understand what you mean, to unselect just set the index to -1.

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ' on startup hide btnSubmit
            btnSubmit.Visible = False
        End Sub
    
        Private Sub cboEnvironment_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboEnvironment.SelectedIndexChanged
            ' when combo index changes check selected index value
            If cboEnvironment.SelectedIndex = -1 Then ' no item selected
                btnSubmit.Visible = False
            Else ' item is selected
                btnSubmit.Visible = True
            End If
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            ' reset combo selection
            cboEnvironment.SelectedIndex = -1
        End Sub
    
        Private Sub btnSubmit_Click(sender As System.Object, e As System.EventArgs) Handles btnSubmit.Click
            ' submit was click, do something..
        End Sub
    End Class

  5. #5
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Making button invisible if nothing from Combobox is selected

    Sorry Edgemeal,

    Yes, you can set this back to -1 in Code, but I was thinking more along the lines of a user not being able to set this back to an Unselected option using the drop down style being used.

    Apologies for the confusion and maybe this is the additional question I was expecting?

    Cheers,

    Ian

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width