简体   繁体   English

如何使用 c# 的按键事件将每个单词的首字母大写?

[英]How can i capitalize first letter of every word using keypress event with c#?

I must capitalize first letter of every word but with keypress event using C#.我必须将每个单词的第一个字母大写,但使用 C# 进行按键事件。 Right now every letter in the textbox gets capitalized, I added the code I use.现在文本框中的每个字母都大写,我添加了我使用的代码。 I cant figure out how to capitalize just the first letter, or what am I doing wrong.我不知道如何只将第一个字母大写,或者我做错了什么。 Can you help me?你能帮助我吗?

private void txt_name_KeyPress(object sender, KeyPressEventArgs e)
{
    e.KeyChar = (e.KeyChar.ToString()).ToUpper().ToCharArray()[0];
}

I got by using Keydown我通过使用 Keydown

private void tbxName_KeyDown(object sender, KeyEventArgs e)
    {
      
            if (tbxName.Text.Length == 0 || tbxName.SelectedText.Length == tbxName.Text.Length)
            {
                if ((e.Key >= Key.A) && (e.Key <= Key.Z))
                {
                    tbxName.Text = e.Key.ToString().ToUpper();
                    tbxName.SelectionStart = tbxName.Text.Length;
                    e.Handled = true;
                }
            }

            if (tbxName.Text.Length > 0)
            {
                int selectionStart = tbxName.SelectionStart;
                string character = tbxName.Text.Substring(selectionStart - 1, 1);

                if (character.Trim() == string.Empty)
                {
                    if ((e.Key >= Key.A) && (e.Key <= Key.Z))
                    {
                        tbxName.Text = tbxName.Text.Insert(selectionStart, e.Key.ToString().ToUpper());
                        tbxName.SelectionStart = selectionStart + 1;
                        e.Handled = true;
                    }
                }
            }

            if (e.Key == Key.Enter || e.Key == Key.Tab)
            {
                if (tbxName.Text.Length > 2)
                {
                    tbxDegree.Focus();
                    if (e.Key == Key.Tab)
                        e.Handled = true;
                }
                else
                {
                    MessageBox.Show("Kindly verify the Name Entered");
                    tbxName.Focus();
                }
            }
       
    }

You will need to keep track of previous key presses if you must go this route:如果您必须 go 这条路线,您将需要跟踪以前的按键:

private char PreviousChar;

private void txt_name_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Char.IsWhiteSpace(PreviousChar) || PreviousChar == '\0')
    {
        e.KeyChar = Char.ToUpper(e.KeyChar);
    }
    PreviousChar = e.KeyChar;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C# 中将字符串的每三个字母大写? - How can I capitalize every third letter of a string in C#? 如何使用char将C#中每个句子的第一个字母大写? - How can I capitalize the first letter of each sentence in C# using char? 如何使用XAML和C#在WP8中实现转换器以大写TexBlock中单词的首字母? - How to implement a converter in WP8 using XAML and C# for capitalize the first letter of a word in a TexBlock? 如何在C#中将大写的名字和姓氏大写? - How do I capitalize first letter of first name and last name in C#? regexp将每个单词的每个首字母大写而忽略一些 - regexp to capitalize every first letter of every word and ignore some C#-如何在段落中将首字母“大写”大写? (与正则表达式结合使用) - C# - How to capitalize first letter 'only' on a paragraph? (Combined with regex) 我如何在C#中使用textBox的KeyPress事件绑定一个CheckBox - How I can bound a checkBox with KeyPress event of a textBox in C# C# 将字符串中每个句子的首字母大写 - C# Capitalize first letter in in each sentence in a string C#:我希望单词的每个字母都以新行开头 - C#: I want every letter of a word to begin in a new line 正则表达式删除第一个单词并将第二个单词的第一个字符大写为 c# - Regex to remove first word and capitalize the first char of the second with c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM