Tengo un cuadro de texto enriquecido que puede contener una cadena que tiene elementos en negrita, cursiva o incluso fuentes y tamaños diferentes. Si selecciono la cadena completa, incluidas todas las diferencias, ¿cómo puedo "Negrita" esa cadena sin convertir toda la cadena a la fuente genérica con solo un atributo "negrita"?¿Cómo mantengo el formato de RichText (negrita/cursiva/etc.) al cambiar cualquier elemento?
Por ejemplo: Quiero convertir "Este es un poco detext
" en "Este es un poco detext
"
Tenga en cuenta que "es un poco de" permaneció en cursiva y "texto" seguía siendo una fuente diferente .
Lo que tengo actualmente es bastante simple:
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();
}
Por supuesto, esto va a aplicar exactamente el mismo tipo de letra a toda la selección. ¿Hay alguna forma de agregar "negrita" a la (s) fuente (s) existente (s)?
RESPUESTA Mientras que el "oficial" respuesta a continuación es sólo la punta del iceberg, que fue el empujón que necesitaba en la dirección correcta. Gracias por el consejo.
aquí está mi solución oficial:
he añadido esto a mi RichTextBox objeto:
/// <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;
}
entonces he cambiado los métodos Haga clic para utilizar el siguiente patrón:
private void tsBold_Click(object sender, EventArgs e)
{
enhancedRichTextBox1.ChangeFontStyle(FontStyle.Bold, tsBold.Checked);
enhancedRichTextBox1.Focus();
}
Por qué es tan duro en Windows Forms? Es un ['EM_SETCHARFORMAT'] (http://msdn.microsoft.com/en-us/library/bb774230 (v = VS.85) .aspx trivial) en el nivel de Win32. Como tiene el asa de la ventana a mano, ¿no puede hacerlo de esa manera? –