Jump to content

  •  

  • iBotModz CBox


    Photo

    Listbox updates from Database


    • Please log in to reply
    7 replies to this topic

    #1 ixGAMEOVERxixx

    ixGAMEOVERxixx

      Private Grade 2

    • VIP

    • 478 posts
    • Joined: 27-October 08
    • Gender:Not Telling
    • Location:Custom Games

    Posted 26 August 2009 - 05:48 PM

    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, 04 September 2009 - 03:06 PM.


    #2 SotG Caboose

    SotG Caboose

      TheMasterSnails Pimp!

    • Donors+

    • 827 posts
    • Joined: 29-May 08
    • Gender:Male

    Posted 26 August 2009 - 06:15 PM

    WebClient MyClient = new WebClient();
    
        Stream s = MyClient.OpenRead("http://www.lolcats.com");
    
        StreamReader MyReader = new StreamReader(s);
    
    
    
        Listbox1.Items.Add(MyReader.ReadLine());
    
        s.Close();


    #3 ixGAMEOVERxixx

    ixGAMEOVERxixx

      Private Grade 2

    • VIP

    • 478 posts
    • Joined: 27-October 08
    • Gender:Not Telling
    • Location:Custom Games

    Posted 26 August 2009 - 06:32 PM

    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

    #4 SotG Caboose

    SotG Caboose

      TheMasterSnails Pimp!

    • Donors+

    • 827 posts
    • Joined: 29-May 08
    • Gender:Male

    Posted 26 August 2009 - 06:35 PM

    At the top, type:

    Using System.IO
    Using System.Web(or Net, I forgot :p)

    #5 iBotPeaches

    iBotPeaches

      General Grade 6

    • Owner

    • 6,570 posts
    • Joined: 29-July 07
    • Gender:Male
    • Location:Kansas

    Posted 27 August 2009 - 05:30 AM

    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.

    #6 ixGAMEOVERxixx

    ixGAMEOVERxixx

      Private Grade 2

    • VIP

    • 478 posts
    • Joined: 27-October 08
    • Gender:Not Telling
    • Location:Custom Games

    Posted 27 August 2009 - 08:50 AM

    How can I get it to do the last bit you mentioned?

    #7 iBotPeaches

    iBotPeaches

      General Grade 6

    • Owner

    • 6,570 posts
    • Joined: 29-July 07
    • Gender:Male
    • Location:Kansas

    Posted 27 August 2009 - 04:48 PM

    The peek or foreach statement?

    #8 Rogue Modder

    Rogue Modder

      Class of 2008

    • VIP

    • 1,328 posts
    • Joined: 02-January 09
    • Gender:Male
    • Location:London, UK

    Posted 27 August 2009 - 04:52 PM

    Yeah.