Results 1 to 35 of 35

Thread: [RESOLVED] TabControl remove tab with middle button click?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    11

    Resolved [RESOLVED] TabControl remove tab with middle button click?

    I am making a web browser in Visual Basic and I am trying to figure out how to make a middle mouse button (Scroll Button) click to close the tab that the mouse is hovering over (Like In Google Chrome). Can anybody help me make this?

    cwarcarblue11

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: TabControl remove tab with middle button click?

    There might be a better way but give it the try:

    NOTE: I'm using TabStrip control as it offers more flexibility.

    Code:
    Option Explicit
    
    Dim myTabIndex As Integer
    
    Private Sub Form_Load()
    Dim i As Integer
    
        TabStrip1.Tabs.Clear
        For i = 1 To 5
            TabStrip1.Tabs.Add i, , "Tab" & i
        Next i
    
    End Sub
    
    Private Sub TabStrip1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
        If GetTab(x + TabStrip1.Left, y + TabStrip1.Top) > 0 Then
            If Button = vbMiddleButton Then
                TabStrip1.Tabs.Remove myTabIndex
            End If
        End If
    End Sub
    
    Private Function GetTab(x As Single, y As Single)
    Dim i As Integer
    Dim tabLeft As Single
    
        myTabIndex = -1
        
        For i = 1 To TabStrip1.Tabs.Count
            tabLeft = TabStrip1.Left + TabStrip1.Tabs(i).Left
            If (tabLeft <= x And tabLeft + TabStrip1.Tabs(i).Width >= x) And _
                (TabStrip1.Tabs(i).Top <= y And TabStrip1.Tabs(i).Top + TabStrip1.Tabs(i).Height >= y) Then
                
                myTabIndex = i
                Exit For
            End If
        Next i
        
        GetTab = myTabIndex
    
    End Function

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    11

    Re: TabControl remove tab with middle button click?

    Unfortunately I am using Visual Basic Express Edition and it does not have have the tabstrip control. Can you use tabcontrol?

  4. #4
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: TabControl remove tab with middle button click?

    Cwar

    Visual Basic Express Edition sounds like a .Net app
    as opposed to VB6, but I could be wrong.

    Spoo

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: TabControl remove tab with middle button click?

    The Express edition is .Net (2005 or 2008).

    Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: TabControl remove tab with middle button click?

    cwarcarblue11, now that your thread is in the correct forum, use the MouseClick event, check for the Middle button then remove the selected tab:
    Code:
     Private Sub TabControl1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseClick
            If e.Button = Windows.Forms.MouseButtons.Middle Then
                TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
            End If
        End Sub
    Though I should point out that not all mice report the 'Middle' button as 'Middle' button in .Net, my basic 3 button logitech mouse here reports 2 left buttons and 1 right button, so any code looking for the 'Middle' button will never report a middle button. I think it has something to do with the SetPoint drivers, which is logitach's only x64 Vista/Win7 mouse drivers so hundreds of people are using it.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    11

    Re: TabControl remove tab with middle button click?

    Thanks JuggaloBrotha that worked!

  8. #8
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: TabControl remove tab with middle button click?

    Quote Originally Posted by JuggaloBrotha View Post
    Code:
     Private Sub TabControl1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseClick
            If e.Button = Windows.Forms.MouseButtons.Middle Then
                TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
            End If
        End Sub
    I was really lucky that google had indexed this so quickly, it was exactly what I have been searching for for a while.

    When I use this code with multiple tabs, it doesn't seem to work correctly and I'm not sure why. It works correctly when there is only one tab, but it gets sort of screwy when there is multiple tabs.

  9. #9

  10. #10
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: TabControl remove tab with middle button click?

    Quote Originally Posted by DevinG View Post
    I was really lucky that google had indexed this so quickly, it was exactly what I have been searching for for a while.

    When I use this code with multiple tabs, it doesn't seem to work correctly and I'm not sure why. It works correctly when there is only one tab, but it gets sort of screwy when there is multiple tabs.
    Define screwy, like what it is doing?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    11

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Yeah, actually it is screwy for me too. When there are 4 or more tabs, and you close one in the middle while you are on the first tab, it moves you to the end tab. Rarely does it because it is so small, but kind of annoying.

  12. #12
    Addicted Member Spirited Machine's Avatar
    Join Date
    May 2009
    Posts
    215

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Quote Originally Posted by cwarcarblue11 View Post
    Yeah, actually it is screwy for me too. When there are 4 or more tabs, and you close one in the middle while you are on the first tab, it moves you to the end tab. Rarely does it because it is so small, but kind of annoying.
    If you remove the selected tab, it's probably default behavior to select the newest tab. If that's not acceptable, add code to tell it which tab to select after one is removed.

  13. #13
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Yeah I'll need to code in which is selected after the tab is removed.

    The problem I don't know how to fix is that if I middle click a tab that I am not in, then the tab that I was in gets removed and the one I middle clicked and wanted to remove remains.
    (e.g I'm in tabpage1 and middle click tabpage2, well tabpage1 gets removed and tabpage2 remains)

  14. #14
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] TabControl remove tab with middle button click?

    You can get the tab from the click location. I think the function is called GetTab. Pass it the e.X and e.Y values (in the mouseclick event) and it will return the tab you clicked. Then just remove that instead of removing the selected tab.

  15. #15
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] TabControl remove tab with middle button click?

    To expand on Nick's post.

    In the MouseDown event check for the middle mouse button, if it is store the mouse location in a class level var (a Point() ) would work

    in the MouseClick event check for middle mouse button, if so get the tab at the location stored in your class level point var and remove that tab (you can also set the tab control to a specific tab, ie the first one or whichever after removing a tab, be sure to check that there's even a tab to go to first)

    in the MouseUp event set the class level point var to nothing (to clear it out)
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  16. #16
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Quote Originally Posted by JuggaloBrotha View Post
    In the MouseDown event check for the middle mouse button, if it is store the mouse location in a class level var (a Point() ) would work
    I'm not sure what a class level var is, or how it works. Please expand a little more

  17. #17
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] TabControl remove tab with middle button click?

    It is just a variable declared outside of any methods (so in the 'empty space' between the methods, but inside the 'Public Class <...>' and 'End Class' lines). It is a normal variable but it can be accessed from inside any method, hence it is often called a global variable. Variables declared inside a method are called local variables, because they can only be used inside that method. In .NET, global variables are also often called member variables or fields.

  18. #18
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Here's an example, it's a variable you declare outside of a Sub/Function and it's a variable that can be used in Subs & Functions:
    Code:
    Public Class Form1
        Private m_SomeVar As String 'This is a Class-Level var
    
        Private Sub Form1_Load (...) Handles MyBase.Load
            'm_SomeVar can be used here
        End Sub
    
        Private Sub Button1_Click (...) Handles Button1.Click
            'm_SomeVar can be used here as well
        End Sub
    
    End Class
    Edit: You've beaten me twice in the same thread today Nick...
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  19. #19
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Ah yes, I think I'm onto the right track. Thanks for the replies guys , much appreciated. Let's see how it goes for me.

  20. #20
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: [RESOLVED] TabControl remove tab with middle button click?

    I'm having some troubles getting the tabpage locations now.

  21. #21
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Here's what I ended up doing, I have a TabControl (TabControl1) on the form and I made sure it has a few tabs. Here's my code (and it defaults to selecting the 1st tab, if any, when a tab is removed):
    Code:
    Public Class Form1
    
        Private m_MouseCoords As Point
    
        Private Sub TabControl1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseClick
            If e.Button = Windows.Forms.MouseButtons.Middle Then
                If TabControl1.TabPages.Count > 0I Then
                    Dim TabPageIndex As Integer = -1I
                    For Counter As Integer = 0I To TabControl1.TabPages.Count - 1I
                        If TabControl1.GetTabRect(Counter).Contains(m_MouseCoords) Then
                            TabPageIndex = Counter
                        End If
                    Next Counter
                    If TabPageIndex > -1I Then
                        TabControl1.TabPages.RemoveAt(TabPageIndex)
                        If TabControl1.TabPages.Count > 0I Then TabControl1.SelectedIndex = 0I 'Selects the first one
                    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.Middle Then m_MouseCoords = New Point(e.X, e.Y)
        End Sub
    
        Private Sub TabControl1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseUp
            m_MouseCoords = Nothing
        End Sub
    
    End Class
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  22. #22
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Great JuggaloBrotha! That works perfectly, thanks!

  23. #23
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Just so I can understand a little better what I am using in that code, what is that 0I 1I stuff? It looks to me as if it is saying, if there are more than 0 tabpages, then that other stuff happens.

    Also, how does the following work:
    Code:
    1. Private m_MouseCoords As Point

  24. #24
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] TabControl remove tab with middle button click?

    0I and 1I mean simply 0 and 1. The I prefix indicates that they are integers. It is not necessary, as 0 and 1 are integers by default, but it could be used for clarification.
    There's more of these prefixes, and they can be useful. For example, 3.14D means that 3.14 should be a Decimal, instead of the (default) Double type.


    Quote Originally Posted by DevinG View Post
    Also, how does the following work:
    Code:
    1. Private m_MouseCoords As Point
    What do you mean? There's nothing 'working' there. It's just a variable declaration, of type Point.

  25. #25
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Ah so it was just a declaration, I was just confused why private was used.

    Thanks!

  26. #26
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] TabControl remove tab with middle button click?

    This article is for an older version of VB, but the info should be fairly similar:
    What is the difference between Dim/Private/Public/Global/Static/Const?

  27. #27
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Quote Originally Posted by NickThissen View Post
    0I and 1I mean simply 0 and 1. The I prefix indicates that they are integers.
    Pardon my being picky, but don't you mean suffix?

    Spoo

  28. #28

  29. #29
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Quote Originally Posted by NickThissen View Post
    Oops, yes of course.
    Haha.. no probs.

    BTW, as a newbie to .Net, I too had been wondering what those
    suffixes meant. Thanks

  30. #30
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Code:
    Private Sub TabControl1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseClick
            If e.Button = Windows.Forms.MouseButtons.Middle Then
                If TabControl1.TabPages.Count > 0I Then
                    Dim TabPageIndex As Integer = -1I
                    For Counter As Integer = 0I To TabControl1.TabPages.Count - 1I
                        If TabControl1.GetTabRect(Counter).Contains(m_MouseCoords) Then
                            TabPageIndex = Counter
                        End If
                    Next Counter
                    If TabPageIndex > -1I Then
                        TabControl1.TabPages.RemoveAt(TabPageIndex)
                        If TabControl1.TabPages.Count > 0I Then TabControl1.SelectedIndex = 0I 'Selects the first one
                    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.Middle Then m_MouseCoords = New Point(e.X, e.Y)
        End Sub
    
        Private Sub TabControl1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseUp
            m_MouseCoords = Nothing
        End Sub
    So I was tinkering with my tabpages and middle click removing them and I came across a problem:
    Make a new form with this code and then make a tabcontrol with 8 tabs, run it and click tabpage8 then tabpage7 and then try to remove tabpage4; unfortunately it removes tabpage3.
    Last edited by DevinG; Mar 4th, 2010 at 10:23 PM.

  31. #31
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] TabControl remove tab with middle button click?

    DevinG, that's a good observation, while I don't have any answers right now, I'll look into this when I get the time.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  32. #32
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Quote Originally Posted by JuggaloBrotha View Post
    DevinG, that's a good observation, while I don't have any answers right now, I'll look into this when I get the time.
    Thank you

  33. #33
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Should I make a new topic to get info on this question?

  34. #34
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] TabControl remove tab with middle button click?

    I'm able to replicate it, but I'm not able to offer any reasons to why (it's not making sense to me) the code simply shouldn't allow this scenario to happen, yet the TabControl does. I'm wondering if there's a bug in the FW or if I'm just plain missing something here.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  35. #35
    Junior Member
    Join Date
    Feb 2010
    Posts
    21

    Re: [RESOLVED] TabControl remove tab with middle button click?

    Any new information on this?

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