Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey team. I developed a small win forms that has a tab control for switching between tab pages.The form consists of two tab pages.
Thanks in advance for the help.

Criteria what i need:

1. tab pages should not get selected on mouse click

What I have tried:

Code for disabling the manual selection of tab.(NOT WORKING)

C#
 private void tabControl1_Click(object sender, EventArgs e)
        {            
           if (tabControl1.SelectedTab == tabPage2)
{
tabControl1.SelectedTab = tabPage1;
MessageBox.Show("You don’t have permission !!");
}   

else if(tabControl1.SelectedTab == tabPage1)
{
tabControl1.SelectedTab = tabPage2;
MessageBox.Show("You don’t have permission !!");
}   
   }


Code for redirecting from tabpage1 to tabpage2(WORKING GOOD)

C#
 private void t1NextTabRedirectButton_Click(object sender, EventArgs e)
        {    
            tabControl1.SelectedTab = tabPage2;
}
Posted
Updated 4-Jan-18 17:03pm
v4

Try it like this :
C#
public class myTabControl : TabControl
{
    protected override void OnSelecting(TabControlCancelEventArgs e)
    {
        e.Cancel = true;
        base.OnSelecting(e);
    }

    protected override void OnMouseClick(MouseEventArgs e)
    {
        MsgBox("! No Permission !");
    }

}
 
Share this answer
 
v2
Comments
Arvi.S 4-Jan-18 23:00pm    
In the above code you are trying to cancel the mouse click. I tried your code and i don't fetch with any positive result.. Any other way?? Small changes made to my code which i had changed abouve. Kindly see to it
Ralf Meier 5-Jan-18 2:43am    
I don't understand ...
Of course ... you could select the 2nd TabPage ... but the Control doesn't switch to it ...
So if this code-snippet isn't the thing you need ... what do you need exactly ?
Ralf Meier 5-Jan-18 8:26am    
I made a little change - perhaps you take a look on it.
Now the Control raises not longer the MouseClick-Event ...
C#
private void tabControl_MouseDown(object sender, MouseEventArgs e)
{
    if (tabControl.SelectedTab == tabPage2)
{
tabControl.SelectedIndex = 0;
tabControl.SelectedTab = tabPage1;
MessageBox.Show("Action Denied!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
      else if (tabControl.SelectedTab == tabPage1)
{
tabControl.SelectedTab = tabPage2;
MessageBox.Show("Action Denied!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
 
Share this answer
 
Comments
Arvi.S 4-Jan-18 22:57pm    
There is no response in the above code which you had sent. Is there any other solution?
Samuel Corpuz 4-Jan-18 23:57pm    
Did you try to edit it?
Samuel Corpuz 4-Jan-18 23:56pm    
go to the event of mousedown of your tabcontrol
then you will prompt to tabControl1_Mousedown

copy and paste this it will be fix
if (tabControl1.SelectedTab == tabPage2)
{
tabControl1.SelectedTab = tabPage1;
MessageBox.Show("You don’t have permission !!");
}

else if(tabControl1.SelectedTab == tabPage1)
{
tabControl1.SelectedTab = tabPage2;
MessageBox.Show("You don’t have permission !!");
}
Arvi.S 8-Jan-18 0:35am    
The same code i tried with the names i have kept. But even then its not working!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900