0

I have tried to make a program that would parse a raw list from Chrome://policy (input by RichTextbox) into a text array, then dump it into another RichTextbox. All of the raw string are exactly 32 characters long, followed by a comma. Here is the code:

Public Class Form1
    Dim tempExt As String
    Dim extsHandled As Integer
    Dim numOfExts As Integer
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If RichTextBox1.Text = "" Then
            MsgBox("Please enter the raw extensions from Chrome://policy")
        Else
            RichTextBox3.Text = RichTextBox1.Text
            numOfExts = TextBox1.Text
            Dim place As Integer = 0
            Dim exts(150) As String
            While extsHandled < numOfExts
                tempExt = RichTextBox1.Text.Substring(0, 32)
                exts(place) = tempExt
                RichTextBox1.Text.Remove(0, 33)
                place = place + 1
                extsHandled = extsHandled + 1
            End While
            Dim newPlace As Integer = 0
            While newPlace < numOfExts
                RichTextBox2.AppendText(exts(newPlace))
                newPlace = newPlace + 1
                RichTextBox2.AppendText(" ")
            End While

        End If
    End Sub
End Class

Most of it works, but it would seem something is going wrong with removing the characters from the richtextbox, as when I run it, it only parses the first part of the string over and over:

enter image description here

Am I doing something wrong?

3
  • You should turn on Option Strict. That will help you fix your errors. Commented Sep 27, 2018 at 18:18
  • @JoelCoehoorn New code. Option strict changed nothing. Same results Commented Sep 27, 2018 at 18:27
  • Also... that text array is gone at the end of the method. Did you want to keep it somewhere? Commented Sep 27, 2018 at 18:30

2 Answers 2

1

If it's always like that you can do it like this:

RichTextBox3.Text = RichTextBox1.Text.Replace(",", vbNewLine)

The #3 is your result, while #1 is original right?

enter image description here

Ah yeah, you can count how many there simply by

RichTextBox2.Text=  RichTextBox1.Text.Split({","}, StringSplitOptions.RemoveEmptyEntries).Count.ToString
Sign up to request clarification or add additional context in comments.

2 Comments

I'm such an idiot I don't know why I didn't do this from the start. Thank you
Sometimes the obvious can not be seen. I had my moments like that too.
0

This line returns a new string:

RichTextBox1.Text.Remove(0, 33)

It does not modify the textbox in place. The next iteration through the loop, you're still working with the original value, looking at the same set of 32 characters at the beginning of the string.

Additionally, nothing in this code initializes the extsHandled variable. You should turn on Option Strict, which helps catch that kind of error. Running of Option Strict off is poor practice. You should also give a meaningful name to any control you will actually reference from code.

It's not clear to me right now the exact format. If it's all on the same line (no line break characters as part of the string, even if it wraps), this should work:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If String.IsNullOrWhitespace(RichTextBox1.Text) Then
        MsgBox("Please enter the raw extensions from Chrome://policy")
        Exit Sub
    End If

    RichTextBox3.Text = RichTextBox1.Text
    Dim exts() As String
    Using rdr As New TextFieldParser(RichTextBox1.Text)
        rdr.TextFieldType = FileIO.FieldType.Delimited
        rdr.Delimiters = New String() {","}
        exts = rdr.ReadFields()
    End Using

    For Each ext As String In exts
         RichTextBox2.AppendText(ext)
    Next ext

    RichTextBox1.Text = ""
End Sub

The problem is this code doesn't do anything. The array is gone when the method ends. Consider making the array a property of the class, or having method that returns the array as the result.

You can also look at this, to save typing into the textbox, though it's just a starting point:

Public Function GetChromeExtensionKeys() As IEnumerable(Of String)
    Dim BasePath As String = 
EndEnvironment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
    BasePath = Path.Combine(BasePath, "Google\Chrome\User Data")

    Dim dflt As String = Path.Combine(BasePath, "Default\Extensions")
    Dim profileExts() As String = Directory.GetDirectories(BasePath, "Profile *").
          Select(Function(p) Path.Combine(p, "Extensions"))

    Dim result As New List(Of String)()
    result.AddRange(Directory.GetDirectories(dflt))
    For Each folder As String In profiles
        result.AddRange(Directory.GetDirectories(folder))
    Next folder   

    Return result.Select(Function(e) Path.GetFileName(e)).Distinct()
Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each ext As String In GetChromeExtensionKeys()
         RichTextBox2.AppendText(ext)
    Next ext
End Sub

5 Comments

There's a formatting error on line 18. While line = sr.ReadLine() IsNot Nothing VB is complaining about this.
You need the parentheses. Why did you remove them?
The parentheses show up as gray. That's not the error it is complaining about. Same thing happens with them
I'm actually re-working this a bit. I'm less sure than I was those strings are on separate lines, so give me a minute.
This new case does not work for multiple reasons. Reason 1: System.IO.PathTooLongException. Reason 2: It changed the code completely instead of sticking with the original, and does not accomplish the goal that originally somewhat worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.