Introduction
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
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
- public UC_UpperCase_TextBox()
- {
- this.TextChanged += new TextChangedEventHandler(UC_UpperCase_TextBox_TextChanged);
- }
Step 4
Inside TextChanged event put code for convert text in Upper Case
- void UC_UpperCase_TextBox_TextChanged(object sender, TextChangedEventArgs e)
- {
- var tb = (TextBox) sender;
- var selectionStart = tb.SelectionStart;
- var selectionLength = tb.SelectionLength;
- tb.Text = tb.Text.ToUpper();
- tb.SelectionStart = selectionStart;
- tb.SelectionLength = selectionLength;
- }
Your code will Look Like :
Figure 2: Code
Step 5
You can use it as a control in .xaml like:
- <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.