Results 1 to 5 of 5

Thread: [RESOLVED] Color Change of Line in Rich Text Box Based on Number of

  1. #1

    Thread Starter
    Addicted Member dprontnicki's Avatar
    Join Date
    Sep 2016
    Posts
    151

    Resolved [RESOLVED] Color Change of Line in Rich Text Box Based on Number of

    Hi,

    I have a rich text box that populates from a license program. There are lines generated as such: Users of 86815AECCOL_T_F: (Total of 14 licenses issued; Total of 6 licenses in use)

    I have a method that changes the color of the line if it sees "Users of 86815AECCOL_T_F:". What I want to do is change color based on license count. In pseudo code, like this:

    if Line contains "Users of 86815AECCOL_T_F:" and 14 of 14 licenses are used change color to Red

    if Line contains "Users of 86815AECCOL_T_F:" and 5 of 14 licenses are used change color to Green

    Ideally it would be great to have three levels 0% to 50% - Green, 51% to 99% - Orange, and 100% - Red

    I hope that makes sense. Keep in mind each server has a different license count. One could contain 12 licenses or 4 licenses, so i cant search for the exact phase. And I can only parse the text, that I am aware of, there is access to API.

    Below is the code I am using now:

    Code:
        Private Sub rtbOutput_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rtbOutput.TextChanged
    
            Dim lines() As String = rtbOutput.Text.Split(vbLf)
            Dim startIndex As Integer = 0
    
            For i As Integer = 0 To lines.Length - 1
    
                rtbOutput.Select(startIndex, lines(i).Length)
    
                If lines(i).StartsWith("Users of 86815AECCOL_T_F:") Then
    
                    rtbOutput.SelectionColor = Color.Red
                    rtbOutput.SelectionFont = New Font(rtbOutput.SelectionFont, FontStyle.Bold)
    
                ElseIf lines(i).StartsWith("License server status:") Then
    
                    rtbOutput.SelectionColor = Color.DodgerBlue
                    rtbOutput.SelectionFont = New Font(rtbOutput.SelectionFont, FontStyle.Bold)
    
                ElseIf lines(i).StartsWith("------") Then
    
                    rtbOutput.SelectionColor = Color.DodgerBlue
                    rtbOutput.SelectionFont = New Font(rtbOutput.SelectionFont, FontStyle.Bold)
    
                Else
    
                    rtbOutput.SelectionColor = Color.Black
    
                End If
    
                startIndex += lines(i).Length + vbLf.Length
    
            Next
    
        End Sub
    Name:  Capture.PNG
Views: 354
Size:  16.0 KB

  2. #2
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    Re: Color Change of Line in Rich Text Box Based on Number of

    Looking at the lines you are using perhaps a regular expression picking out the 2nd and 3rd values would work, once the numbers are converted and placed in their own variable you can do whatever you want with the values.

    Code:
     'Imports System.Text.RegularExpressions
    
    
            Dim source As String = "  Users of 86815AECCOL_T_F: (Total of 14 licenses issued; Total of 6 licenses in use)
    "
            Dim parser As New Regex("[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?")
            Dim result(2) As String
    
            Dim sourceMatches As MatchCollection = parser.Matches(source)
            Dim idx As Integer
    
            For idx = 0 To sourceMatches.Count - 1
    
                result(idx) &= sourceMatches(idx).Value.ToString()
    
            Next idx
    
            MsgBox("Issued " & result(1) & vbCr & "In use " & result(2))

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Color Change of Line in Rich Text Box Based on Number of

    Regex is overkill in my opinion here...

    Code:
    Dim source As String = "  Users of 86815AECCOL_T_F: (Total of 14 licenses issued; Total of 6 licenses in use)
    dim parts() as string = source.split(new string() {"Total of "}, stringsplitoptions.none)
    
    dim issued as integer = cint(val(parts(1))
    dim used as integer = cint(val(parts(2))

  4. #4

    Thread Starter
    Addicted Member dprontnicki's Avatar
    Join Date
    Sep 2016
    Posts
    151

    Re: Color Change of Line in Rich Text Box Based on Number of

    Mc_VB,

    Thank you very much! This worked great. I really need learn/get better at Regular Expressions.

  5. #5
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: [RESOLVED] Color Change of Line in Rich Text Box Based on Number of

    hi

    as long as the Loaded file has only 1 ref. to a License it will work, if there are more you
    will get a error ..Index out of Range.. or something like that
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width