tags:

views:

61

answers:

2

Software Utilize : C#, VS-2005

Is This Possible to override Shift+Tab Function/Method or detect Shift+Tab Function and Utilize it with Backspace.?

In Shot replace Shift+Tab Function with Backspace. And Then Backspace will Behave like Shift+Tab:

Is this possible in C#?

+1  A: 

I suppose you are working on a win-form. Register a key down event:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Tab && e.Shift)
    {
        // act like a backspace is pressed
    }
    else if (e.KeyCode == Keys.Back)
    { 
       SendKeys.Send("+{TAB}");   // simualte a shift-tab press
    }
}

To resolve the issue @liggett78 mentioned in the comment, you can set

form.KeyPreview = true;

to handle all the key events of child controls in the KeyDown event of the from.

EDIT: To prevent deleting a character in textbox when pressing BACKSPACE, you can:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Back)
    {
        e.SuppressKeyPress = true;
        SendKeys.Send("+{TAB}"); 
    }
}
Danny Chen
Won't work if you have controls on the form - which is virtually always the case - and one of them (e.g. a TextBox) is focused.
liggett78
@Danny, act whatever you like e.g.
mahesh
@liggett78: see my edit.
Danny Chen
@Danny It is not solution.Read carefully my demand
mahesh
@mahesh: In your question you want "pressing BACKSPACE will behave like Shift+Tab". What is the behavior of Shift+Tab in your application?
Danny Chen
@Danny, To get Previous control focus like textbox as usual shift+tab works
mahesh
@mahesh: I think I know what you want now. See my edit code within `SendKeys.Send` method.
Danny Chen
Nice approach. But, unfortunately, some of the components, for example textbox, handle backspace before main form. If you know how to disable this behavior, please update your answer.
MAKKAM
Nice works Danny. I accept the same.
mahesh
@Makkam I didn't get u. It's works nice in winforms.
mahesh
@mahesh yes, the behavior is clear. But, when I press the backspace on active textbox with sometext this handler first cuts the last letter of the text then goes to the previous control in the tab sequence. However, if that answer works for you, you should mark it as right.
MAKKAM
@MAKKAM it's true. do u have any solution for knowledge?
mahesh
@MAKKAM @mahesh: If we resolve the issue you mentioned, how can user delete a character in the textbox? He can't. So the root problem is: Switching the behavior between shift-tab and backspace is really not a good idea.
Danny Chen
@MAKKAM @mahesh: See my edit, how can we make this.
Danny Chen
@Danny Actually, by the question statement user CAN delete a character pressing Shift-Tab. I agree with you, that's not a good idea.)
MAKKAM
mahesh
No Need to type it on each object event/method
mahesh
@Danny Edit Your Code. From textbox method to Form1_keydown method
mahesh
@Mahesh: oops sorry I arrived at home and used a new test win-form project without setting `form.KeyPreview = true;`. You are correct.
Danny Chen
A: 

Override ProcessDialogKey or ProcessTabKey on your form.

liggett78