C# 在 CurrentItemChanged 上将文本框的内容设置为大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/615106/
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-04 10:19:50  来源:igfitidea点击:

Setting the contents of a textBox to upper-case on CurrentItemChanged

c#.netvisual-studiotextbox

提问by Sakkle

I would like the text in my textBox to be set to upper-case whenever currentItemChanged is triggered. In other words, whenever the text in the box changes I'd like to make the contents upper-case. Here is my code:

我希望在触发 currentItemChanged 时将我的 textBox 中的文本设置为大写。换句话说,每当框中的文本发生变化时,我都想让内容大写。这是我的代码:

private void rootBindingSource_CurrentItemChanged(object sender, System.EventArgs e)
{
    toUserTextBox.Text.ToUpper();
    readWriteAuthorization1.ResetControlAuthorization();
}

The event triggers for sure, I've tested with a messageBox. So I know I've done something wrong here... the question is what.

该事件肯定会触发,我已经使用 messageBox 进行了测试。所以我知道我在这里做错了……问题是什么。

采纳答案by BFree

Strings are immutable. ToUpper() returns a new string. Try this:

字符串是不可变的。ToUpper() 返回一个新字符串。尝试这个:

private void rootBindingSource_CurrentItemChanged(object sender, System.EventArgs e)
{
    toUserTextBox.Text = toUserTextBox.Text.ToUpper();
    readWriteAuthorization1.ResetControlAuthorization();
}

回答by Romias

I imagine that your question is Why your code is not working.

我想你的问题是为什么你的代码不起作用。

You are not assigning the "Uppered" text to the textbox again.

您不会再次将“Uppered”文本分配给文本框。

Should be:

应该:

private void rootBindingSource_CurrentItemChanged(object sender, System.EventArgs e)
{
    toUserTextBox.Text = toUserTextBox.Text.ToUpper();
    readWriteAuthorization1.ResetControlAuthorization();
}

回答by John Myczek

If all you need to do is force the input to upper case, try the CharacterCasingproperty of the textbox.

如果您需要做的只是强制输入为大写,请尝试使用文本框的CharacterCasing属性。

toUserTextBox.CharacterCasing = CharacterCasing.Upper;