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