See that user selected a new Tabpage.. how?

  • Thread starter Thread starter Andre Nogueira
  • Start date Start date
A

Andre Nogueira

Hi there.
I would like to know it is possible in VB.Net 2003 to see that a user has
clicked a tab page.
My goal is to close the tab page if the user clicks a tabpage "separator"
(not sure how to call it.. but it's that part that when you click on it the
whole tab page becomes visible) holding the Shift key it closes.
I can already see if the Shift key is being pressed using the Windows API,
but I don't have a clue about where should I place that code...
Any help would be greatly appreciated.
Thank you!

André Nogueira
 
* "Andre Nogueira said:
I would like to know it is possible in VB.Net 2003 to see that a user has
clicked a tab page.
My goal is to close the tab page if the user clicks a tabpage "separator"
(not sure how to call it.. but it's that part that when you click on it the
whole tab page becomes visible) holding the Shift key it closes.
I can already see if the Shift key is being pressed using the Windows API,
but I don't have a clue about where should I place that code...

<URL:http://www.google.de/[email protected]>
 
Hi there.
Thank you for your help.
However, I tried changing the code a bit so it could see that Shift was
beeing pressed and, as such, close the tab instead but to no avail.
How can this be done?
The code I tried is below. The program just closes without any error message
or anything when opening the form that contains this control.
Thank you again for your patience!

Andre Nogueira

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Dim OldTab, NewTab As Integer
Try
If Me.TabPages.Count > 0 Then OldTab = Me.SelectedIndex
If m.Msg = (WM_REFLECT + WM_NOTIFY) Then
'We've received a WM_NOTIFY message
'get the NMHDR struct
Dim hdr As NMHDR
hdr = System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam,
hdr.GetType())
If hdr.code = TCN_SELCHANGING Then
'the selection is changing.
'Raise the SelectedIndexChanging event and allow user to change
Dim e As New System.ComponentModel.CancelEventArgs
RaiseEvent SelectedIndexChanging(Me, e)
m.Result = IIf(e.Cancel, New IntPtr(1), m.Result)
End If
End If
Catch ex As Exception
'ignore errors
Finally
'if we haven't cancelled the closing, call the default window procedure
If Me.TabPages.Count > 0 Then NewTab = Me.SelectedIndex
If m.Result.ToInt32 <> 1 Then MyBase.WndProc(m)
Dim ShiftState As Integer
ShiftState = GetKeyState(Keys.ShiftKey)
If (ShiftState And &H8000) And Me.TabPages.Count > 1 Then
If NewTab > OldTab Then NewTab = NewTab - 1
Me.TabPages.RemoveAt(OldTab)
Me.SelectedIndex = NewTab
End If
End Try
End Sub
 
Do you need to cancel the tab page change before it occurs?
Or do you just want to revert back once it has occured and some condition is
met?

Richard
 
I just want to close the tab if the user clicks on it while holding shift.

André Nogueira
 
OK, this seems to be getting unnecessarily complicated. Although I do like
the method shown, it is not the answer.

You can get the state of the Shift key without interop.
\\\
Me.ModifierKeys = Keys.Shift
///

You can use the OnselectedIndexChanged event to determine when a tabpage is
selected.

However, You cannot use this event to remove the tabpage with your desired
method. As soon as the tabpage is removed another is selected and, since I
assume you cannot release the shift key faster than your app can remove a
tabpage, that tabpage is removed. This carries on until there are no more
tabpages.

The good news is that you can use the Tabcontrols MouseUp Method, and in
fact this is the only choice since you may shift-click on the currently
selectedtab.
\\\
Private Sub TabControl1_MouseUp(...)...
If e.Button = MouseButtons.Left Then
If Me.ModifierKeys = Keys.Shift Then
TabControl1.TabPages.Remove(TabControl1.SelectedTab)
End If
End If
End Sub
///
 
Hi there.
Thank you for your reply!
I soon found out that you can't release the Shiftkey quickly enough... So I
explored a bit and ended up trying the Mousedown event of the tab control.
Now I feel really dumb for not thinking about it before... :S
Thank you for your reply! It is indeed the best way to do it... Even better
than the Mousedown event (as I currently have to check the mouse position to
see what tab is underneath it)

André Nogueira

"Mick Doherty"
 
You're welcome.
I've been playing around with the tabcontrol forever and I didn't
immediately jump to the MouseUp event.
 
Back
Top