Click here to Skip to main content
13,732,381 members
Rate this:
 
Please Sign up or sign in to 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)

 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)

 private void t1NextTabRedirectButton_Click(object sender, EventArgs e)
        {    
            tabControl1.SelectedTab = tabPage2;
}
Posted 2-Jan-18 22:37pm
Updated 4-Jan-18 17:03pm
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try it like this :
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 !");
    }

}
  Permalink  
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 ...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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);
}
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 5 Jan 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100