C# 如何在Windows文本框中将字符串转换为大写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10104515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:26:50  来源:igfitidea点击:

How to convert string to uppercase in windows textbox?

c#winformstextboxuppercase

提问by Sukanya

I've a textbox in my windows application. It allows only alphabets and digits. I want when ever I type any alphabet, it should be converted to uppercase.How can I do that and in which event? I've used str.ToUpper() but the cursor is shifting to the beginning of the string. Please give me solution.

我的 Windows 应用程序中有一个文本框。它只允许字母和数字。我希望每当我输入任何字母时,它都应该转换为大写。我该怎么做,在哪种情况下?我使用过 str.ToUpper() 但光标正在移动到字符串的开头。请给我解决方案。

采纳答案by Ezio Auditore da Firenze

You just need to change CharacterChasing property to Upper.

您只需要将 CharacterChasing 属性更改为Upper.

textBox1.CharacterCasing = CharacterCasing.Upper

回答by Oded

You need to assign the results of ToUpperback to the textbox:

您需要将ToUpper返回的结果分配给文本框:

txtBox.Text = txtBox.Text.ToUpper();

Alternatively, set the CharacterCasingproperty of the textbox to Upper:

或者,将CharacterCasing文本框的属性设置为Upper

txtBox.CharacterCasing = CharacterCasing.Upper;

回答by daryal

Try to use KeyPress event and the handler should be similar to the following;

尝试使用 KeyPress 事件,处理程序应类似于以下内容;

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

回答by mihai

Maybe you should use the event: TextBox1_EditValueChanging.

也许您应该使用事件:TextBox1_EditValueChanging。

If each time the cursor moves at the first position, you can calculate the number of characters in your txt and shift the cursor after the last charachter.

如果每次光标移动到第一个位置,您可以计算您的txt中的字符数并将光标移动到最后一个字符后。

回答by Ali Issa

private void mytextbox_KeyPress(object sender, KeyPressEventArgs e)

{

e.KeyChar = Char.ToUpper(e.KeyChar);

}

回答by kashif

In properties of TextBox simply set CharacterCasing to Upper. It'll convert all entered character in uppercase.

在 TextBox 的属性中,只需将 CharacterCasing 设置为 Upper。它会将所有输入的字符转换为大写。

回答by Dave

  1. TxtInput //This is the textbox user inputs
  2. LblLengthstrong //Label to show length
  3. LblUpper //Label that shows it in uppercase
  4. LblLower //Label that shows it in lowercase
  5. LblRight //Label that shows shows last 3 characters
  6. LblSubscript //Label that shows characters 1 through 3
  1. TxtInput //这是用户输入的文本框
  2. LblLengthstrong //标签显示长度
  3. LblUpper //以大写显示的标签
  4. LblLower //以小写字母显示的标签
  5. LblRight //显示最后3个字符的标签
  6. LblSubscript //显示字符1到3的标签

//show length of all characters inputed

//显示所有输入字符的长度

 private void BtnLength_Click(object sender, EventArgs e)
        {
            LblLength.Text = TxtInput.Text.Length.ToString();
        }

//make to characters upper

//使字符向上

  private void btnUpper_Click(object sender, EventArgs e)
        {
            LblUpper.Text = TxtInput.Text.ToUpper();
        }

//make characters to lowercase

//将字符转为小写

 private void BtnLower_Click(object sender, EventArgs e)
        {
            LblLower.Text = TxtInput.Text.ToLower();
        }

//show last 3 characters

//显示最后3个字符

 private void BtnRight_Click(object sender, EventArgs e)
        {
            LblRight.Text = TxtInput.Text.Substring(TxtInput.Text.Length - 3);
        }

//show characters in position 1 through 3

//显示位置1到3的字符

 private void BtnSubscript_Click(object sender, EventArgs e)
        {
            LblSubscript.Text = TxtInput.Text.Substring(1, 3);
        }

//ASCII

//ASCII

  private void BtnGo_Click(object sender, EventArgs e)
        {
            string name;
            int letter;

            name = TxtInput.Text;

            for (int index = 0; index < name.Length; index++)
            {
                letter = name[index];
                MessageBox.Show(letter.ToString());
            }
        }

//Password

//密码

      int InNumTry = 0;
    private void BtnGo_Click_1(object sender, EventArgs e)
    {
        string password;
        password = TxtIn.Text;

            switch (password)
            {
                case " ": MessageBox.Show("Passowrd is empty.");
                    break;

                case "MIKE": MessageBox.Show("Password is OK!");
                    FrmBOO newForm = new FrmBOO();
                    newForm.Show();
                    break;

                default:
                    InNumTry++;
                    MessageBox.Show("Invalid Passwrod, try again!");
                    TxtIn.Text = "";
                    TxtIn.Focus();
                    break;
            }

            if (InNumTry >= 3)
            {
                MessageBox.Show("You have tried too many times, have a good day.");
                TxtIn.Enabled = false;
            }
        }

// Adding timer(In the timer add the code under this (Add add timer1.Start(); in the start form)

// 添加定时器(在定时器中添加 this 下面的代码(在启动表单中添加 add timer1.Start();)

 private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime datetime = DateTime.Now;
            this.LblTime.Text = datetime.ToString();
        }

回答by Arunkumar Pushparaj

Right click the TextBox in the Designer, under Properties change CharacterCasing to Upper.

右键单击设计器中的 TextBox,在 Properties 下将 CharacterCasing 更改为 Upper。

回答by ePandit

Why to reinvent the wheel, just set 'CharacterCasing' property of textBox to 'Upper'. You don't need to write any code.

为什么要重新发明轮子,只需将 textBox 的“CharacterCasing”属性设置为“Upper”。您无需编写任何代码。

Make letters in textBox uppercase

将 textBox 中的字母设为大写

In case of masked textbox, you can use '>' (in mask property) to make following characters uppercase. e.g. For a input alphanumeric string (A-Z, 0-9) of length eight, use mask '>AAAAAAAA'. To restrict to letters only (A-Z), use '>LLLLLLLL'.

在蒙版文本框的情况下,您可以使用 '>'(在蒙版属性中)使后面的字符大写。例如,对于长度为 8 的输入字母数字字符串 (AZ, 0-9),请使用掩码 '>AAAAAAAAA'。要限制为仅字母 (AZ),请使用“>LLLLLLLL”。

Make letters in maskedTextBox uppercase

使 maskedTextBox 中的字母大写