how to make a tabpage inaccessible in tabcontrol - c#

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

Related

Change happens after 2 mouse clicks when it should happen after a single click

I have a c# windows form application where I have a treeView inside a tabPage of a tabControl which is a part of the main form.
For the tree view, I click on the items of the treeView which I want to select then some change happens based on my selected Items.
I am using the AfterSelect event for item selection and and the mouseUp event for undoing the selection.
The item selection and deselection happens right away with a single click (no problem). The other change with should happen based on the selected items happens after two clicks! A single click either on the item node or outside the node's area do not trigger this change. I have to click again in order to see the change. That is wired. I am not using mouse double click events for this or something similar, I am only using the events I described above.
How can this be happening? and How to resolve it? Thanks.
EDIT: I am using my own multi-selection version of the treeView and I found (using debug) that when I get the selected nodes of the tree in the AfterSelect event after the first click is zero, then it is the number of selected nodes with the second click. How come this is happening when selected nodes are added and to the current selectedNodes list with every click in the overrided OnAfterSelect event of the treeView?
here is part of tree view code:
public List<TreeNode> SelectedNodes
{
get
{
return selectedNodes;
}
set
{
removeSelectionFromNodes();
selectedNodes = value;
selectNodes();
}
}
protected override void OnAfterSelect(TreeViewEventArgs e)
{
base.OnAfterSelect(e);
base.SelectedNode = null;
List<MSTreeNode> nodes = new List<MSTreeNode>();
.
.
.
removeSelectionFromNodes();
selectedNodes.Clear();
selectedNodes.AddRange(nodes);
selectNodes();
}
Maybe Treeview is losing focus in between clicks (?). You could try setting Treeview HideSelection property to False to keep the currently selected item highlighted when the control loses focus.
I tried to use the MouseDown event instead of the AfterSelect event. I override it in the my own multi-selection version of the treeView and used in the c# application I am developing but still it did not work. I am not sure how mouse events really work. If not used carefully, you may see wired behaviors.
Well, I ended up overriding the MouseUp and MouseUp events in my treeView subclass then I created an event which listens for changes in the selectedNodes list. If a change to the selectedNodes happnes in any of the mouse events this event is triggered. Then, I used the ChangedSelectedNodes event handler of the treeview instance in my application to do the other changes when there is a change in the node selection. This time it worked as expected.
I posted this in hope that it would be beneficial to anyone else who ran into the same problem like me.
P.S. Sometimes things do not work as you expect them to be and you just have fight and go through every other possibility until you find the solution.
Disable the hide selection option and use afterSelect option
in my project that works well

How to determine if a right click was performed on a Panel?

I have a Panel to play media. How can I determine if it was Right clicked? I want to bring up a ContextualMenu on the panel with (Pause, Play, Stop and Start Over)
just wire up your ContextMenu to the Panels ContextMenu property... another option is assign your handler to the MouseClick event of the Panel and check the event args for Button == MouseButtons.Right.
In visual studio on your form, add a contextmenustrip, and populate it with the values that you want. The panel has a property called contextmenustrip, all you need to do is set that to the one that you create and visual studio will do the rest for you.

totally disabling TabOrder on the form

WinForms: I don't want any tab order. I want myself be able to programatically handle all the tab orders on the form with some logic that I need.
How can I completely disable tab order? I assume after that I should deal with KeyDown event of each contorl or some similar event ....
You need to override the form's ProcessCmdKey() method. Test keydata == Keys.Tab and (Keys.Shift | Keys.Tab) to detect respectively a forward and a backward tab. Return true to indicate that you've used the key and it shouldn't be used anymore. Which defeats Winforms default handling for the Tab key. No additional changes are needed to the controls.
The form's ActiveControl property tells you which control currently has the focus, you'll need to use it to figure out which control should be focused next. Beware that it can technically be null. Watch out for controls that are embedded in a container control, like a Panel or UserControl. Making this work is definitely unpleasant, also very hard to maintain. Only do this if there are a limited number of controls on the form.
As Adrian said by setting tab stop to false you can disable it
a Function like this can be usefull to diable all tabstop
private void DiableTabStop(Control ctrl)
{
ctrl.TabStop = false;
foreach (Control item in ctrl.Controls)
{
DiableTabStop(item);
}
}
and calling it at form load
DiableTabStop(this);
One approach is to set the TabStop property of every control in the form to false. This will prevent the tab key from giving the controls focus.
If you don't want to do this manually for every control (e.g. in the design view), you can create a method that will iterate over all of the controls in the form's Controls collection and set the property on each one, then call it from your form's constructor.
In addition to disabling the tab stops for the pageframe, as you mentioned, YOU want to control which "tab" is active. You can have a custom property on your form of "WhichTab" should be shown. Then, override the click event and check if the incoming sender/eventarg page is that of another page... no matter what, force focus back to the "WhichTab" YOU are in control of setting... When ready to activate said page, tell the tab control object to ACTIVATE the new page to get displayed to the user.

How to tell when the user has clicked outside of the bounds your control?

I have a custom UserControl. I want to use it in a few different products, so I want something that can be implemented inside of the UserControl itself. I want to know when the user has clicked outside of the bounds of the UserControl so that I can hide it, similar to a ComboBox. How can I do that?
I tried handling the click event, but it only seems to fire if the click occured within the bounds of the control.
That's what the Capture property is designed to do. Set it to true and all mouse messages are routed to your control, even if it moves out of the window bounds. Check the e.Location property in the MouseDown event.
Hm, you may be able to accomplish what you want by listening to the GotFocus/LostFocus events. ComboBoxes give the drop downs focus when they open and close them when they lose focus.
do this
Select all controls on your form including form
In Property Window select MouseClick event
Now enter below Code in Common_MouseClick
Code:
if (!sender.Equals(yourControl))
{
yourControl.Visible=false;
}

.NET : Double-click event in TabControl

I would like to intercept the event in a .NET Windows Forms TabControl when the user has changed tab by double-clicking the tab (instead of just single-clicking it).
Do you have any idea of how I can do that?
The MouseDoubleClick event of the TabControl seems to respond just fine to double-clicking. The only additional step I would do is set a short timer after the TabIndexChanged event to track that a new tab has been selected and ignore any double-clicks that happen outside the timer. This will prevent double-clicking on the selected tab.
For some reason, MouseDoubleClick, as suggested by Jason Z is only firing when clicking on the tabs and clicking on the tab panel does not do anything, so that's exactly what I was looking for.
How about subclassing the TabControl class and adding your own DoubleClick event?

Categories

Resources