Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a usercontrol which is derived from control, can i override the TabStop property to have some modification.?
If so please let me know, how to override the tabstop property in my usercontrol.

What I have tried:

i have tried to override the tabstop in my usercontrol, but the tabstop property is not listed in override properties
Posted
Updated 25-May-16 21:00pm
Comments
BillWoodruff 26-May-16 6:02am    
I think you need to state clearly what your goal is here: how exactly do you want your UserControl to "behave" when:

1. an instance of it is on a Form

2. an instnace of it is in another container, like a Panel, on a Form

Exactly what do you want to see happen when the Tab Key is pressed in these scenarios ?

1 solution

Tabstop is just a bool, which tells the system that the user input focus can go to the control when he presses the TAB key: if it's true, the control is included in the "tabable" controls collection at the position indicated by the TabIndex property. Overriding it (which you can't do as it's not marked virtual, abstract, or override) wouldn't allow you to add and real functionality to the tab process. You can hide teh original implementation:
C#
public new  bool TabStop { get; set; }
Which would probably prevent the TAB key from ever being used to enter it, but that's about all.
If you want to modify TAB behaviour (and I really don't recommend it) you probably need to override WndProc ProcessCmdKey in the parent container and filter out TAB yourself.

Ignore WndProc, you need ProcessCommandKey (my caffeine hadn;t kicked in fully last time):
C#
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    Keys keyPressed = (Keys)(int)msg.WParam;
    if (keyData == Keys.Tab)
        {
        Console.WriteLine("Tab!");
        return true;
        }
    return base.ProcessCmdKey(ref msg, keyData);
    }
But seriously: Faff with TAB functionality at your own risk! Users are used to it doing particular things, and you really don't want to subvert that or they will hate your app with a passion you will not believe...
 
Share this answer
 
v2
Comments
AmalRaj@Sync 26-May-16 3:03am    
Thanks. How to identify the tab key pressed through WndProc?
OriginalGriff 26-May-16 3:30am    
Answer updated.
AmalRaj@Sync 26-May-16 3:41am    
when i press tab key from other controls, the override processcmdkey doesnt hit.

I want to stop the focus of my usercontrol when moving from other controls using tabkey
OriginalGriff 26-May-16 3:58am    
I did say "...in the parent container..."
You can't trap in in your control because it's already been processed. Almost certainly, you need to override the method for the form that your UserControl is part of.
AmalRaj@Sync 26-May-16 4:48am    
the usercontrol which i placed in a panel only gets focus even when the TabStop for usercontrol is set to false.

But the same usercontrol which is not placed in a panel(simply placed in form), didnt get focus when i set TabStop to false.


is there any reason why the userCOntrol gets focused only when it is placed inside a panel?

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