Como mantenho a formatação RichText (negrito / itálico / etc) ao alterar qualquer element
Tenho uma caixa de rich text que pode conter uma sequência que contenha elementos em negrito, itálico ou até mesmo fontes e tamanhos diferentes. Se eu selecionar a sequência inteira, incluindo todas as diferenças, como posso "Negrito" essa sequência sem converter a sequência inteira na fonte genérica com apenas um atributo "negrito"?
Por exemplo: quero ativar "Esteé algu text
" para dentro "Esteé algu text
"
Observe que "é um pouco" permaneceu em itálico e "texto" permaneceu uma fonte diferent
O que eu tenho atualmente é bastante simplista:
private void tsBold_Click(object sender, EventArgs e)
{
if (rtb.SelectionFont == null) return;
Font f;
if (tsBold.Checked)
f = new Font(rtb.SelectionFont, FontStyle.Bold);
else
f = new Font(rtb.SelectionFont, FontStyle.Regular);
rtb.SelectionFont = f;
rtb.Focus();
}
Claro, isso vai aplicar exatamente a mesma fonte para toda a seleção. Existe alguma maneira de acrescentar "negrito" às fontes existentes?
RESPOND Embora a resposta "oficial" abaixo seja apenas a ponta do iceberg, foi o empurrão que eu precisava na direção certa. Obrigado pela dica
Aqui está a minha correção oficial:
Adicionei isso ao meu objeto RichTextBox:
/// <summary>
/// Change the richtextbox style for the current selection
/// </summary>
public void ChangeFontStyle(FontStyle style, bool add)
{
//This method should handle cases that occur when multiple fonts/styles are selected
// Parameters:-
// style - eg FontStyle.Bold
// add - IF true then add else remove
// throw error if style isn't: bold, italic, strikeout or underline
if (style != FontStyle.Bold
&& style != FontStyle.Italic
&& style != FontStyle.Strikeout
&& style != FontStyle.Underline)
throw new System.InvalidProgramException("Invalid style parameter to ChangeFontStyle");
int rtb1start = this.SelectionStart;
int len = this.SelectionLength;
int rtbTempStart = 0;
//if len <= 1 and there is a selection font then just handle and return
if (len <= 1 && this.SelectionFont != null)
{
//add or remove style
if (add)
this.SelectionFont = new Font(this.SelectionFont, this.SelectionFont.Style | style);
else
this.SelectionFont = new Font(this.SelectionFont, this.SelectionFont.Style & ~style);
return;
}
using (EnhancedRichTextBox rtbTemp = new EnhancedRichTextBox())
{
// Step through the selected text one char at a time
rtbTemp.Rtf = this.SelectedRtf;
for (int i = 0; i < len; ++i)
{
rtbTemp.Select(rtbTempStart + i, 1);
//add or remove style
if (add)
rtbTemp.SelectionFont = new Font(rtbTemp.SelectionFont, rtbTemp.SelectionFont.Style | style);
else
rtbTemp.SelectionFont = new Font(rtbTemp.SelectionFont, rtbTemp.SelectionFont.Style & ~style);
}
// Replace & reselect
rtbTemp.Select(rtbTempStart, len);
this.SelectedRtf = rtbTemp.SelectedRtf;
this.Select(rtb1start, len);
}
return;
}
Alterei os métodos de clique para usar o seguinte padrão:
private void tsBold_Click(object sender, EventArgs e)
{
enhancedRichTextBox1.ChangeFontStyle(FontStyle.Bold, tsBold.Checked);
enhancedRichTextBox1.Focus();
}