Hello, I have a code that adds proxies to a richtext box, and I would like to make it add each proxy to the list box, Ive tried many things. Can anyone help?

private void Button1_Click(object sender, EventArgs e)
{
    string innerHtml;
    IEnumerator enumerator;
    try
    {
        if (this.WebBrowser1.Url != new Uri("http://tools.rosinstrument.com/raw_free_db.htm"))
        {
            this.WebBrowser1.Navigate("http://tools.rosinstrument.com/raw_free_db.htm");
            this.BrowserWait(false);
        }
    }
    catch (Exception exception1)
    {
        ProjectData.SetProjectError(exception1);
        Exception exception = exception1;
        ProjectData.ClearProjectError();
    }
    Regex regex = new Regex("<TD><A\\ title=\"([0-9A-Za-z\\-\\.]*?):([0-9]*?)/[A-Za-z]*\\ stat\"\\ href=\"/cgi-bin/shdb\\.pl\\?key=[0-9A-Za-z\\-\\.]*?:[^\"]*?\">[^:]*?:[0-9]*?</A></TD>");
    try
    {
        innerHtml = this.WebBrowser1.Document.Body.InnerHtml;
    }
    catch (Exception exception3)
    {
        ProjectData.SetProjectError(exception3);
        Exception exception2 = exception3;
        innerHtml = "";
        ProjectData.ClearProjectError();
    }
    MatchCollection matchs = regex.Matches(innerHtml);
    int num = 0;
    int num2 = 0;
    try
    {
        enumerator = matchs.GetEnumerator();
        while (enumerator.MoveNext())
        {
            IEnumerator enumerator2;
            Match current = (Match) enumerator.Current;
            num = 0;
            try
            {
                enumerator2 = current.Groups.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    RichTextBox box;
                    Group group = (Group) enumerator2.Current;
                    switch (num)
                    {
                        case 1:
                            box = this.RichTextBox1;
                            box.Text = box.Text + group.Value;
                            box = this.RichTextBox2;
                            box.Text = box.Text + group.Value + "\n";
                            break;

                        case 2:
                            box = this.RichTextBox1;
                            box.Text = box.Text + ":" + group.Value + "\n";
                            break;
                    }
                    num++;
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
            num2++;
        }
    }
    finally
    {
        if (enumerator is IDisposable)
        {
            (enumerator as IDisposable).Dispose();
        }
    }
    this.iTotal += num2;
    if (this.iCounter < 50)
    {
        this.page.Text = Conversions.ToString(this.iCounter);
        this.proxy.Text = Conversions.ToString(this.iTotal);
        if (num2 > 0)
        {
            this.iCounter++;
            this.iAttempts++;
        }
        if (this.iAttempts < 3)
        {
            this.iAttempts = 0;
            this.WebBrowser1.Navigate("http://tools.rosinstrument.com/raw_free_db.htm?" + this.iCounter.ToString());
            this.BrowserWait(false);
            this.Button1_Click(RuntimeHelpers.GetObjectValue(sender), e);
        }
        else
        {
            Interaction.MsgBox("Can't get Proxies... WHY!?", MsgBoxStyle.OkOnly, null);
            this.RichTextBox2.Text = this.WebBrowser1.Document.Body.InnerHtml;
        }
    }
    else
    {
        Interaction.MsgBox("Done. Fetched: " + this.iTotal.ToString() + " proxies", MsgBoxStyle.OkOnly, null);
    }
}

Dani AI

Generated

Short answer for : the simplest, fastest way is to use the RichTextBox.Lines array and push that into the ListBox in one go, filtering empty lines and trimming whitespace first.

Example (safe, fast, and readable):

using System.Linq;

var lines = richTextBox2.Lines
    .Select(s => s.Trim())
    .Where(s => s.Length > 0)
    .Distinct()
    .ToArray();

listBox1.BeginUpdate();
listBox1.Items.Clear();
listBox1.Items.AddRange(lines);
listBox1.EndUpdate();

Notes and troubleshooting:

  • Lines already splits on the control's newlines; use Trim() and Where(...) to remove blank entries. Distinct() prevents duplicates.
  • If you need to add items as you parse (instead of after everything is collected), call listBox1.Items.Add(item) inside the loop, but wrap multiple adds with BeginUpdate()/EndUpdate() for performance.
  • If parsing happens on a background thread, marshal updates to the UI thread: check InvokeRequired and call Invoke(...) when necessary, or collect results on the worker thread then update the ListBox once on the UI thread.
  • If you bind the ListBox to a data source, use a BindingList<string> (or update the underlying list and reset the binding) instead of Items.AddRange.

If the RichTextBox contains extra markup or HTML, parse/clean each line to produce only the host:port string before adding it to the ListBox so entries are normalized and reliable.

Actualy, how would I be able to do, for each line in richtextbox, add each line to the listbox?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.