Windows Develop Bookmark and Share   
 index > Windows Forms General > Disabling the tabcontrol from cliking
 

Disabling the tabcontrol from cliking

Hi all i am a newbie in development
I have a window application in vb.net in VS 2003 there is a tabcontrol which has three tab pages.
i want that user was not able to change tabpages by clicking tabcontrol rahter than it is done by clicking button on tabpages how it is possible
please help me
thanks in advance
rak4u  Thursday, January 15, 2009 6:28 AM

Hi Rak4u,

Based on my understanding, you have a TabControl on a form. You don't want the user to switch among the tabpages by clicking the tab header. Instead, he can only do this by clicking the button on each tabpage. If I'm off base, please feel free to let me know.

When the user clicks onthe tab header of a TabControl, the TabControl receives WM_LBUTTONDOWN windows message and judge which tab header is clicked and then switch to that tab page. To prevent this, we can capture the WM_LBUTTONDOWN message and just discard it.

In addition, the user can also press Ctrl+Tab key to switch among the tabpages. To prevent this, we can override the OnKeyDown method and do nothing when Ctrl+Tab is pressed.

The following is a sample:
public class MyTabControl : TabControl
{
int WM_LBUTTONDOWN = 0x201;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN)
{

}
else
{
base.WndProc(ref m);
}

}

protected override void OnKeyDown(KeyEventArgs ke)
{
if (ke.Control && ke.KeyCode == Keys.Tab)
{
ke.Handled = true;
}
else
{
base.OnKeyDown(ke);
}
}
}

Use the derived TabControl instead of the standard TabControl on your form. The above code works fine on my side. Please try it in your application to see if there's any problem.

BTW, the IMessageFilterinterface allows an application to capture a message before it is dispatched to a control or form. A class that implements the IMessageFilter interface can be added to the application's message pump to filter out a message or perform other operations before the message is dispatched to a form or control. To add the message filter to an application's message pump, use the AddMessageFilter method in the Application class.

Obviously, it's not appropriate to add a message filter to the whole application in your scenario because what you need to do is to capture the message sent to just the TabControl.

Sincerely,
Linda Liu

Linda Liu  Monday, January 19, 2009 4:03 AM
Here too!

pasting this

Try Tab Control's selecting event

Private Sub TabControl1_Selecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
If tab = 1 Then
e.Cancel = True
Else

End If

tab = 1
End Sub

Arjun Paudel
Arjun Paudel  Thursday, January 15, 2009 9:43 AM
I've got this mental image of the user of your program clicking on the tab over and over again. Then looking at the bottom of the mouse to see what could be wrong. Anyhoo, use Arjun's code, but you'll need to set a boolean class member to true to indicate that the Selecting event fired because of the button.
Hans Passant.
nobugz  Thursday, January 15, 2009 12:32 PM
Dear Hans!

Thanks for the comment, but this thread got generated here

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/7286d461-a3bb-4584-972c-e6005a701af8

I am not able to think correct event for his problem.

I am sure your input will give us some idea, main problem is validating event which does not fire in tabpage.

Do you suggest processing windows message? I am not sure since leave, and selectedIndexchanged events are not good for this.

Thank you for your expert opinion, I always read threads you have participated(windows forms, visual basic) which are very valuable to me.

Happy New Year 2009

Arjun,

Arjun Paudel
Arjun Paudel  Thursday, January 15, 2009 1:17 PM
Happy New Year Arjun. Here's how to do it:

Public Class Form1

Private mButtonClicked As Boolean

Private Sub btnShowPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowPage2.Click
mButtonClicked = True
Try
TabControl1.SelectedTab = TabPage2
Finally
mButtonClicked = False
End Try
End Sub

Private Sub TabControl1_Selecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
If e.TabPage Is TabPage2 And Not mButtonClicked Then e.Cancel = True
End Sub

End Class


Hans Passant.
nobugz  Thursday, January 15, 2009 1:35 PM
The problem is: there is no selecting event in TabControl(.net 1.1)

I think that is available in 3.0 and + only

Thanks Again
Edit: I have suggested that in another thread

Arjun Paudel
Arjun Paudel  Thursday, January 15, 2009 1:40 PM
Hans, Did you check this thread?

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/7286d461-a3bb-4584-972c-e6005a701af8

Arjun Paudel
Arjun Paudel  Thursday, January 15, 2009 1:42 PM

First of all thanks Arjun for ur interest & help & also thanks hans. I read lot of ur post for help. I only entered in the programming job since last 2 month. I am working in Publighing house. they have no IT department Or IT staff. They want to develop their data entry program in vb.net & SQL server.
AS i mention i am a newbie no experience of any type. So i develop most of their model window application by taking help og internet & books but here i got in problem

My application is window application for dataentry. It has three tabpages First tab page is filter which is use to specify fitering condition to filter data from database & display that in datagrid present in second tab page browse. oN BROWSE TABPAGE i can present that data in excel format & also in crystal report.
then there is third tabpage ADDEDIT. On which ican edit the record or add new record. Tell me how u think about my approach to develop that application. Any suggesion help orimprovement in any aspect will welcom & must be appriciated

Now coming on current problem
As there is no selecting event in VS 2003 So what u think about usingIMessageFilter.PreFilterMessage
function & trap the click event od tabcontrol. I dont know how to use that function.

please tell me is that function is useful in this scenerio & if yes than please tell me any link to useIMessageFilter.PreFilterMessage function to capture click event of tabcontrol

againthanks all for ur support & help

bye

rak4u  Friday, January 16, 2009 4:20 AM
I can be done by overriding WndProc() and catching the TCN_SELCHANGING. Why don't you put this one on your to-do list and focus on the next part of the project. Looking at the bottom of the mouse and all that...
Hans Passant.
nobugz  Friday, January 16, 2009 5:30 AM
Please explainIMessageFilter.PreFilterMessage functionin detail or give any link where i know about that I have no idea about that.
My project is completed in anyway & this is a shortcoming mention by my boss (its not my idea) So i have to work on this only because all other work had done.& after completed this project i will get next assignment
so please help.
rak4u  Friday, January 16, 2009 5:45 AM
Hmm, excellent opportunity to point out that you can't get the job done with grossly outdated tools...
Hans Passant.
nobugz  Friday, January 16, 2009 5:56 AM
Hey we have VS 2003,2005,2008 in company. But where i learn vb.net they taught at VS 2003 version
so i had alittle experience on 2003 version so i choose 2003 version for working can i now upgrade to 2005 or 2008 will it affect my previously developed project please comment
rak4u  Friday, January 16, 2009 6:23 AM
Backup your project and open it in 2008 (or backup while opening), it should ask to upgrade the code, just do it and see the errors, and decide if that is manageable.

I was just thinking about something like this, since validating fires when control inside the tabpage is selected

In form's load event select a control inside first tab page

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Select()
Button1.CausesValidation = False ' not causing validation when user presses the button
End Sub

Private Sub TabPage1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TabPage1.Validating
If tab = 1 Then
e.Cancel = True

Else
e.Cancel = False
End If
End Sub

and

Private Sub TabPage1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Enter
tab = 1 ' setting the selected tab when user gets back to this tab
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tab = 2
TabControl1.SelectedIndex = 1
End Sub




Arjun Paudel
Arjun Paudel  Friday, January 16, 2009 8:51 AM

This is the same way what i had used but there is one error when i close application then it is not closed.

I used putting Tab=2 in closing event then application will close but by clicking two times to close because in first time in closing event the tab value change to 2 then it will close please solve this problem

thanks

rak4u  Friday, January 16, 2009 9:06 AM

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

e.Cancel = False

End Sub


Arjun Paudel
Arjun Paudel  Friday, January 16, 2009 9:07 AM
thanks for ur so much help I apriciate that
now it is working
but do u think it is right approach to solve the problem or in programming solve ur problem any way give advise
rak4u  Friday, January 16, 2009 9:24 AM
when you close the form, it cause validation to fire on the tabcontrol, when validating fires e.cancel is set to true, then you cant close the form.

If you are going to lose nothing by closing the form then setting e.Cancel = False is ok



Arjun Paudel
Arjun Paudel  Friday, January 16, 2009 11:12 AM
I am not said about closing event that is perfectly finebut the whole procedure to stop clicking tabcontrol by assining the tab variable is this a correct approach

thanks
rak4u  Friday, January 16, 2009 11:57 AM

Hi Rak4u,

Based on my understanding, you have a TabControl on a form. You don't want the user to switch among the tabpages by clicking the tab header. Instead, he can only do this by clicking the button on each tabpage. If I'm off base, please feel free to let me know.

When the user clicks onthe tab header of a TabControl, the TabControl receives WM_LBUTTONDOWN windows message and judge which tab header is clicked and then switch to that tab page. To prevent this, we can capture the WM_LBUTTONDOWN message and just discard it.

In addition, the user can also press Ctrl+Tab key to switch among the tabpages. To prevent this, we can override the OnKeyDown method and do nothing when Ctrl+Tab is pressed.

The following is a sample:
public class MyTabControl : TabControl
{
int WM_LBUTTONDOWN = 0x201;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN)
{

}
else
{
base.WndProc(ref m);
}

}

protected override void OnKeyDown(KeyEventArgs ke)
{
if (ke.Control && ke.KeyCode == Keys.Tab)
{
ke.Handled = true;
}
else
{
base.OnKeyDown(ke);
}
}
}

Use the derived TabControl instead of the standard TabControl on your form. The above code works fine on my side. Please try it in your application to see if there's any problem.

BTW, the IMessageFilterinterface allows an application to capture a message before it is dispatched to a control or form. A class that implements the IMessageFilter interface can be added to the application's message pump to filter out a message or perform other operations before the message is dispatched to a form or control. To add the message filter to an application's message pump, use the AddMessageFilter method in the Application class.

Obviously, it's not appropriate to add a message filter to the whole application in your scenario because what you need to do is to capture the message sent to just the TabControl.

Sincerely,
Linda Liu

Linda Liu  Monday, January 19, 2009 4:03 AM

You can use google to search for other answers

Custom Search

More Threads

• ListView Problem
• Sending an encrypted email attachment
• DataGridComboBoxColumn
• S.O.S - Align ComboBox's DropDownList to the right
• How to: SplitContainer with 4 columns?
• how can i add a new form in vc++2005 express
• Can't choose label size
• Hook shortcut key over the system
• Text with different colours in DataGridView Cell
• BUG: LinkLabel