Results 1 to 18 of 18

Thread: Tab Questions

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Tab Questions

    A) I would like to prevent the user from closing the last remaining tab in my program.
    So, I need to check to see what the total number of open tabs are - if the open tabs > 1, then close the selected tab - else if the open tabs = 1, then do nothing.
    I'm not exactly sure how to go about doing this. - specifically the checking of the currently opened tabs.

    B) When the user opens a new tab, they remain on their current tab. I dont like that. How can I make the new tab automatically selected once created?

    C) I would like to close a tab by right clicking it - that was easy to do on a selected tab, but how can I do this to an unselected tab without actually selecting/opening that tab.
    I want to be able to have one tab open, and right click/close any of the available tabs without leaving my current tab. Any thoughts here?

    Any help would be greatly appreciated!
    Thank you for your time!

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Tab Questions

    As for A)
    Code:
            If TabControl1.TabCount > 1 Then
                TabControl1.Controls.RemoveAt(TabControl1.Controls.Count - 1)
            End If
    As for B)
    Code:
            Dim tp As New TabPage
            tp.Text = "Tabpage" & TabControl1.Controls.Count + 1
    
            TabControl1.Controls.Add(tp)
    
    
            TabControl1.SelectedTab = tp
    As for C, I'm not to sure. But I hope I gave you a jumpstart.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Tab Questions

    A + B + C:

    Code:
    Public Class Form1
    
        Private WithEvents cms As New ContextMenuStrip
        Dim tabPageIndex As Integer = Nothing
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            cms.Items.Add("Remove Tab", Nothing, AddressOf clicked)
            TabControl1.ContextMenuStrip = cms
        End Sub
    
        Private Sub clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
            If Not tabPageIndex = -1 Then
                Dim currentIndex As Integer = TabControl1.SelectedIndex
                TabControl1.Controls.RemoveAt(tabPageIndex)
                If currentIndex < tabPageIndex Then
                    TabControl1.SelectedIndex = currentIndex
                ElseIf currentIndex > tabPageIndex Then
                    TabControl1.SelectedIndex = currentIndex - 1
                Else
                    If currentIndex < TabControl1.TabCount Then
                        TabControl1.SelectedIndex = currentIndex
                    Else
                        TabControl1.SelectedIndex = 0
                    End If
                End If
            End If
        End Sub
    
        Private Sub TabControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Right Then
                If TabControl1.TabCount > 1 Then
                    tabPageIndex = Enumerable.Range(0, TabControl1.TabCount).Where(Function(x) TabControl1.GetTabRect(x).Contains(e.Location)).FirstOrDefault
                Else
                    tabPageIndex = -1
                End If
            Else
                tabPageIndex = -1
            End If
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim tp As New TabPage
            tp.Text = "Tabpage" & TabControl1.TabCount + 1
            TabControl1.TabPages.Add(tp)
            TabControl1.SelectedTab = tp
        End Sub
    
    End Class

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Tab Questions

    dday9 - Thank you for taking the time to reply to my post!

    Your first line of code worked perfectly - thank you so much for the help here.

    Your second line of code worked, as far as auto selecting the newly created tab, however it broke some of the remove tab functionality.

    I have the remove tab function set to a double click - so when the user clicks on a tab it is highlighted, and if its double clicked it gets deleted.
    Well, since adding your code, when you are on a tab and want to delete another, it highlights that tab and deletes the one you were just on (instead of simply leaving you on the page you were on, and deleting the tab you are ready to get rid of).

    Any idea on how to incorporate both functions?
    (I hope my description made sense - havent had coffee yet)

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Tab Questions

    Alright - so I took dday9's code and .paul.'s code and combined them - solving A, B and C.
    Thank you very much for your help!
    dday9.vbforumRatings += 1
    paul.vbForumRatings += 1
    Last edited by codenewbie; Jan 26th, 2013 at 02:38 PM.

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Tab Questions

    D) I noticed one bug that I would like to fix. If you create a new tab, and decide that you didnt want to do that and want to go back to the previous tab, right clicking the currently selected tab doesnt close that tab.
    It ends up closing the previous tab and leaves the new tab open. Any thoughts?

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Tab Questions

    Quote Originally Posted by codenewbie View Post
    D) I noticed one bug that I would like to fix. If you create a new tab, and decide that you didnt want to do that and want to go back to the previous tab, right clicking the currently selected tab doesnt close that tab.
    It ends up closing the previous tab and leaves the new tab open. Any thoughts?
    How are you removing the tab?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Tab Questions

    Confusing - I know.

    Scenario-
    So, imagine running the program, where it opens up to a screen that is displaying a single tab, we'll call it "Dashboard".
    The user decided to create another tab, the "Settings" tab (remember: the program automatically takes you to the newly created "Settings" tab upon creation).
    Once this happens, the user decides that they really didnt want to create that tab in the first place, so they close it (remember: right clicking on any tab closes that tab).

    What I would like to happen:
    The user decides to close the currently selected tab ("Settings") by right clicking it.
    The tab closes, and forces the user back to the previously selected tab (in this case, the "Dashboard").

    What actually happens:
    The user decides to close the currently Selected tab ("Settings") by right clicking it.
    The "Dashboard" tab is closed, leaving the user on the "Settings" tab.


    Hopefully that clears up what I'm trying to accomplish a bit.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Tab Questions

    we need to see your exact code again.

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Tab Questions

    Here is my code for right clicking on a tab to delete it.

    Code:
     Private Sub TabControlBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControlBox.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Right Then
                If Not TabPageIndex = -1 Then
                    Dim CurrentIndex As Integer = TabControlBox.SelectedIndex
                    TabControlBox.Controls.RemoveAt(TabPageIndex)
                    If CurrentIndex < TabPageIndex Then
                        TabControlBox.SelectedIndex = CurrentIndex
                    ElseIf CurrentIndex > TabPageIndex Then
                        TabControlBox.SelectedIndex = CurrentIndex - 1
                    Else
                        If CurrentIndex < TabControlBox.TabCount Then
                            TabControlBox.SelectedIndex = CurrentIndex
                        Else
                            TabControlBox.SelectedIndex = 0
                        End If
                    End If
                End If
            End If
    
        End Sub

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Tab Questions

    where do you set tabPageIndex? it never changes in your code + also your code doesn't prevent removing the last tabpage

  12. #12

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Tab Questions

    As stated, this code was just for closing the tab - not the other functions, as I didnt think those mattered to the problem

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Tab Questions

    Here is the full code.
    updated to prevent the user from closing the last tab.

    Code:
        Private Sub MainWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Tp As New TabPage
            Dim NewTab As New Tab
            NewTab.Show()
            NewTab.TopLevel = False
            NewTab.Dock = DockStyle.Fill
            Tp.Controls.Add(NewTab)
            TabControlBox.TabPages.Add(Tp)
        End Sub
    
        Private Sub MainWindow_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDoubleClick
            Dim Tp As New TabPage
            Dim NewTab As New Tab
            NewTab.Show()
            NewTab.TopLevel = False
            NewTab.Dock = DockStyle.Fill
            Tp.Controls.Add(NewTab)
            TabControlBox.TabPages.Add(Tp)
            TabControlBox.SelectedTab = Tp
        End Sub
    
        Private Sub TabControlBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControlBox.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Right Then
                If TabControlBox.TabCount > 1 Then
                    If Not TabPageIndex = -1 Then
                        Dim CurrentIndex As Integer = TabControlBox.SelectedIndex
                        TabControlBox.Controls.RemoveAt(TabPageIndex)
                        If CurrentIndex < TabPageIndex Then
                            TabControlBox.SelectedIndex = CurrentIndex
                        ElseIf CurrentIndex > TabPageIndex Then
                            TabControlBox.SelectedIndex = CurrentIndex - 1
                        Else
                            If CurrentIndex < TabControlBox.TabCount Then
                                TabControlBox.SelectedIndex = CurrentIndex
                            Else
                                TabControlBox.SelectedIndex = 0
                            End If
                        End If
                    End If
                End If
            End If
        End Sub
    
    End Class
    Last edited by codenewbie; Jan 26th, 2013 at 09:03 PM.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Tab Questions

    TabPageIndex will always equal 0, therefore:

    Code:
    TabControlBox.Controls.RemoveAt(TabPageIndex)
    is always equal to:

    Code:
    TabControlBox.Controls.RemoveAt(0)

  15. #15

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Tab Questions

    Thank you for the tip!
    I changed out the code, but didnt notice any difference when running the program

    Right click still closes the incorrect tab when trying to close the selected tab.

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Tab Questions

    Code:
    Private Sub TabControlBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControlBox.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If TabControlBox.TabCount > 1 Then
                tabPageIndex = Enumerable.Range(0, TabControl1.TabCount).Where(Function(x) TabControl1.GetTabRect(x).Contains(e.Location)).FirstOrDefault
                If Not TabPageIndex = -1 Then
                    Dim CurrentIndex As Integer = TabControlBox.SelectedIndex
                    TabControlBox.Controls.RemoveAt(TabPageIndex)
                    If CurrentIndex < TabPageIndex Then
                        TabControlBox.SelectedIndex = CurrentIndex
                    ElseIf CurrentIndex > TabPageIndex Then
                        TabControlBox.SelectedIndex = CurrentIndex - 1
                    Else
                        If CurrentIndex < TabControlBox.TabCount Then
                            TabControlBox.SelectedIndex = CurrentIndex
                        Else
                            TabControlBox.SelectedIndex = 0
                        End If
                    End If
                End If
            End If
        End If
    End Sub

  17. #17

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    50

    Re: Tab Questions

    .paul. - thank you for your help - your code worked perfectly.

    If you have a free minute, may I ask what this line of code is doing, I dont understand what is happening here.

    Code:
    TabPageIndex = Enumerable.Range(0, TabControlBox.TabCount).Where(Function(x) TabControlBox.GetTabRect(x).Contains(e.Location)).FirstOrDefault

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Tab Questions

    it returns the index of the tabpage that you clicked on

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