Upper Case TextBox


In this Blog you will see how to create user control for upper case textbox

Step 1

Add class1.cs to your current project as UC_UpperCase_TextBox

Add Class

Figure 1:Add Class

Step 2

Inherit this class with textbox like

public class UC_UpperCase_TextBox : TextBox

Step 3

Create TextChanged Event for this control

  1. public UC_UpperCase_TextBox()  
  2. {  
  3.       this.TextChanged += new TextChangedEventHandler(UC_UpperCase_TextBox_TextChanged);  
  4. }  
Step 4

Inside TextChanged event put code for convert text in Upper Case
  1. void UC_UpperCase_TextBox_TextChanged(object sender, TextChangedEventArgs e)   
  2. {  
  3.     var tb = (TextBox) sender;  
  4.     var selectionStart = tb.SelectionStart;  
  5.     var selectionLength = tb.SelectionLength;  
  6.     tb.Text = tb.Text.ToUpper();  
  7.     tb.SelectionStart = selectionStart;  
  8.     tb.SelectionLength = selectionLength;  
  9. }  
Your code will Look Like :


Figure 2:

Step 5

You can use it as a control in .xaml like:
  1. <my2:UC_UpperCase_TextBox Grid.Row="1" Grid.Column="1" Margin="2,3" Height="23" Text="{Binding TITLE,Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Center" x:Name="txt_Title" MaxLength="1024" />  
It can be use in WPF and Silverlight Application to make it Upper case Letter in text box.