简体   繁体   中英

how To Type Uppercase String in windows phone TextBox Wpf c#

how To Type Uppercase String in windows phone TextBox Wpf c#.

I tried it on KeyDown Event.

   void TxtPanno_KeyDown(object sender, KeyEventArgs e)  
   {

   TxtPanno.Text = TxtPanno.Text.ToUpper();  //1 code.

   TxtPanno.Text= CultureInfo.CurrentCulture.TextInfo.ToUpper(TxtPanno.Text); //2 code     

   TxtPanno.Text=Regex.Replace(TxtPanno.Text, "^[A-Z]", m => m.Value.ToUpper());  //3 code. 
   }

But Problems is that.

Cursor always go to leftside.

Try doing:

TxtPanno.Text = TxtPanno.Text.ToUpper(); 
TxtPanno.SelectionStart = TxtPanno.Text.Length;
TxtPanno.SelectionLength = 0;

It makes the text uppercase, moves the cursor to the end and selects nothing.

You should not try to replace text,instead you should apply styles like this

<Style TargetType="{x:Type TextBox}">
    <Setter Property="CharacterCasing" Value="Upper"/>
</Style>

The above style will make all textboxes uppercase, you can change it to only apply to a specific textbook like this

<TextBox CharacterCasing="Upper" />

then when using the text entered, make it upper case.

Finally I tried this code and it worked.

void TxtPanno_TextChanged(object sender, TextChangedEventArgs e)
{

  TxtPanno.Text = TxtPanno.Text.ToUpper();
  TxtPanno.SelectionStart = TxtPanno.Text.Length;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM