-
Jun 25th, 2014, 09:15 AM
#1
Thread Starter
Member
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
-
Jun 25th, 2014, 09:24 AM
#2
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
-
Jun 25th, 2014, 09:34 AM
#3
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
-
Jun 25th, 2014, 10:04 AM
#4
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
-
Jun 25th, 2014, 10:26 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|