Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am working on a computer application. I want to provide user to select upper case from combobox and when he type, the font in rich text box convert into uppercase. The final typed document will consist mixture of few lines in uppercase and few lines in lower case.

I am new to wpf, c#. In text box there is option for character casing but could not able to get a solution for richtextbox wpf.

What I have tried:

C#
I have tried with in textbox. There is charactercasing. But no such thing in wpf rich text box. 
Posted
Updated 8-Nov-16 9:32am
Comments
[no name] 8-Nov-16 6:54am    
ToUpper()

1 solution

It is not easy but by registering the following TextChanged event handler I got the code below to convert every input to uppercase. If you add code to check your combobox selection you might get it to work.

C#
private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    var textBox = (RichTextBox)sender;

    var docStart = textBox.Document.ContentStart.DocumentStart;
    foreach ( var change in e.Changes )
    {
        if( change.AddedLength > 0 )
        {
            var changeStart = docStart.GetPositionAtOffset(change.Offset, LogicalDirection.Forward);
            var changeEnd =   docStart.GetPositionAtOffset(change.Offset+change.AddedLength, LogicalDirection.Forward);
            var changedRange = new TextRange(changeStart, changeEnd);
            var currentText = changedRange.Text;
            var upperText = currentText.ToUpper();
            if( upperText != currentText )
            {
                changedRange.Text = upperText;
                Debug.Print("Replaced " + currentText);
            }
            textBox.Selection.Select(changeEnd, changeEnd);
        }
    }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900