Textfarbe ändern aus Array in RichTextBox

  • VB.NET
  • .NET (FX) 4.0

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von x3.vb.noob.

    Textfarbe ändern aus Array in RichTextBox

    Hallo zusammen

    Fange gerade mit VB an, hab mir auch ein Buch zugelegt.

    Bin gerade am üben, und komme nicht weiter,
    ich habe eine RichTextBox, mit folgendem Text

    VB.NET-Quellcode

    1. RichTextBox1.Text = "xyz Hallo Welt Programm"


    Jetzt möchte ich die Textfarbe ändern und nur von "Hallo" & "Welt".
    Die Wörter die in einer Array List sind.

    VB.NET-Quellcode

    1. Dim array0 As New List(Of String)({"Hallo", "Welt"})


    bsp.>> xyz Hallo Welt Programm

    Bin für jede Hilfe dankbar.
    1. Farbe Rot ist der Moderation vorbehalten.
    2. google.de/webhp?sourceid=chrom…xtbox+mark+specific+words - Schau mal da durch, da findet sich bestimmt was. ;)
    3. SelectionStart, SelectionLength und SelectionColor sollten auch wichtig sein.
    4.

    x3.vb.noob schrieb:

    Die Wörter die in einer Array List sind.

    Das ist keine ArrayList, sondern eine List(Of T). Folglich ist der Name array0 auch nicht passend, denn ein Array ist wieder was anderes.
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    @x3.vb.noob Willkommen im Forum. :thumbup:
    So was:

    VB.NET-Quellcode

    1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2. RichTextBox1.Text = "xyz Hallo Welt Programm"
    3. Dim index = RichTextBox1.Text.IndexOf("Programm")
    4. Dim len = "Programm".Length
    5. RichTextBox1.Select(index, len)
    6. RichTextBox1.SelectionColor = Color.Red
    7. End Sub
    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!
    Danke für die schnelle Hilfe
    Ja so in etwa hatte ich mir das vorgestellt.

    Es ist mein erstes Projekt es sol ein kleiner vbNet Sourcecode Editor werden.


    VB.NET-Quellcode

    1. 'Keywords
    2. Dim array0 As New List(Of String)({"AddHandler", "AddressOf", "Alias", "And", "AndAlso", "As", "ByRef", "ByVal", "Call", "Case", "Catch", "Char", "Class", "Const", "Continue", "Declare", "Default", "Delegate", "Dim", "DirectCast", "Do", "Each", "Else", "ElseIf", "End", "EndIf", "Enum", "Erase", "Error", "Event", "Exit", "False", "Finally", "For", "Friend", "Function", "Get", "GetType", "GetXMLNamespace", "Global", "GoSub", "GoTo", "Handles", "If", "Implements", "Imports", "In", "Inherits", "Interface", "Is", "IsNot", "Let", "Lib", "Like", "Loop", "Me", "Mod", "Module", "Module Statement", "MustInherit", "MustOverride", "MyBase", "MyClass", "Namespace','Narrowing", "New", "Next", "Not", "Nothing", "NotInheritable", "NotOverridable", "Of", "On", "Operator", "Option", "Optional", "Or", "OrElse", "Out", "Overloads", "Overridable", "Overrides','ParamArray", "Partial", "Private", "Property", "Protected", "Public", "RaiseEvent", "ReadOnly", "ReDim', 'REM", "RemoveHandler", "Resume", "Return", "Select','Set", "Shadows", "Shared", "Static", "Step", "Stop", "Structure", "Sub", "SyncLock", "Then", "Throw", "To", "True", "Try", "TryCast", "TypeOf", "Using", "Wend", "When", "While", "Widening", "With", "WithEvents", "WriteOnly", "Xor"})
    3. 'Data Types
    4. Dim array1 As New List(Of String)({"Boolean", "Byte", "Date", "Decimal", "Double", "Integer", "Long", "Object", "SByte", "Short", "Single", "String", "UInteger", "ULong", "UShort"})
    5. 'Compiler Directives
    6. Dim array2 As New List(Of String)({"Const", "Else", "ElseIf", "End", "If"})
    7. 'Constants
    8. Dim array3 As New List(Of String)({"CBool", "CByte", "CChar", "CChr", "CDate", "CDbl", "CDec','CInt", "CLng", "CLng8", "CObj", "CSByte", "CShort", "CSng", "CStr", "CType", "CUInt", "CULng", "CUShort"})
    9. 'Linq
    10. Dim array4 As New List(Of String)({"By", "From", "Group", "Where"})
    11. 'Built-in functions
    12. Dim array5 As New List(Of String)({"ABS", "ARRAY", "ASC", "ASCB", "ASCW", "CALLBYNAME", "CHOOSE", "CHR", "CHR$", "CHRB", "CHRB$", "CHRW", "CLOSE", "COMMAND", "COMMAND$", "CONVERSION", "COS", "CREATEOBJECT", "CURDIR", "CVDATE", "DATEADD", "DATEDIFF", "DATEPART", "DATESERIAL", "DATEVALUE", "DAY", "DDB", "DIR", "DIR$", "EOF", "ERROR$", "EXP", "FILEATTR", "FILECOPY", "FILEDATATIME", "FILELEN", "FILTER", "FIX", "FORMAT", "FORMAT$", "FORMATCURRENCY", "FORMATDATETIME", "FORMATNUMBER", "FORMATPERCENT", "FREEFILE", "FV", "GETALLSETTINGS", "GETATTRGETOBJECT", "GETSETTING", "HEX", "HEX$", "HOUR", "IIF", "IMESTATUS", "INPUT$", "INPUTB", "INPUTB$", "INPUTBOX','INSTR", "INSTRB", "INSTRREV", "INT", "IPMT", "IRR", "ISARRAY", "ISDATE", "ISEMPTY", "ISERROR", "ISNULL", "ISNUMERIC", "ISOBJECT", "JOIN", "LBOUND", "LCASE", "LCASE$", "LEFT", "LEFT$", "LEFTB", "LEFTB$", "LENB", "LINEINPUT", "LOC", "LOF", "LOG", "LTRIM", "LTRIM$", "MID$", "MIDB", "MIDB$", "MINUTE", "MIRR", "MKDIR", "MONTH", "MONTHNAME", "MSGBOX", "NOW", "NPER", "NPV", "OCT", "OCT$", "PARTITION", "PMT", "PPMT", "PV", "RATE", "REPLACE", "RIGHT", "RIGHT$", "RIGHTB", "RIGHTB$", "RMDIR", "RND", "RTRIM", "RTRIM$", "SECOND", "SIN", "SLN", "SPACE", "SPACE$", "SPC", "SPLIT", "SQRT", "STR", "STR$',, 'STRCOMP", "STRCONV", "STRING$", "STRREVERSE", "SYD", "TAB", "TAN", "TIMEOFDAY", "TIMER", "TIMESERIAL", "TIMEVALUE", "TODAY", "TRIM", "TRIM$", "TYPENAME", "UBOUND", "UCASE", "UCASE$", "VAL", "WEEKDAY", "WEEKDAYNAME", "YEAR"})
    13. 'Symbols
    14. Dim array6 As New List(Of String)({"+", "-", "*", "?", "=", "/", "%", "&", ">", "<", "^", "!", "(", ")", "{", "}", ".", Chr(34)})
    15. 'Comments
    16. Dim array7 As New List(Of String)({"'"})


    _____________________________________________________________

    Habe da noch was gefunden funktioniert nicht so wie ich das will,
    hm muss wohl noch ein wenig suchen was ich da so falsch machen, oder wie ich das besser umsetzen kann.

    VB.NET-Quellcode

    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2. RichTextBox1.Text = "Test Hallo By by xx From"
    3. End Sub
    4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5. Dim array4 As New List(Of String)({"By", "From", "Group", "Where"})
    6. For Each p As String In array4
    7. Me.HightLightWord(RichTextBox1, p.ToString) 'bsp: p = "By"
    8. Next
    9. End Sub
    10. Private Sub HightLightWord(ByVal ctrl As RichTextBox, ByVal word As String)
    11. With ctrl
    12. ' Reset
    13. .SelectionStart = 0
    14. .SelectionLength = .TextLength
    15. .SelectionColor = Color.Black
    16. ' Action
    17. For Each m As Match In Regex.Matches(.Text, word, RegexOptions.IgnoreCase)
    18. .SelectionStart = m.Index ' Begin
    19. .SelectionLength = m.Length ' Length
    20. .SelectionColor = Color.Blue ' Higlight Color
    21. Next
    22. End With
    23. End Sub

    x3.vb.noob schrieb:

    Es ist mein erstes Projekt es sol ein kleiner vbNet Sourcecode Editor werden.
    Fang vielleicht eine Nummer kleiner an.
    Gugst Du hier.
    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!
    Versuch mal so :

    VB.NET-Quellcode

    1. Option Strict On
    2. Imports System.Text.RegularExpressions
    3. Public Class Form1
    4. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    5. RichTextBox1.Text = "Hallo ich bin es Hallo wer bist du Hallo" & vbCrLf & "Hallo ich bin es Hallo wer bist du Hallo"
    6. End Sub
    7. Dim Highlightcolor() As String
    8. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    9. Highlightcolor = {"Bin", "Wer"}
    10. HighlightKeyWords()
    11. End Sub
    12. Private Sub HighlightKeyWords()
    13. Dim currentline As Integer = RichTextBox1.GetFirstCharIndexOfCurrentLine
    14. currentline += RichTextBox1.Lines(RichTextBox1.GetLineFromCharIndex(currentline)).Length
    15. For Each Highlight As String In Highlightcolor
    16. Dim matches As MatchCollection = Regex.Matches(RichTextBox1.Text.ToLower, Highlight.ToLower)
    17. For Each Match As Match In matches
    18. RichTextBox1.Select(Match.Index, Match.Length)
    19. RichTextBox1.SelectionColor = Color.Blue
    20. RichTextBox1.Select(currentline, 0)
    21. RichTextBox1.SelectionColor = Color.Black
    22. Next
    23. Next
    24. End Sub
    25. End Class

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „TVX“ ()