Richtextbox - Farbe, fett, kursiv, unterstrichen

  • VB.NET

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von Ninoxo.

    Richtextbox - Farbe, fett, kursiv, unterstrichen

    Ich arbeite mit einer Richtextbox. Standardmäßig kann man ja dort die Farbe allgemein einstellen.
    Ich möchte jetzt aber nur dass z.B. die nächste Zeile grün wird, nur bestimmte Wörter oder dass z.B. alles unterstrichen ist, was zwischen '> <' steht.
    Ich habe schon viel gegoogelt aber es hat alles nichts geholfen, da sich bei mir immer die ganze RTB verfärbt hat und nicht nur die nächste Zeile.

    mfg
    Ich würde den Text in der RichTextBox Char für Char durchgehen und dann prüfen, ob es sich beim aktuellen Char um ein > oder ein < handelt.

    Meine Lösung:

    VB.NET-Quellcode

    1. Private Sub UnderLine(ByVal rtb As RichTextBox)
    2. Dim text As String = rtb.Text
    3. rtb.Text = String.Empty
    4. For i As Integer = 0 To text.Length - 1
    5. If Not text(i) = ">"c AndAlso Not text(i) = "<"c Then
    6. rtb.AppendText(text(i).ToString())
    7. Else
    8. If text(i) = ">"c Then
    9. rtb.AppendText(">")
    10. rtb.SelectionFont = New Font(rtb.Font, FontStyle.Underline)
    11. ElseIf text(i) = "<"c Then
    12. rtb.SelectionFont = New Font(rtb.Font, FontStyle.Regular)
    13. rtb.AppendText("<")
    14. End If
    15. End If
    16. Next
    17. End Sub


    MfG
    War doch alles schon mal da:

    VB.NET-Quellcode

    1. With RichTextBox1
    2. .Text = "0123456789"
    3. .SelectionStart = 4 ' 1
    4. .SelectionLength = 3
    5. .SelectionFont = New Font(.SelectionFont, .SelectionFont.Style Or FontStyle.Bold)
    6. .SelectionStart = 5 ' 2
    7. .SelectionLength = 3
    8. .SelectionFont = New Font(.SelectionFont, .SelectionFont.Style Or FontStyle.Italic)
    9. .SelectionStart = 6 ' 3
    10. .SelectionLength = 3
    11. .SelectionFont = New Font(.SelectionFont, .SelectionFont.Style Or FontStyle.Underline)
    12. End With

    Manchmal hilft auch Probieren oder in der Hilfe nachschlagen.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Ich würde sagen wenn du z.B. nur ein Wort färben willst, hilft das hier:

    VB.NET-Quellcode

    1. RichTextBox1.SelectionColor = ColorPickerDropDown1.SelectedColor


    Einfach einen Colorpicker einfügen und alles läuft :)
    "Life isn't about winning the race. Life is about finishing the race and how many people we can help finish the race." ~Marc Mero

    Nun bin ich also auch soweit: Keine VB-Fragen per PM! Es gibt hier ein Forum, verdammt!