2012-03-25 8 views
1

Ich habe einen einfachen Texteditor erstellt. Darin ist ein Kombinationsfeld zum Ändern der Schriftfamilie, des Stils und der Farbe verfügbar. Es ist mir gelungen, die Schriftarteigenschaften des ausgewählten Textes zu ändern. Mein CodeC# richttextbox Schrifteigenschaften

private void fontFamilySelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (notesCon[currentNotes].SelectedText != "") 
      { 
       notesCon[currentNotes].SelectionFont = new Font(fontFamily.SelectedItem.ToString(), notesCon[currentNotes].SelectionFont.Size, notesCon[currentNotes].SelectionFont.Style);     
      } 
     } 

     private void fontStyleSelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (notesCon[currentNotes].SelectedText != "") 
      { 
       switch (fontStyle.SelectedItem.ToString()) 
       { 
        case "Bold": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Bold); 
         break; 
        case "Italic": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Italic); 
         break; 
        case "Regular": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Regular); 
         break; 
        case "Strikeout": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Strikeout); 
         break; 
        case "Underline": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Underline); 
         break; 

       } 
      }    
     } 

     private void fontColorSelectedValueChanged(object sender, EventArgs e) 
     { 
      if (notesCon[currentNotes].SelectedText != "") 
      { 
       notesCon[currentNotes].SelectionFont = new Font(fontFamily.SelectedItem.ToString(), notesCon[currentNotes].SelectionFont.Size, notesCon[currentNotes].SelectionFont.Style); 
       notesCon[currentNotes].SelectionColor = fontColor.SelectedItem.Color; 
      } 
     } 

mein Dilemma:

Jedes Mal, wenn ich versuche, die aktuellen Schriftart Eigenschaften der Rich-Text-Box auf einen neuen zu setzen, ändert es erfolgreich, aber nicht behalten die Schrifteigenschaften des vorherigen Textes. Wie soll ich die Schrifteigenschaften ändern, ohne die vorherigen Schrifteigenschaften des Textes im Rich-Text-Feld zu ändern? Hilfe bitte, Vielen Dank im Voraus!

Antwort

0

das was Sie brauchen. Ich hatte auch dieses Problem. Zuerst müssen Sie alle Zeichen des ausgewählten Textes durchlaufen und dann die Schriftart anwenden.

private void ChangeFontStyleForSelectedText(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle) 
    { 
     _maskChanges = true; 
     try 
     { 
      int txtStartPosition = txtFunctionality.SelectionStart; 
      int selectionLength = txtFunctionality.SelectionLength; 
      if (selectionLength > 0) 
       using (RichTextBox txtTemp = new RichTextBox()) 
       { 
        txtTemp.Rtf = txtFunctionality.SelectedRtf; 
        for (int i = 0; i < selectionLength; ++i) 
        { 
         txtTemp.Select(i, 1); 
         txtTemp.SelectionFont = RenderFont(txtTemp.SelectionFont, familyName, emSize, fontStyle, enableFontStyle); 
        } 

        txtTemp.Select(0, selectionLength); 
        txtFunctionality.SelectedRtf = txtTemp.SelectedRtf; 
        txtFunctionality.Select(txtStartPosition, selectionLength); 
       } 
     } 
     finally 
     { 
      _maskChanges = false; 
     } 
    } 

und Rendern der Schrift:

/// <summary> 
    /// Changes a font from originalFont appending other properties 
    /// </summary> 
    /// <param name="originalFont">Original font of text 
    /// <param name="familyName">Target family name 
    /// <param name="emSize">Target text Size 
    /// <param name="fontStyle">Target font style 
    /// <param name="enableFontStyle">true when enable false when disable 
    /// <returns>A new font with all provided properties added/removed to original font</returns> 
    private Font RenderFont(Font originalFont, string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle) 
    { 
     if (fontStyle.HasValue && fontStyle != FontStyle.Regular && fontStyle != FontStyle.Bold && fontStyle != FontStyle.Italic && fontStyle != FontStyle.Underline) 
      throw new System.InvalidProgramException("Invalid style parameter to ChangeFontStyleForSelectedText"); 

     Font newFont; 
     FontStyle? newStyle = null; 
     if (fontStyle.HasValue) 
     { 
      if (fontStyle.HasValue && fontStyle == FontStyle.Regular) 
       newStyle = fontStyle.Value; 
      else if (originalFont != null && enableFontStyle.HasValue && enableFontStyle.Value) 
       newStyle = originalFont.Style | fontStyle.Value; 
      else 
       newStyle = originalFont.Style & ~fontStyle.Value; 
     } 

     newFont = new Font(!string.IsNullOrEmpty(familyName) ? familyName : originalFont.FontFamily.Name, 
          emSize.HasValue ? emSize.Value : originalFont.Size, 
          newStyle.HasValue ? newStyle.Value : originalFont.Style); 
     return newFont; 
    } 

Weitere Einzelheiten und Schritte, die Sie diesen Artikel lesen können: http://how-to-code-net.blogspot.ro/2014/01/how-to-make-custom-richtextbox-control.html