I am new in Visual Studio and using visual Studio 2008. In a project I want to make all text in uppercase while typed by the user without pressing shift key or caps lock on. I have used this code
我是Visual Studio中的新手并使用visual Studio 2008.在一个项目中,我希望用户键入所有文本,而不是按Shift键或大写锁定。我用过这段代码
TextBox1.Text = TextBox1.Text.ToUpper();
but it capitalize characters after pressing Enter key.
但按Enter键后,它会大写字符。
I just want that characters appear in uppercase while typing by the user without pressing shift key or without caps lock on.
我只想让用户在不按Shift键或没有大写锁定的情况下键入大写字母。
Total page code is as...
页面总代码为...
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox1.Text = TextBox1.Text.ToUpper();
}
}
Have any one any solution, please guide me.
有任何解决方案,请指导我。
4 个解决方案
#1
37
There is a specific property for this. It is called CharacterCasing and you could set it to Upper
这是一个特定的属性。它被称为CharacterCasing,您可以将其设置为Upper
TextBox1.CharacterCasing = CharacterCasing.Upper;
In ASP.NET you could try to add this to your textbox style
在ASP.NET中,您可以尝试将其添加到文本框样式中
style="text-transform:uppercase;"
You could find an example here: http://www.w3schools.com/cssref/pr_text_text-transform.asp
你可以在这里找到一个例子:http://www.w3schools.com/cssref/pr_text_text-transform.asp
#2
7
Edit (for ASP.NET)
After you edited your question it's cler you're using ASP.NET. Things are pretty different there (because in that case a roundtrip to server is pretty discouraged). You can do same things with JavaScript (but to handle globalization with toUpperCase()
may be a pain) or you can use CSS classes (relying on browsers implementation). Simply declare this CSS rule:
编辑完问题之后,你就是在使用ASP.NET。那里的情况非常不同(因为在这种情况下,非常不鼓励往返服务器)。您可以使用JavaScript执行相同的操作(但使用toUpperCase()来处理全球化可能会很麻烦)或者您可以使用CSS类(依赖于浏览器实现)。只需声明此CSS规则:
.upper-case
{
text-transform: uppercase
}
And add upper-case class to your text-box:
并在文本框中添加大写类:
<asp:TextBox ID="TextBox1" CssClass="upper-case" runat="server"/>
General (Old) Answer
but it capitalize characters after pressing Enter key.
但按Enter键后,它会大写字符。
It depends where you put that code. If you put it in, for example, TextChanged
event it'll make upper case as you type.
这取决于你放置代码的位置。如果你把它放入,例如,TextChanged事件,它会在你键入时大写。
You have a property that do exactly what you need: CharacterCasing
:
您有一个完全符合您需求的属性:CharacterCasing:
TextBox1.CharacterCasing = CharacterCasing.Upper;
It works more or less but it doesn't handle locales very well. For example in German language ß is SS when converted in upper case (Institut für Deutsche Sprache) and this property doesn't handle that.
它可以或多或少地工作,但它不能很好地处理语言环境。例如,在德语中,当用大写字母转换时,ß是SS(InstitutfürDeutscheSprache)并且此属性不能处理。
You may mimic CharacterCasing
property adding this code in KeyPress
event handler:
您可以模仿在KeyPress事件处理程序中添加此代码的CharacterCasing属性:
e.KeyChar = Char.ToUpper(e.KeyChar);
Unfortunately .NET framework doesn't handle this properly and upper case of sharp s character is returned unchanged. An upper case version of ß exists and it's ẞ and it may create some confusion, for example a word containing "ss" and another word containing "ß" can't be distinguished if you convert in upper case using "SS"). Don't forget that:
不幸的是,.NET框架没有正确处理这个问题,并且尖锐的s字符的大写字母返回不变。 ß的大写版本存在并且它是ẞ并且它可能产生一些混淆,例如包含“ss”的单词和包含“ß”的另一个单词如果使用“SS”以大写形式转换则无法区分。别忘了:
However, in 2010 the use of the capital sharp s became mandatory in official documentation when writing geographical names in all-caps.
但是,在2010年,在全部大写字母中使用地名时,使用大写字母成为官方文件中的强制性要求。
There isn't much you can do unless you add proper code for support this (and others) subtle bugs in .NET localization. Best advice I can give you is to use a custom dictionary per each culture you need to support.
除非你在.NET本地化中添加适当的代码来支持这个(以及其他)微妙的错误,否则你无能为力。我能给你的最佳建议是根据你需要支持的每种文化使用自定义词典。
Finally don't forget that this transformation may be confusing for your users: in Turkey, for example, there are two different versions of i upper case letter.
最后不要忘记,这种转变可能会让您的用户感到困惑:例如,在土耳其,有两种不同版本的i大写字母。
If text processing is important in your application you can solve many issues using specialized DLLs for each locale you support like Word Processors do.
如果文本处理在您的应用程序中很重要,您可以使用专门的DLL为您支持的每个语言环境(如Word处理器)解决许多问题。
What I usually do is to do not use standard .NET functions for strings when I have to deal with culture specific issues (I keep them only for text in invariant culture). I create a Unicode class with static methods for everything I need (character counting, conversions, comparison) and many specialized derived classes for each supported language. At run-time that static methods will user current thread culture name to pick proper implementation from a dictionary and to delegate work to that. A skeleton may be something like this:
我通常做的是当我不得不处理特定于文化的问题时,不要对字符串使用标准的.NET函数(我只保留它们用于不变文化中的文本)。我创建了一个带有静态方法的Unicode类,用于我需要的一切(字符计数,转换,比较)以及针对每种受支持语言的许多专用派生类。在运行时,静态方法将使用当前线程文化名称从字典中选择适当的实现并将工作委托给它。骨架可能是这样的:
abstract class Unicode
{
public static string ToUpper(string text)
{
return GetConcreteClass().ToUpperCore(text);
}
protected virtual string ToUpperCore(string text)
{
// Default implementation, overridden in derived classes if needed
return text.ToUpper();
}
private Dictionary<string, Unicode> _implementations;
private Unicode GetConcreteClass()
{
string cultureName = Thread.Current.CurrentCulture.Name;
// Check if concrete class has been loaded and put in dictionary
...
return _implementations[cultureName];
}
}
I'll then have an implementation specific for German language:
然后我会有一个特定于德语的实现:
sealed class German : Unicode
{
protected override string ToUpperCore(string text)
{
// Very naive implementation, just to provide an example
return text.ToUpper().Replace("ß", "ẞ");
}
}
True implementation may be pretty more complicate (not all OSes supports upper case ẞ) but take as a proof of concept. See also this post for other details about Unicode issues on .NET.
真正的实现可能会更复杂(并非所有操作系统都支持大写ẞ),而是作为概念证明。有关.NET上的Unicode问题的其他详细信息,请参阅此文章。
#3
0
if you can use LinqToObjects in your Project
如果您可以在项目中使用LinqToObjects
private YourTextBox_TextChanged ( object sender, EventArgs e)
{
return YourTextBox.Text.Where(c=> c.ToUpper());
}
An if you can't use LINQ (e.g. your project's target FW is .NET Framework 2.0) then
如果你不能使用LINQ(例如你的项目的目标FW是.NET Framework 2.0)那么
private YourTextBox_TextChanged ( object sender, EventArgs e)
{
YourTextBox.Text = YourTextBox.Text.ToUpper();
}
Why Text_Changed
Event ?
为什么选择Text_Changed事件?
There are few user input events in framework..
框架中的用户输入事件很少..
1-) OnKeyPressed
fires (starts to work) when user presses to a key from keyboard after the key pressed and released
1-)OnKeyPressed在按下并释放按键后用户从键盘按下按键时触发(开始工作)
2-) OnKeyDown
fires when user presses to a key from keyboard during key presses
2-)当用户在按键期间从键盘按下键时,OnKeyDown将触发
3-) OnKeyUp
fires when user presses to a key from keyboard and key start to release (user take up his finger from key)
3-)OnKeyUp在用户从键盘按下键并且键开始释放时触发(用户从键上取下手指)
As you see, All three are about keyboard event..So what about if the user copy and paste some data to the textbox?
如你所见,这三个都是关于键盘事件的。那么如果用户将一些数据复制并粘贴到文本框呢?
if you use one of these keyboard events then your code work when and only user uses keyboard..in example if user uses a screen keyboard with mouse click or copy paste the data your code which implemented in keyboard events never fires (never start to work)
如果您使用其中一个键盘事件,那么您的代码只在用户使用键盘时工作..例如,如果用户使用鼠标单击屏幕键盘或复制粘贴数据,您在键盘事件中实现的代码永远不会触发(永远不会开始工作) )
so, and Fortunately there is another option to work around : The Text Changed
event..
所以,幸运的是还有另一种解决方法:Text Changed事件..
Text Changed
event don't care where the data comes from..Even can be a copy-paste, a touchscreen tap (like phones or tablets), a virtual keyboard, a screen keyboard with mouse-clicks (some bank operations use this to much more security, or may be your user would be a disabled person who can't press to a standard keyboard) or a code-injection ;) ..
Text Changed事件并不关心数据的来源.Even可以是复制粘贴,触摸屏点击(如手机或平板电脑),虚拟键盘,鼠标点击屏幕键盘(某些银行操作使用此功能更安全,或者可能是你的用户将是一个无法按下标准键盘的残疾人)或代码注入;)..
No Matter !
不管 !
Text Changed
event just care about is there any changes with it's responsibility component area ( here, Your TextBox's Text
area) or not..
文本更改事件只是关心它的责任组件区域(此处,您的TextBox的文本区域)是否有任何变化..
If there is any change occurs, then your code which implemented under Text changed event works..
如果发生任何更改,那么在Text更改事件下实现的代码将起作用。
#4
-1
set your CssClass property in textbox1 to "cupper", then in page content create new css class :
将textbox1中的CssClass属性设置为“cupper”,然后在页面内容中创建新的css类:
<style type="text/css">.cupper {text-transform:uppercase;}</style>
Then, enjoy it ...
然后,享受它......
#1
37
There is a specific property for this. It is called CharacterCasing and you could set it to Upper
这是一个特定的属性。它被称为CharacterCasing,您可以将其设置为Upper
TextBox1.CharacterCasing = CharacterCasing.Upper;
In ASP.NET you could try to add this to your textbox style
在ASP.NET中,您可以尝试将其添加到文本框样式中
style="text-transform:uppercase;"
You could find an example here: http://www.w3schools.com/cssref/pr_text_text-transform.asp
你可以在这里找到一个例子:http://www.w3schools.com/cssref/pr_text_text-transform.asp
#2
7
Edit (for ASP.NET)
After you edited your question it's cler you're using ASP.NET. Things are pretty different there (because in that case a roundtrip to server is pretty discouraged). You can do same things with JavaScript (but to handle globalization with toUpperCase()
may be a pain) or you can use CSS classes (relying on browsers implementation). Simply declare this CSS rule:
编辑完问题之后,你就是在使用ASP.NET。那里的情况非常不同(因为在这种情况下,非常不鼓励往返服务器)。您可以使用JavaScript执行相同的操作(但使用toUpperCase()来处理全球化可能会很麻烦)或者您可以使用CSS类(依赖于浏览器实现)。只需声明此CSS规则:
.upper-case
{
text-transform: uppercase
}
And add upper-case class to your text-box:
并在文本框中添加大写类:
<asp:TextBox ID="TextBox1" CssClass="upper-case" runat="server"/>
General (Old) Answer
but it capitalize characters after pressing Enter key.
但按Enter键后,它会大写字符。
It depends where you put that code. If you put it in, for example, TextChanged
event it'll make upper case as you type.
这取决于你放置代码的位置。如果你把它放入,例如,TextChanged事件,它会在你键入时大写。
You have a property that do exactly what you need: CharacterCasing
:
您有一个完全符合您需求的属性:CharacterCasing:
TextBox1.CharacterCasing = CharacterCasing.Upper;
It works more or less but it doesn't handle locales very well. For example in German language ß is SS when converted in upper case (Institut für Deutsche Sprache) and this property doesn't handle that.
它可以或多或少地工作,但它不能很好地处理语言环境。例如,在德语中,当用大写字母转换时,ß是SS(InstitutfürDeutscheSprache)并且此属性不能处理。
You may mimic CharacterCasing
property adding this code in KeyPress
event handler:
您可以模仿在KeyPress事件处理程序中添加此代码的CharacterCasing属性:
e.KeyChar = Char.ToUpper(e.KeyChar);
Unfortunately .NET framework doesn't handle this properly and upper case of sharp s character is returned unchanged. An upper case version of ß exists and it's ẞ and it may create some confusion, for example a word containing "ss" and another word containing "ß" can't be distinguished if you convert in upper case using "SS"). Don't forget that:
不幸的是,.NET框架没有正确处理这个问题,并且尖锐的s字符的大写字母返回不变。 ß的大写版本存在并且它是ẞ并且它可能产生一些混淆,例如包含“ss”的单词和包含“ß”的另一个单词如果使用“SS”以大写形式转换则无法区分。别忘了:
However, in 2010 the use of the capital sharp s became mandatory in official documentation when writing geographical names in all-caps.
但是,在2010年,在全部大写字母中使用地名时,使用大写字母成为官方文件中的强制性要求。
There isn't much you can do unless you add proper code for support this (and others) subtle bugs in .NET localization. Best advice I can give you is to use a custom dictionary per each culture you need to support.
除非你在.NET本地化中添加适当的代码来支持这个(以及其他)微妙的错误,否则你无能为力。我能给你的最佳建议是根据你需要支持的每种文化使用自定义词典。
Finally don't forget that this transformation may be confusing for your users: in Turkey, for example, there are two different versions of i upper case letter.
最后不要忘记,这种转变可能会让您的用户感到困惑:例如,在土耳其,有两种不同版本的i大写字母。
If text processing is important in your application you can solve many issues using specialized DLLs for each locale you support like Word Processors do.
如果文本处理在您的应用程序中很重要,您可以使用专门的DLL为您支持的每个语言环境(如Word处理器)解决许多问题。
What I usually do is to do not use standard .NET functions for strings when I have to deal with culture specific issues (I keep them only for text in invariant culture). I create a Unicode class with static methods for everything I need (character counting, conversions, comparison) and many specialized derived classes for each supported language. At run-time that static methods will user current thread culture name to pick proper implementation from a dictionary and to delegate work to that. A skeleton may be something like this:
我通常做的是当我不得不处理特定于文化的问题时,不要对字符串使用标准的.NET函数(我只保留它们用于不变文化中的文本)。我创建了一个带有静态方法的Unicode类,用于我需要的一切(字符计数,转换,比较)以及针对每种受支持语言的许多专用派生类。在运行时,静态方法将使用当前线程文化名称从字典中选择适当的实现并将工作委托给它。骨架可能是这样的:
abstract class Unicode
{
public static string ToUpper(string text)
{
return GetConcreteClass().ToUpperCore(text);
}
protected virtual string ToUpperCore(string text)
{
// Default implementation, overridden in derived classes if needed
return text.ToUpper();
}
private Dictionary<string, Unicode> _implementations;
private Unicode GetConcreteClass()
{
string cultureName = Thread.Current.CurrentCulture.Name;
// Check if concrete class has been loaded and put in dictionary
...
return _implementations[cultureName];
}
}
I'll then have an implementation specific for German language:
然后我会有一个特定于德语的实现:
sealed class German : Unicode
{
protected override string ToUpperCore(string text)
{
// Very naive implementation, just to provide an example
return text.ToUpper().Replace("ß", "ẞ");
}
}
True implementation may be pretty more complicate (not all OSes supports upper case ẞ) but take as a proof of concept. See also this post for other details about Unicode issues on .NET.
真正的实现可能会更复杂(并非所有操作系统都支持大写ẞ),而是作为概念证明。有关.NET上的Unicode问题的其他详细信息,请参阅此文章。
#3
0
if you can use LinqToObjects in your Project
如果您可以在项目中使用LinqToObjects
private YourTextBox_TextChanged ( object sender, EventArgs e)
{
return YourTextBox.Text.Where(c=> c.ToUpper());
}
An if you can't use LINQ (e.g. your project's target FW is .NET Framework 2.0) then
如果你不能使用LINQ(例如你的项目的目标FW是.NET Framework 2.0)那么
private YourTextBox_TextChanged ( object sender, EventArgs e)
{
YourTextBox.Text = YourTextBox.Text.ToUpper();
}
Why Text_Changed
Event ?
为什么选择Text_Changed事件?
There are few user input events in framework..
框架中的用户输入事件很少..
1-) OnKeyPressed
fires (starts to work) when user presses to a key from keyboard after the key pressed and released
1-)OnKeyPressed在按下并释放按键后用户从键盘按下按键时触发(开始工作)
2-) OnKeyDown
fires when user presses to a key from keyboard during key presses
2-)当用户在按键期间从键盘按下键时,OnKeyDown将触发
3-) OnKeyUp
fires when user presses to a key from keyboard and key start to release (user take up his finger from key)
3-)OnKeyUp在用户从键盘按下键并且键开始释放时触发(用户从键上取下手指)
As you see, All three are about keyboard event..So what about if the user copy and paste some data to the textbox?
如你所见,这三个都是关于键盘事件的。那么如果用户将一些数据复制并粘贴到文本框呢?
if you use one of these keyboard events then your code work when and only user uses keyboard..in example if user uses a screen keyboard with mouse click or copy paste the data your code which implemented in keyboard events never fires (never start to work)
如果您使用其中一个键盘事件,那么您的代码只在用户使用键盘时工作..例如,如果用户使用鼠标单击屏幕键盘或复制粘贴数据,您在键盘事件中实现的代码永远不会触发(永远不会开始工作) )
so, and Fortunately there is another option to work around : The Text Changed
event..
所以,幸运的是还有另一种解决方法:Text Changed事件..
Text Changed
event don't care where the data comes from..Even can be a copy-paste, a touchscreen tap (like phones or tablets), a virtual keyboard, a screen keyboard with mouse-clicks (some bank operations use this to much more security, or may be your user would be a disabled person who can't press to a standard keyboard) or a code-injection ;) ..
Text Changed事件并不关心数据的来源.Even可以是复制粘贴,触摸屏点击(如手机或平板电脑),虚拟键盘,鼠标点击屏幕键盘(某些银行操作使用此功能更安全,或者可能是你的用户将是一个无法按下标准键盘的残疾人)或代码注入;)..
No Matter !
不管 !
Text Changed
event just care about is there any changes with it's responsibility component area ( here, Your TextBox's Text
area) or not..
文本更改事件只是关心它的责任组件区域(此处,您的TextBox的文本区域)是否有任何变化..
If there is any change occurs, then your code which implemented under Text changed event works..
如果发生任何更改,那么在Text更改事件下实现的代码将起作用。
#4
-1
set your CssClass property in textbox1 to "cupper", then in page content create new css class :
将textbox1中的CssClass属性设置为“cupper”,然后在页面内容中创建新的css类:
<style type="text/css">.cupper {text-transform:uppercase;}</style>
Then, enjoy it ...
然后,享受它......