How am I able to disable a tab from selecting? [duplicate] - c#

This question already has answers here:
How can I disable a tab inside a TabControl?
(26 answers)
Closed 7 years ago.
I have a tab control with couple of tabs on it, I want to disable one of the tabs while checking a radio button on the other tab page. I can disable the controls on it by using tab.Enabled = False; But I want to know how I am able to disable the whole page from even clicking on the tab. The way I did I can select the tab still, just the controls on it are disable.

There's no native way to disable just one tab and leave the rest working. You'll need to make your own TabControl and use the Selecting event to detect when someone is about to enter your tab and disable it.
Here's a really basic example that stops you from entering tabindex 1.
// Make your own control deriving from TabControl
class CustomTabControl : TabControl
{
// Subscribe to the Selecting event
public CustomTabControl()
{
Selecting += tabSelecting;
}
private void tabSelecting(object sender, TabControlCancelEventArgs e)
{
// You would put your own logic here to detect which tabs to disable
// For this example I'm disabling access to tab in index 1
if (e.TabPageIndex == 1)
{
e.Cancel = true;
}
}
}
If you want the tab to grey out you'll need to override the DrawItem even and paint the tab yourself. If I have the code for that I'll update my answer in a bit.
Edit: It seems the example for custom drawing is pretty long, but here is the tutorial I used to create my own tabs.
http://www.codeproject.com/Articles/91387/Painting-Your-Own-Tabs-Second-Edition

Related

Tab index is not working on radio buttons

This is the part of my form that I am asking about
This is the tab index:
The problem that the tab goes from Farmer Audi Status to Yes, then to Ownder Bank Name instead of going to No
please notice that the yes and no already have 0.1.6.0 and 0.1.6.1 respectively.
could you help me please?
Notice
both radio buttons has TabStop property to True
From How to: Set the Tab Order on Windows Forms (MSDN):
A radio button group has a single tab stop at run time. The selected button (that is, the button with its Checked property set to true) has its TabStop property automatically set to true, while the other buttons have their TabStop property set to false.
In other words, what you're seeing is normal. Those "Yes/No" radio buttons are in the same group, and you can't tab between radio buttons in the same group. As you tab, you'll only focus on the currently selected one, then move to the next control on the form (in your case, a TextBox).
To work around this, you could place each radio button in its own container (such as a Panel), which means you'd have two "groups" each with one radio button. But then you lose the built-in functionality that automatically deselects one radio button when you select the other. Your user will be able to select both radio buttons, so you'd need to add some logic that disables the other. If you decide to try that, experiment with the radio buttons' CheckedChanged or Click / MouseClick events.
As Steve said, and as stated in the answer he linked to, the way it works out-of-the-box is expected behavior for Windows, so think twice before overriding it unless you have a good reason for doing so.
It worked for me!
first you have to create a method like this:
private void TabStopChanged(object sender, EventArgs e)
{
((RadioButton)sender).TabStop = true;
}
and then, put this in your Form_Load event:
private void Form_Load(object sender, EventArgs e)
{
foreach (var item in this.Controls)
{
if (item.GetType() == typeof(RadioButton))
((RadioButton)item).TabStopChanged += new System.EventHandler(TabStopChanged);
}
}
For radio buttons, you don't have to use Tab to navigate. Just use right and left keys to traverse radio buttons.
Check out this link to read more - https://www.csun.edu/universal-design-center/web-accessibility-criteria-tab-order

Adding close button on tab header in c# winforms [duplicate]

My scenario is the following:
I am working on a winforms application in C# that has a button inside the main page of a tabcontrol that will generate another tabpage each time that it is clicked. Each new tabpage will contain a layout defined by a user control.
My Questions are:
How can I allow the user to then close one of the tabs that were created dynamically at runtime?
How might I go about modifying the tabcontrol itself so that it has a small 'X' in each tab that the user may click on in order to close that particular tab? (Like Firefox has)
How can I expose the SelectedIndex property of the tabcontrol to the user control if I want to close the tab with a button inside the user control instead?
I found this code and was very helpful to me:
private void tabControl_MouseUp(object sender, MouseEventArgs e)
{
// check if the right mouse button was pressed
if(e.Button == MouseButtons.Right)
{
// iterate through all the tab pages
for(int i = 0; i < tabControl1.TabCount; i++)
{
// get their rectangle area and check if it contains the mouse cursor
Rectangle r = tabControl1.GetTabRect(i);
if (r.Contains(e.Location))
{
// show the context menu here
System.Diagnostics.Debug.WriteLine("TabPressed: " + i);
}
}
}
}
TabControl: How To Capture Mouse Right-Click On Tab
I created a derived tab control about one year ago. I am not going to post the source here, because it's about 700 lines long and coded quite messy. Maybe I will find some time to clean the code up and then release it here. For now I will briefly outline the way it is build.
Each tab page has a 'X' icon to the left of the title and the tab pages support reordering by drag and drop and moving them between multiple tab control.
I choose the easy way to get the icon on the tab pages. The tab control has the TabControl.ImageList property and a tab page has a TabPage.ImageIndex property. So I just added three icons to a image list - normal, hover, pressed - and process the mouse events.
With TabControl.GetTabRect() you can test if the mouse is over a specific tab pages and with some math you find if it is over the icon. Then you just need to change the icon depending on the mouse button state and eventually remove the tab page under the mouse if the button was pressed.
The main problem with this solution is, that calculating if the mouse is over the icon requires to know where the icon is painted relative to the tab page and this might change with a new windows version. And the icon is to the left of the title, but that does not look too bad.
I did the following:
on the create (add) TabPage stage, I added a toolStrip
ToolStrip ts = new ToolStrip();
ts.Dock = DockStyle.Top;
ts.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
Then, create the X button and add it to toolstrip
ToolStripButton ToolStripButton = new ToolStripButton("X");
ts.Items.Add(ToolStripButton);
create an event on clicking the X button
ToolStripButton.Click += new EventHandler(ToolStripButton_Click);
add toolstrip to the tabpage
tabControl1.TabPages[curenttabpage].Controls.Add(ts);
now for the ToolStripButton_Click is as follows:
void ToolStripButton_Click(object sender, EventArgs e)
{
ToolStripButton t = (ToolStripButton)(sender);
ToolStrip ts = t.Owner;
TabPage tb = (TabPage)
(ts.Parent);tabControl1.TabPages.Remove(tb);
}
Maybe it is not as you want, but it will work well.
I created a setup that is similar.
Each control that is added to the tab page at runtime is derived from a special base control I created. This base control has a close button (along with some other features such as safe to close flag).
Close tab code I'm using on my base control:
TabPage tabpage = (TabPage)this.Parent;
TabControl tabControl = (TabControl)tabpage.Parent;
tabControl.TabPages.Remove(parent);
I know this is an old thread but I did find this link that will allow you to "hide" tabs in an array and then you can just re-load the tabs you want at run time. I added this more for a place I can easily find it again.
This code might help throgh closing the tab controls with middle mouse click :
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != System.Windows.Forms.MouseButtons.Middle)
return;
for (int i = 0; i < MainTabControl.TabPages.Count; i++)
{
if (this.MainTabControl.GetTabRect(i).Contains(e.Location))
{
this.MainTabControl.TabPages.RemoveAt(i);
return;
}
}
}
It´s works!
TabPage tabpage = (TabPage)this.Parent;
TabControl tabControl = (TabControl)tabpage.Parent;
tabControl.TabPages.Remove(tabpage);

How to change the content of windows form with button control click? [duplicate]

This question already has answers here:
Change content in a windows form
(2 answers)
Closed 10 years ago.
I'm using visual studio 2010 and I have a winforms application.
When I start the application I show the user the 'MainMenu' which have:
1) New Game button
2) Options
each button click create a new form...
what I want to do when the user clicks on the options button is to change the MainWindow
content to be the Options window content.. and when he accepts the
changes on the options window -> return to the mainMenu view.. is it possible to do?
You could either use Panels and switch them round:
panelMain.Visible = false;
panelOptions.Visible = true;
Or you could have a numerous forms, and show and hide them:
frmOptions.Show();
frmMain.Hide();
Create multiple Panels and hide and show these on Button click.
Button_Click(object sender, EventArgs e)
{
PanelNewGame.Visible = true;
PanelOptions.Visible = false;
PanelMain.Visible = false;
}
Depending on the button you click handle them differently
One way you could do it is to use invisible labels and text fields for example, and when the user clicks the Options button then everything becomes clear.
I would advise that you take a look at the Tab Control class. This control allows you to switch between different blocks of content programmatically using the command
tabControl.SelectTab(i)
where i is the index of the tab.

how to make a tabpage inaccessible in tabcontrol

I currently have a tabcontrol with 3 tabpages (lets call them A,B, and C) the thing is I want the user to only be able to click certian tabs (if on tabA only can navigate to tabB, if on tabC only can navigate to tabA...) is there a way to do this? I'm a bit stumped, any help is appreciated thanks!
--C#2.0
--Windows Visual Studio 2005
You could hook up to the Selecting event on the TabControl and inside the event handler, you could check some class variable specifying which tab(s) are allowed to be clicked. If the one you're selecting doesn't match the variable, you can cancel the event.
In order to control which TabPages you can navigate to at a time, you can use the Enabled property on the TabPage. Set it to false in order to prevent any user from being able to interact with it.
In order to dynamically decide which tabs are enabled based on what tab is open you can use the Selected event on the TabControl (detailed here: http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selected.aspx). This will fire whenever you change the current tab on the TabControl. In here, you can determine what the current TabPage is and then use that to enable or disable TabPages as appropriate.
Add a handler to the TabControl.Selecting event to check whether you want to allow the tabpage selection.
Maybe something like
If (SelectedIndex == 1) //tab a
{
tabC.enabled = false;
tabB.enabled = true;
}

How can I add a button to the empty area of a TabControl in Winforms?

Grabbed the images from a similar question in WPF. Basically I want the extra buttons to do different things, not create or delete tabs. Actions related to the current tab which are gonna be the same for all the tabs in a single TabControl.
When I try to place my buttons on that area, the visual studio designer throws my control away, back to their original position. It doesn't allow me to put anything there.
Can this be done?
alt text http://www.freeimagehosting.net/uploads/92ca1b0a8c.png
alt text http://www.freeimagehosting.net/uploads/ff0d08e0ed.png
For specifically that visual experience I'd just create a separate tab with header "+" and add some logic on switching to it - so it will perform some other action instead of switching. The same can be done with multiple tabs I guess.
UPDATE
Actual code for this:
private void TabControl1_Selecting(Object sender, TabControlCancelEventArgs e)
{
if (e.TabPage... /* Do check whether some of your special TabPages is being selected */)
{
e.Cancel = true;
// All other TabPage-specific actions here
...
}
}
So it looks like they are using a tab as a button here. I would imagine they have the tab controls index change event fire a new tab into the tab control and force focus to it.

Categories

Resources