如何在C#中将字符串与制表符进行比较? [英] how can i compare a string with tab in c#?

查看:359
本文介绍了如何在C#中将字符串与制表符进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样做了,没有错误,但是没有检测到制表符

i did this, no error but it does not detect tab

switch (value)
{
    case "":
        return true;
    case " ":
        return true;
    case "\t":
        return true;
    default:
        return false;
}

推荐答案

在标准系统中,KeyDown,KeyUp和KeyPress事件不会响应Tab键.要检测此键,您需要覆盖为所有窗体控件定义的ProcessCmdKey方法.

请看一下

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

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



在这种情况下,如果您按Tab键,您将获得字符串值"Tab"



In this case if you press tab you will get the string value "Tab"


这应该有效,并且在快速测试应用程序中也可以运行

我通过调用以下方法的即时窗口对它进行了测试:

"\ t"-正常工作
Tab键直接按下进入即时窗口-VS将Tab键转换为4个空格
将Tab键压入记事本并粘贴到即时窗口中-正常工作(调试器将其视为\ t)

您确定源字符串实际上包含一个制表符而不是另一个空格表示吗?
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?


这应该可以.通常,输入到该开关的值具有一些额外的字符,或者如Parkings所说的不包含Tab.
This should work. Mostly the value coming into the switch have some extra characters or as said by Parkings doesn''t contain Tab.


这篇关于如何在C#中将字符串与制表符进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆