Jump to content

cbox


Recommended Posts

Posted (edited)

Working code.

 

VB.net

 

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Imports System.Net
Imports System.Web

   Public Partial Class Form1
       Inherits Form
       Public Sub New()
           InitializeComponent()
       End Sub
       
       Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As EventArgs)
           Try
               Dim MyClient As New WebClient()
               Dim s As Stream = MyClient.OpenRead("http://www.flashinthepan.eu/message.txt")
               
               Dim MyReader As New StreamReader(s)
               Dim line As String = ""
               While (InlineAssignHelper(line, MyReader.ReadLine())) IsNot Nothing
                   listBoxlolcats.Items.Add(line)
               End While
               MyReader.Close()
           Catch x As Exception
               If MessageBox.Show("Retry?", "Error!", MessageBoxButtons.RetryCancel, MessageBoxIcon.[Error]) = DialogResult.Retry Then
                   Dim MyClient As New WebClient()
                   Dim s As Stream = MyClient.OpenRead("http://www.flashinthepan.eu/message.txt")
                   Dim MyReader As New StreamReader(s)
                   Dim line As String = ""
                   While (InlineAssignHelper(line, MyReader.ReadLine())) IsNot Nothing
                       listBoxlolcats.Items.Add(line)
                   End While
                   MyReader.Close()
               Else
                   Exit Sub
               End If
           End Try
       End Sub
       Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
           target = value
           Return value
       End Function
   End Class

 

C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Web;

namespace WindowsFormsApplication1
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void btnLoad_Click(object sender, EventArgs e)
       {
           try
           {
               WebClient MyClient = new WebClient();
               Stream s = MyClient.OpenRead("http://www.flashinthepan.eu/message.txt");

               StreamReader MyReader = new StreamReader(s);
               string line = "";
               while ((line = MyReader.ReadLine()) != null)
               {
                   listBoxlolcats.Items.Add(line);
               }
               MyReader.Close();
           }
           catch (Exception x)
           {
               if (MessageBox.Show(x + Environment.NewLine + Environment.NewLine + "Retry?", "Error!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
               {
                   WebClient MyClient = new WebClient();
                   Stream s = MyClient.OpenRead("http://www.flashinthepan.eu/message.txt");
                   StreamReader MyReader = new StreamReader(s);
                   string line = "";
                   while ((line = MyReader.ReadLine()) != null)
                   {
                       listBoxlolcats.Items.Add(line);
                   }
                   MyReader.Close();
               }
               else
               {
                   return;
               }
           }
       }
   }
}

Edited by ixGAMEOVERxixx
  • Like 1
Posted

SotG I converted that C# Snippet into VB.net and got this..

 

Dim MyClient As New WebClient()

Dim s As Stream = MyClient.OpenRead("http://www.lolcats.com")

Dim MyReader As New StreamReader(s)

ListBox1.Items.Add(MyReader.ReadLine())

s.Close()

 

I get the following errors -

 

Type 'WebClient' is not defined.

Type 'Stream' is not defined.

Type 'StreamReader' is not defined.

 

Any help? Thanks

Posted

When you download the string from the text file use this to split it into an array with each line a new item in the array.

 

string[] lineArray = Regex.Split(MyReader.ReadToEnd(), "\r\n");

 

Then like a foreach statement for that array to pull it into a listbox line by line. Or do like do until MyReader.Peek = -1.

  • Like 1

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...