|
-
Feb 13th, 2010, 07:27 PM
#1
Thread Starter
New Member
[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
-
Feb 13th, 2010, 07:56 PM
#2
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
-
Feb 13th, 2010, 09:26 PM
#3
Thread Starter
New Member
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?
-
Feb 14th, 2010, 12:24 AM
#4
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
-
Feb 14th, 2010, 06:14 AM
#5
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
-
Feb 14th, 2010, 09:07 AM
#6
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.
-
Feb 15th, 2010, 02:01 PM
#7
Thread Starter
New Member
Re: TabControl remove tab with middle button click?
Thanks JuggaloBrotha that worked!
-
Feb 26th, 2010, 12:41 PM
#8
Junior Member
Re: TabControl remove tab with middle button click?
 Originally Posted by JuggaloBrotha
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.
-
Feb 26th, 2010, 01:00 PM
#9
Re: [RESOLVED] TabControl remove tab with middle button click?
-
Feb 26th, 2010, 01:00 PM
#10
Re: TabControl remove tab with middle button click?
 Originally Posted by DevinG
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?
-
Feb 26th, 2010, 01:18 PM
#11
Thread Starter
New Member
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.
-
Feb 26th, 2010, 01:22 PM
#12
Addicted Member
Re: [RESOLVED] TabControl remove tab with middle button click?
 Originally Posted by cwarcarblue11
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.
-
Feb 26th, 2010, 02:38 PM
#13
Junior Member
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)
-
Feb 26th, 2010, 02:43 PM
#14
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.
-
Feb 26th, 2010, 02:51 PM
#15
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)
-
Feb 26th, 2010, 02:58 PM
#16
Junior Member
Re: [RESOLVED] TabControl remove tab with middle button click?
 Originally Posted by JuggaloBrotha
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
-
Feb 26th, 2010, 03:05 PM
#17
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.
-
Feb 26th, 2010, 03:07 PM
#18
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...
-
Feb 26th, 2010, 03:15 PM
#19
Junior Member
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.
-
Feb 26th, 2010, 03:39 PM
#20
Junior Member
Re: [RESOLVED] TabControl remove tab with middle button click?
I'm having some troubles getting the tabpage locations now.
-
Feb 26th, 2010, 04:10 PM
#21
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
-
Feb 27th, 2010, 01:35 PM
#22
Junior Member
Re: [RESOLVED] TabControl remove tab with middle button click?
Great JuggaloBrotha! That works perfectly, thanks!
-
Feb 27th, 2010, 02:17 PM
#23
Junior Member
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:
Private m_MouseCoords As Point
-
Feb 27th, 2010, 02:26 PM
#24
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.
 Originally Posted by DevinG
Also, how does the following work:
Code:
Private m_MouseCoords As Point
What do you mean? There's nothing 'working' there. It's just a variable declaration, of type Point.
-
Feb 27th, 2010, 02:32 PM
#25
Junior Member
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!
-
Feb 27th, 2010, 02:36 PM
#26
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?
-
Feb 27th, 2010, 06:47 PM
#27
Re: [RESOLVED] TabControl remove tab with middle button click?
 Originally Posted by NickThissen
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
-
Feb 27th, 2010, 06:59 PM
#28
Re: [RESOLVED] TabControl remove tab with middle button click?
 Originally Posted by Spoo
Pardon my being picky, but don't you mean suffix?
Spoo
Oops, yes of course.
-
Feb 28th, 2010, 09:27 AM
#29
Re: [RESOLVED] TabControl remove tab with middle button click?
 Originally Posted by NickThissen
Oops, yes of course.
Haha.. no probs.
BTW, as a newbie to .Net, I too had been wondering what those
suffixes meant. Thanks
-
Mar 4th, 2010, 10:15 PM
#30
Junior Member
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.
-
Mar 5th, 2010, 09:22 AM
#31
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.
-
Mar 5th, 2010, 01:17 PM
#32
Junior Member
Re: [RESOLVED] TabControl remove tab with middle button click?
 Originally Posted by JuggaloBrotha
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
-
Mar 6th, 2010, 08:44 PM
#33
Junior Member
Re: [RESOLVED] TabControl remove tab with middle button click?
Should I make a new topic to get info on this question?
-
Mar 7th, 2010, 09:50 PM
#34
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.
-
Mar 10th, 2010, 08:22 PM
#35
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|