|
-
Nov 23rd, 2002, 08:39 AM
#1
Thread Starter
Junior Member
-
Nov 23rd, 2002, 10:20 AM
#2
Frenzied Member
Use the classes in the framework. It will be much easier. How is the file formated? Is it comma separated or is line by line?
Dont gain the world and lose your soul
-
Nov 23rd, 2002, 10:52 AM
#3
Thread Starter
Junior Member
i'm a newbie ..so u gotta be more specifc
and the file is line by line with commas as well ,because i want to sperate the line into 2 and store them in an array ,but i've not got a clue how it is done??...so it like this
utguugsdgyiugh, $556
hgihgegiygwiefyi, $568
.
.
.
EOF
thanks
-
Nov 23rd, 2002, 02:01 PM
#4
Registered User
I coulnt come up with anything better than this....
Code:
Dim st As Stream = File.OpenRead("D:\Test.txt")
Dim sr As New StreamReader(st)
Dim a(1, 21) As String
Dim temp As String
Dim c As Integer = 0
sr.BaseStream.Seek(0, SeekOrigin.Begin)
While (sr.Peek() > -1)
temp = sr.ReadLine()
a(0, c) = temp.Substring(0, temp.IndexOf(","c))
a(1, c) = temp.Substring(temp.IndexOf(","c) + 2)
c += 1
End While
Theres probably a smother way to do it but I'm more into beers than VB at the moment.
I'll get back if I find something beter in the morning.
/Leyan
-
Nov 23rd, 2002, 05:14 PM
#5
PowerPoster
This is in C#, but the concepts are pretty much the same. I use this to read in a file and add each line of the file to a listbox.
Code:
try
{
string sTemp;
string sTemp1;
// Create the path to the file.
sTemp = Application.StartupPath;
sTemp += @"\selected.typ";
if(File.Exists(sTemp))
{
// Clear the list box
lbSearchForTypes.Items.Clear();
// Create a fileinfo object and open it into a stream.
FileInfo fiSource = new FileInfo(sTemp);
StreamReader stream = fiSource.OpenText();
// Read the first line of the file.
sTemp1 = stream.ReadLine();
do
{
// Add the file to the list.
lbSearchForTypes.Items.Add(sTemp1);
// Read the next line in.
sTemp1 = stream.ReadLine();
// Check to see if we are at end of file. If not, loop.
}while (sTemp1 != null);
}
}
catch (System.Exception e)
{
// This is here for debug purposes. May be left in the app for release though.
MessageBox.Show(e.Message.ToString());
}
-
Nov 26th, 2002, 10:34 AM
#6
New Member
List control population
I have written two funtions to populater list controls from both databases and text files. Hope these help :
Public Function PopulateListControlFromFile(ByVal ListControl As Object, _
ByVal FullPath As String, Optional ByVal Delimiter As String = vbCrLf) As Boolean
Dim objFso As New Scripting.FileSystemObject()
Dim objTextStream As Scripting.TextStream
Dim strContents As String
Dim arrContents() As String
Dim lCtr As Long, lCount As Long
Try
'Ensure file exists
If Dir(FullPath) = "" Then
MsgBox("The file " & FullPath & " does not exist", MsgBoxStyle.OKOnly, "Load contract lists")
Exit Function
End If
'Get file contents
objTextStream = objFso.OpenTextFile(FullPath, Scripting.IOMode.ForReading)
strContents = objTextStream.ReadAll
objTextStream.Close()
'Split file contents based on delimiter
arrContents = Split(strContents, Delimiter)
lCount = UBound(arrContents)
'Clear List Control.
ListControl.Items.Clear()
'Populate list control
ListControl.Items.AddRange(arrContents)
PopulateListControlFromFile = True
Catch ex As Exception
objTextStream = Nothing
objFso = Nothing
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Load contract lists")
End Try
End Function
and to do the same from a database:
Public Function PopulateListControlFromDb(ByVal ListControl As Object, _
ByVal Database As String, ByVal DataTable As String, ByVal DataColumn As String) As Boolean
'connection stuff
Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Database)
Dim cm As New OleDb.OleDbCommand("SELECT * FROM" & " " & DataTable, con)
Dim dr As OleDb.OleDbDataReader
'empty ListControl first
ListControl.Items.Clear()
'read table and fill ListControl
Try
con.Open()
dr = cm.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read()
ListControl.Items.Add(dr(DataColumn))
End While
Catch ex As Exception
MessageBox.Show(ex.Source & ": " & ex.Message)
Finally
cm.Dispose()
cm = Nothing
con.Dispose()
con = Nothing
dr = Nothing
End Try
End Function
-
Nov 26th, 2002, 03:00 PM
#7
Frenzied Member
I think rgreen is missing something - what imports he uses.
~Peter

-
Nov 26th, 2002, 03:24 PM
#8
New Member
Imports ADODB
Imports System.Data
Imports System.Data.OleDb
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|