Block Tab Changing TabControl

Asked By Gerry H.
18-Nov-09 04:55 PM
Earn up to 0 extra points for answering this tough question.

Hello again.... back with more Q's jejeje xD...

How can I block  changing tabs from a TabControl Vb.NET , so the user is obligated to use a navigation panel.... setting enabled property to false  for each tab when user click a button is done, but makes the code very large.... i´m talking about 13 tabs, so if anyone knows a small func for blockin all click events on tabs i´ll be very gratefull---

 

 

tnx!! all

 

  Why would you want to do this?

Robbe Morris replied to Gerry H.
23-Nov-09 09:34 AM

It is standard UI functionality that if you show a Tab, user's can click on it.  You will confuse your users by making the Tab visible but not allowing them to use it.

Depending on the control, you can usually trap the Tab click and Tab mouseup/mousedown events.  Keep track of the currently selected Tab index that is valid.  Subsequent attempts to change tabs via the click event or mouse events should call a method that determines whether you want to allow the change or not.  If not, reset the selected Tab index to your last valid Tab index.

  re: Block Tab Changing TabControl

Chris replied to Gerry H.
06-Apr-10 12:15 PM
If you are using .net framework 2.0 or above it is really simple, even easier than the firs respondent explained it.  The TabControl has an event called Deselection with a TabControlCancelEventArgs as one of it's parameters.  The TabControlCancelEventArgs has an attribute called cancel.  In this event check for the conditions under which you do not want the user to be able to change tabs.  If they are met simply set the cancel attribute to true otherwise do nothing.

Example (in vb.net should be very similar in c#)

Private Sub TabControl1_Deselecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Deselecting

     If myCondition Then
          e.Cancel = True
     End If
End Sub

I hope this helps
Create New Account