Convert Text to Uppercase while typing in Text box

前端 未结 7 1750
我寻月下人不归
我寻月下人不归 2020-12-14 18:04

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.

相关标签:
7条回答
  • 2020-12-14 18:08

    I use to do this thing (Code Behind) ASP.NET using VB.NET:

    1) Turn AutoPostBack = True in properties of said Textbox

    2) Code for Textbox (Event : TextChanged) Me.TextBox1.Text = Me.TextBox1.Text.ToUpper

    3) Observation After entering the string variables in TextBox1, when user leaves TextBox1, AutoPostBack fires the code when Text was changed during "TextChanged" event.

    0 讨论(0)
  • 2020-12-14 18:14

    set your CssClass property in textbox1 to "cupper", then in page content create new css class :

    <style type="text/css">.cupper {text-transform:uppercase;}</style>
    

    Then, enjoy it ...

    0 讨论(0)
  • 2020-12-14 18:15

    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

    0 讨论(0)
  • 2020-12-14 18:18
    **/*Css Class*/**
    .upCase
    {
        text-transform: uppercase;
    }
    
    <asp:TextBox ID="TextBox1" runat="server" Text="abc" Cssclass="upCase"></asp:TextBox>
    
    0 讨论(0)
  • 2020-12-14 18:18

    I had the same problem with Visual Studio 2008 and solved adding the following event handler to the textbox:

            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 'a') && (e.KeyChar <= 'z'))
            {
                int iPos = textBox1.SelectionStart;
                int iLen = textBox1.SelectionLength;
                textBox1.Text = textBox1.Text.Remove(iPos, iLen).Insert(iPos, Char.ToUpper(e.KeyChar).ToString());
                textBox1.SelectionStart = iPos + 1;
                e.Handled = true;
            }
        }
    

    It works even if you type a lowercase character in a textbox where some characters are selected. I don't know if the code works with a Multiline textbox.

    0 讨论(0)
  • 2020-12-14 18:20

    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.

    0 讨论(0)
提交回复
热议问题