question

ChristKingKennedy-5906 avatar image
0 Votes"
ChristKingKennedy-5906 asked Viorel-1 edited

increase/decrease font size of user text in richtextbox

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'> " ?

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

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 );


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ChristKingKennedy-5906 avatar image
0 Votes"
ChristKingKennedy-5906 answered Viorel-1 edited

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?





· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


You are missing this statement: using System.Text.RegularExpressions;.
'

0 Votes 0 ·