Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,053 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,737 people online right now. Registration is fast and FREE... Join Now!




StreamReader/Writer

 
Reply to this topicStart new topic

StreamReader/Writer

MitchTher
22 Feb, 2008 - 09:08 AM
Post #1

New D.I.C Head
*

Joined: 22 Feb, 2008
Posts: 6



Thanked: 1 times
My Contributions
I am doing an assignment where I need to load in a combobox a list (I have in a .txt) which has the time zones, countries, etc.

I want the combobox to read from the text file so when I run my program I can click the city from the combo box it shows me the time zone and current date and time there.

I have extremly poor VB skills and am completly lost, is anyone able to point me in the right direction?

I have attached a screenshot of what im looking to get as a final completion.

Thanks


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

MitchTher
RE: StreamReader/Writer
22 Feb, 2008 - 09:48 AM
Post #2

New D.I.C Head
*

Joined: 22 Feb, 2008
Posts: 6



Thanked: 1 times
My Contributions
vb

Public Class Form1
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\time.txt"
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Me.ComboBox2.Text = objReader.ReadToEnd
objReader.Close()
End Sub
End Class




This is what ive got so far, right now all of my information is going to the combo box, I want it to read it and display it line by line. So for example if someone picks "Halifax Nova Scotia" I want it to be able to display the time zone and current date and time below the combo box as shown in my screenshot.

This post has been edited by PsychoCoder: 22 Feb, 2008 - 09:56 AM
User is offlineProfile CardPM
+Quote Post

MitchTher
RE: StreamReader/Writer
22 Feb, 2008 - 10:11 AM
Post #3

New D.I.C Head
*

Joined: 22 Feb, 2008
Posts: 6



Thanked: 1 times
My Contributions
QUOTE(MitchTher @ 22 Feb, 2008 - 10:48 AM) *

vb

Public Class Form1
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\time.txt"
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Me.ComboBox2.Text = objReader.ReadToEnd
objReader.Close()
End Sub
End Class




This is what ive got so far, right now all of my information is going to the combo box, I want it to read it and display it line by line. So for example if someone picks "Halifax Nova Scotia" I want it to be able to display the time zone and current date and time below the combo box as shown in my screenshot.



The other thing im looking at doing is putting all my cities in a collection in the combobox, but I dont know how to make it so when you pick some example "halifax" it then loads the time zone and date into the labels below..
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: StreamReader/Writer
22 Feb, 2008 - 10:31 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
To accomplish this you're going to want to use the ReadLine Method of the StreamReader Class, along with Peek Method. Peek acts the same was as EOF worked in legacy VB (VB6 and earlier). This will allow you to search each line for the value you're looking for, something like:

vb

''' <summary>
''' method to read through a text file a line at
''' a time searcing for a particular value that is
''' passed into the method
''' </summary>
''' <param name="value">value we are looking for</param>
''' <returns>the value we're looking for</returns>
Public Function FindValue(ByVal value As String) As String
'variable to hold the return value
Dim returnValue As String = String.Empty
'open your file
Dim reader As New StreamReader("C:\MyFile")
'now use Peek to loop through each line
While reader.Peek() >= 0
'create a variable to hold the current line in
Dim line As String = reader.ReadLine()
'now use an if statement to check the value of the current line
If line = value Then
'do your work here
returnValue = line
'since we have our value we need to exit the loop
Exit While
End If
End While
'return the value
Return returnValue
End Function



That should at least get you started smile.gif Also, moved to VB.Net ass this is a VB.Net issue smile.gif
User is offlineProfile CardPM
+Quote Post

MitchTher
RE: StreamReader/Writer
22 Feb, 2008 - 10:47 AM
Post #5

New D.I.C Head
*

Joined: 22 Feb, 2008
Posts: 6



Thanked: 1 times
My Contributions
I appreciate the help, But im still not sure what to put in "your work here" and how I get it to display... Safe to say im not going to become a programmer, but unfortunally I have to get threw this course..
User is offlineProfile CardPM
+Quote Post

MitchTher
RE: StreamReader/Writer
26 Feb, 2008 - 01:12 PM
Post #6

New D.I.C Head
*

Joined: 22 Feb, 2008
Posts: 6



Thanked: 1 times
My Contributions
Heres what im working with now, but I cant get my information to show up in my combo box..
CODE

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim sr As New StreamReader("c:\time.txt")

        Dim line As String
        Dim lineValues() As String
        Dim contents As New ArrayList()
        Dim finished As Boolean = False

        While Not finished

            line = sr.ReadLine()
            If line Is Nothing Then
                finished = True
            Else
                lineValues = Split(line, ",")
                contents.Add(lineValues)
            End If

            Me.ComboBox1.Show(contents)

        End While


End Class

User is offlineProfile CardPM
+Quote Post

Jayman
RE: StreamReader/Writer
26 Feb, 2008 - 02:29 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Use code.gif tags when posting your code.

Since you are using an ArrayList, all you need to do is set the DataSource property of the combobox to point to the ArrayList.

CODE

Me.ComboBox1.DataSource = contents

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:22PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month