VS 2012 How to make a button visible when a tab is clicked?-VBForums
Results 1 to 5 of 5

Thread: How to make a button visible when a tab is clicked?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    13

    Question 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?

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,414

    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

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,159

    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)

  4. #4
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,592

    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.
    Last edited by kaliman79912; Apr 7th, 2014 at 05:47 PM.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

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

    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).
    Last edited by Edgemeal; Apr 15th, 2014 at 07:45 AM.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.