Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i did this, no error but it does not detect tab
C#
switch (value)
{
    case "":
        return true;
    case " ":
        return true;
    case "\t":
        return true;
    default:
        return false;
}
Posted

In the standard system, the KeyDown, KeyUp and KeyPress events do not respond to the tab key.To detect this key you need to override the ProcessCmdKey method that is defined for all forms control.

Please have a look on that

C#
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
           base.ProcessCmdKey(ref msg, keyData);

            if (keyData == Keys.Tab)
            {
                string value = keyData.ToString();

                return true;
            }
            return false;
        }


In this case if you press tab you will get the string value "Tab"
 
Share this answer
 
That should work, and does in a quick test app i ran up

I tested it through the immediate window calling the method with the following:

"\t" - worked correctly
Tab keypress directly into immediate window - VS converted tab to 4 spaces
Tab keypress into notepad and pasted into immediate window - worked correctly (debugger saw it as \t)

are you sure your source string actually contains a tab and not another whitespace representation?
 
Share this answer
 
Comments
Sweety Khan 6-Sep-11 13:15pm    
yeh i m sure. i hve checked it many times n too fed up :(
wht r other whitespaces? i will try them
Sergey Alexandrovich Kryukov 7-Sep-11 1:56am    
You are wrong. Besides, why immediate constants of string type and comparisons of them in case statement? This is evil.
--SA
Sweety Khan 7-Sep-11 13:11pm    
oOi wht evil in it :( i have made a fn isIgnorable(string value) in which that switch is written bc the input contains many spaces, tabs and etc which i want to ignore.
Sweety Khan 6-Sep-11 13:23pm    
Oops the wrong output's reason is some thing else. u r right :)
Md. Rashim Uddin 6-Sep-11 13:29pm    
Is it for Winform??
This should work. Mostly the value coming into the switch have some extra characters or as said by Parkings doesn't contain Tab.
 
Share this answer
 

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