How to make a button visible when a tab is clicked?
In a program I'm working on I have a tab control. Outside the tab control is a button and the button is only needed when one of the tab control pages is open. How do I ensure that the button will not be visible when the user clicks on the other tab pages?
Re: How to make a button visible when a tab is clicked?
You could check which tab is selected in its "SelectedIndexChanged" event, like this:
Code:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab.Name = "TabPage2" Then
btnUpdate.Visible = False
ElseIf TabControl1.SelectedTab.Name = "TabPage1" Then
btnUpdate.Visible = True
End If
End Sub
Re: How to make a button visible when a tab is clicked?
You could simplify that to this:
Code:
btnUpdate.Enabled = If(TabControl1.SelectedTab.Name = "TabPage1", True, False)
Re: How to make a button visible when a tab is clicked?
Or even simpler:
Code:
btnUpdate.Enabled = (TabControl.SelectedTab.Name = "TabPage1")
.
You do not even need the parenthesys, but leave them to make it clear.
Re: How to make a button visible when a tab is clicked?
Code:
btnUpdate.Visible = TabControl1.SelectedTab.Name = "TabPage1"
When compiled its about 4 calls less, so should be more efficient.
But the general rule is to avoid string comparisons whenever possible becasue they are very slow and integers are very efficient and fast, so use the index instead!
Code:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
btnUpdate.Visible = TabControl1.SelectedIndex = 0
End Sub
Now the compiled code is about half the size with half as many calls compared to the string version! (Wow I wasn't expecting such a huge difference).