Articles in this section
Category / Section

How to change casing of line items in EditControl?

5 mins read

EditControl provides option for an end user to customize the Character casing of its contents. Please follow below steps for achieving this.

 

Step 1: At present there is no property in EditControl, to define Character casing for its contents. So here we are creating Custom control derived from the EditControl and implementing a property to define the Character casing.

Step 2: In EditControl, Lines property will hold its contents i.e. line by line. During the drawing process, each line will be retrieved from this collection and drawn in the EditControl UI.

Step 3: Here, we are iterating through each line and applying the Character casing to it. Then it will be reflected in the EditControl with Character case settings.

The following code demonstrates the same.

 

Code Example: [Xaml]

 

<syncfusion:MenuAdv Grid.Row="0" syncfusion:SkinStorage.VisualStyle="Blend">
    <syncfusion:MenuItemAdv Header="Lower case" Click="MenuItemAdv_Click"/>
    <syncfusion:MenuItemAdv Header="Upper case" Click="MenuItemAdv_Click_1"/>
    <syncfusion:MenuItemAdv Header="Default" Click="MenuItemAdv_Click_2"/>
</syncfusion:MenuAdv>
<syncfusion:EditControl x:Name="editcontrol" Grid.Row="1" Focusable="True" KeyUp="Editcontrol_KeyUp" TextChanged="Editcontrol_TextChanged"/>

 

Code Example: [C#]

 

//Codes in MainWindow.xaml.cs 

private void Editcontrol_TextChanged(object sender, EventArgs e)
{ 
    if (EditControlExt.GetCharacterCasing(editcontrol) == EditControlExt.CharacterCasing.Upper)
    {
        SetUpperCaseText();
    }
    else if (EditControlExt.GetCharacterCasing(editcontrol)==EditControlExt.CharacterCasing.Lower)
    {
        SetLowerCaseText();
    }
}
 
private void Editcontrol_KeyUp(object sender, KeyEventArgs e)
{
    var casing = EditControlExt.GetCharacterCasing(editcontrol);
    if (casing == EditControlExt.CharacterCasing.Upper)
    {
        SetUpperCaseText();
    }
    else if (casing == EditControlExt.CharacterCasing.Lower)
    {
        SetLowerCaseText();
    }
}

private void SetUpperCaseText()
{
    for (int i = 0; i < editcontrol.Lines.Count; i++)
    {
        editcontrol.Lines[i].Text = editcontrol.Lines[i].Text.ToUpper();
    }
}

private void SetLowerCaseText()
{
    for (int i = 0; i < editcontrol.Lines.Count; i++)
    {
        editcontrol.Lines[i].Text = editcontrol.Lines[i].Text.ToLower();
    }
}

//Set Lower case 
private void MenuItemAdv_Click(object sender, RoutedEventArgs e)
{
    EditControlExt.SetCharacterCasing(editcontrol, EditControlExt.CharacterCasing.Lower);
}

//Set Upper case 
private void MenuItemAdv_Click_1(object sender, RoutedEventArgs e)
{
    EditControlExt.SetCharacterCasing(editcontrol, EditControlExt.CharacterCasing.Upper);
}
 
//Set Default/Normal case
private void MenuItemAdv_Click_2(object sender, RoutedEventArgs e)
{
    EditControlExt.SetCharacterCasing(editcontrol, EditControlExt.CharacterCasing.Normal);
}

//Codes in EditControlExt.cs 
public class EditControlExt
{

    //CharacterCasing property
    public enum CharacterCasing
    {
        Upper,
        Lower,
        Normal
    }

    public static CharacterCasing GetCharacterCasing(DependencyObject obj)
    {
        return (CharacterCasing)obj.GetValue(CharacterCasingProperty);
    }

    public static void SetCharacterCasing(DependencyObject obj, CharacterCasing value)
    {
        obj.SetValue(CharacterCasingProperty, value);
    }

    public static readonly DependencyProperty CharacterCasingProperty = DependencyProperty.RegisterAttached("CharacterCasing", typeof(CharacterCasing),    typeof(EditControlExt), new PropertyMetadata(CharacterCasing.Normal));

}

 

Screenshot

 

wpf edit control character casing is lower

Figure:  CharacterCasing is lower.

 

 

wpf edit control character casing is upper

Figure:  CharacterCasing is upper.

 

 

wpf edit control character casing is normal

Figure:  CharacterCasing is Normal.

 

Sample:  EditControlCharacterCasingSample

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied