【问题标题】:Converting to TitleCase with caps lock & shift pressed?在按下大写锁定和换档的情况下转换为 TitleCase?
【发布时间】:2017-05-15 07:46:19
【问题描述】:

以下方法需要成功获取用户输入并将其输入更新为标题格式格式,例如,当他们键入时,每个单词的开头都使用大写字母,这是在 win 表单项目的文本框中输入的。我对这种方法有疑问,因为它可以正确转换,直到我按下大写锁定或移位。如果我按住两个按钮不确定这是否会相互抵消,它也会起作用。 我一直在研究正则表达式,但不确定如何在这个类中实现它。请在下面找到该功能的代码,谢谢。

// User input stored in Temp Var
string myText = tbProductId.Text;

//
if (myText.Equals(null))
{
    // validation, check if the user has entered anything, if Null.
    MessageBox.Show("Please enter somthing");
}
else
{
    // convert to Title Case
    tbProductId.Text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInf‌​o.ToTitleCase(tbProd‌​uctId.Text);
    tbProductId.Focus();
    tbProductId.Select(tbProductId.Text.Length, 0);
    //Move Cursor to location of where error was found
}

【问题讨论】:

  • 有几种方法可以解决这个问题。两个这样的:逐个字符地监视他们的输入,并根据字母是第一个还是跟在空格后面,将字母转换为大写或小写;或等到最后并通过converter 运行整个字符串以将其更改为正确的格式。
  • 您可能还对Keyboard 课程感兴趣。
  • 最简单的一个:tbProductId.Text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInf‌​o.ToTitleCase(tbProd‌​uctId.Text.ToLower());。它首先将全部转换为小写,然后返回标题大小写作为结果。
  • @TetsuyaYamamoto 谢谢你的作品,非常感谢

标签: c# formatting


【解决方案1】:

你可以简单地使用:

tbProd‌​uctId.CharacterCasing = CharacterCasing.Lower

您解释的行为是标准的 docs 是正确的,这是解决它的简单方法。

我也同意@Tetsuya Yamamoto 的上述评论

【讨论】:

  • @Digvijay感谢您的 cmets 您的作品也为您加油
  • 很高兴我能帮上忙!
【解决方案2】:

最后的结果和答案感谢上面发帖的人

        // User input stored in Temp Var
        string myText = tbProductId.Text;
        var regex = new Regex(@"[^a-zA-Z0-9\s]");

        if (myText.Equals("") ||(regex.IsMatch(myText.ToString())))
        {
            MessageBox.Show("Please enter a Valid value no special chars or leaving this blank!!!!");
        }

        else 
        {
            tbProductId.Text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInf‌​‌​o.ToTitleCase(tbPr‌​od‌​uctId.Text.ToLow‌​er());
            tbProductId.Focus();
            tbProductId.Select(tbProductId.Text.Length, 0);
            //Move Cursor to location of where error

}

【讨论】:

    【解决方案3】:

    字符换字符 ...

        public void textBox1_Click(Object ob, EventArgs eventArgs){
            if (textBox1.Text.Length > 0)
            {
                string text = textBox1.Text;
                string tmpText = "";
    
                if (textBox1.Text.Length == 1)
                {
                    tmpText = text.ToUpper();
                }
                else
                {
                    for (int i = 0; i < text.Length; i++)
                    {
                        if (i < text.Length - 1)
                            tmpText += text[i];
                        else if (text[text.Length - 2] == ' ')
                            tmpText += text[text.Length - 1].ToString().ToUpper();
                        else
                            tmpText += text[i];
                    }
                }
                textBox1.Text = tmpText;
                textBox1.Focus();
                textBox1.Select(textBox1.Text.Length, 0);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 2018-08-01
      • 1970-01-01
      相关资源
      最近更新 更多