increase/decrease font size of user text in richtextbox

Christ Kennedy 196 Reputation points
2021-05-27T09:38:31.79+00:00

I'm working on a word-processor and my font-increase/font-decrease buttons change all the font styles when they resize the fonts.

this code is my original code (the want that destroys all Font-Style formatting differences)
public void mnuFontIncrease_Click(object sender, EventArgs e)
{
if (rtx.SelectionStart >= 0)
{
Font fntPrevious = rtx.SelectionFont;
rtx.SelectionFont = FontSize_Change(fntPrevious, 1);
}
}

Font FontSize_Change(Font fntStart, int intDelta)
{
    Font fntRetVal = new Font(fntStart.FontFamily.Name, 
                                               fntStart.Size + intDelta, 
                                               fntStart.Style);
    return fntRetVal;
}

it works but the user loses all Font-Style differences in the selected text.
so I tried testing each character one at a time to find the start/end points of contiguous identical font and only change those.
its working ... but is excruciatingly slow.
here's the code
public void mnuFontDecrease_Click(object sender, EventArgs e)
{

        int intSelection_Start = rtx.SelectionStart;
        int intSelection_Length = rtx.SelectionLength;

        int intStart = intSelection_Length > 0
                                        ? intSelection_Start
                                        : 0;
        if (intStart < 0)
            intStart = 0;
        else if (intStart > rtx.Text.Length -1)
            intStart = rtx.Text.Length - 1;

        int intEnd = intSelection_Length > 0
                                        ? intSelection_Start + intSelection_Length
                                        : rtx.Text.Length;
        if (intEnd < intStart)
            intEnd = intStart;
        else if (intEnd > rtx.Text.Length - 1)
        {
            intEnd = rtx.Text.Length - 1;
            if (intEnd < intStart)
                intEnd = intStart;
        }

        int intIndex = intStart;

        while (intIndex <= intEnd)
        {
            rtx.SelectionStart = intIndex;
            rtx.SelectionLength = 1;
            Font fntCurrent = rtx.SelectionFont;
            Font fntTest = fntCurrent;
            while (fntCurrent.Equals(fntTest) && intIndex <= intEnd)
            {
                intIndex++;
                rtx.SelectionStart = intIndex;
                fntTest = rtx.SelectionFont;
            }

            int intLength = intIndex - intStart - 1;
            if (rtx.SelectionStart + intLength > rtx.Text.Length)
                intLength = rtx.Text.Length - rtx.SelectionStart;
            rtx.SelectionLength = intLength;
            rtx.SelectionStart = intStart;
            Font fntNew = FontSize_Change(fntCurrent, -1);
            rtx.SelectionFont = fntNew;

            intStart = intIndex;
        }

        rtx.SelectionStart = intSelection_Start;
        rtx.SelectionLength = intSelection_Length;
    }


Font FontSize_Change(Font fntStart, int intDelta)
{
    Font fntRetVal = new Font(fntStart.FontFamily.Name, 
                                               fntStart.Size + intDelta, 
                                               fntStart.Style);
    return fntRetVal;
}

is there a way to ask the RichTextBox "what Character Index sees the next change in font <starting from Character Index 'n'> " ?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,616 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 114.5K Reputation points
    2021-05-27T17:44:28.347+00:00

    Try this approach:

    const decimal delta = 4;
    
    richTextBox1.Focus( );
    
    var ss = richTextBox1.SelectionStart;
    var sl = richTextBox1.SelectionLength;
    
    richTextBox1.SelectedRtf =
     Regex.Replace( richTextBox1.SelectedRtf, @"(?<!\\+)\\fs(?<s>\d+([.]\d+)?)",
     m => $@"\fs{decimal.Parse( m.Groups["s"].Value ) + delta}" );
    
    richTextBox1.Select( ss, sl );
    
    0 comments No comments

  2. Christ Kennedy 196 Reputation points
    2021-05-27T19:52:29.497+00:00

    thank you for your response,
    I tried out your code... after modifying it to fit my project I came up with this

    public void mnuFontIncrease_Click(object sender, EventArgs e)
    {
        // /*
        const decimal delta = 4;
    
        rtx.Focus();
    
        var ss = rtx.SelectionStart;
        var sl = rtx.SelectionLength;
    
        rtx.SelectedRtf =
            Regex.Replace(rtx.SelectedRtf, 
                        @"(?<!\\+)\\fs(?<s>\d+([.]\d+)?)",
                        m => $@"\fs{decimal.Parse(m.Groups["s"].Value) + delta}");
    
        rtx.Select(ss, sl);
    
        /*/
        if (rtx.SelectionStart >= 0)
        {
            Font fntPrevious = rtx.SelectionFont;
            rtx.SelectionFont = FontSize_Change(fntPrevious, 1);
        }
        // */
    }
    

    and it gives me the error

    Severity Code Description Project File Line Suppression State
    Error CS0103 The name 'Regex' does not exist in the current context Words C:\Users\Christ\Documents\C#\Projects - Writing\Words\ck_RichTextBox.cs 696 Active

    am I missing an #include<> statement?