Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Text to Uppercase while typing in Text box

Tags:

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

TextBox1.Text = TextBox1.Text.ToUpper(); 

but it capitalize characters after pressing Enter key.

I just want that characters appear in uppercase while typing by the user without pressing shift key or without caps lock on.

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.

like image 969
MUKESH BAFNA Avatar asked May 03 '14 12:05

MUKESH BAFNA


People also ask

How do you automatically set text box to uppercase?

Use keyup() method to trigger the keyup event when user releases the key from keyboard. Use toLocaleUpperCase() method to transform the input to uppercase and again set it to the input element.

How do you change the uppercase of a text?

To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.

How do you change to uppercase in HTML?

The toUpperCase() method converts a string to uppercase letters. The toUpperCase() method does not change the original string.

When you enter text into a input field a function is triggered which transforms the input text to upper case?

When you leave the input field, a function is triggered which transforms the input text to upper case.


2 Answers

There is a specific property for this. It is called CharacterCasing and you could set it to Upper

  TextBox1.CharacterCasing = CharacterCasing.Upper; 

In ASP.NET you could try to add this to your textbox style

  style="text-transform:uppercase;" 

You could find an example here: http://www.w3schools.com/cssref/pr_text_text-transform.asp

like image 198
Steve Avatar answered Oct 25 '22 08:10

Steve


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:

.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.

It depends where you put that code. If you put it in, for example, TextChanged event it'll make upper case as you type.

You have a property that do exactly what you need: 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.

You may mimic CharacterCasing property adding this code in KeyPress event handler:

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:

However, in 2010 the use of the capital sharp s became mandatory in official documentation when writing geographical names in all-caps.

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.

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.

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.

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:

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.

like image 43
Adriano Repetti Avatar answered Oct 25 '22 09:10

Adriano Repetti