I'm a newbie.
I can load a text file into a listbox no problem, but how do I load a list of names into a variable? I don't think I know enought to even ask the question right.
Any examples of loading arrays from a file?
Printable View
I'm a newbie.
I can load a text file into a listbox no problem, but how do I load a list of names into a variable? I don't think I know enought to even ask the question right.
Any examples of loading arrays from a file?
Well, well. A fellow Newbie. Hello!
I have a project I did for a college VB.Net class I'm taking. It reads from, saves to, and deletes lines from a text file. It uses an array to store all the data, and all that good stuff. :D
However, it's too big to attach(336kb). Do you have somewhere I could send it?
The only thing is, you'll have to Unzip it into your C:\ drive for the all file stuff to work right.
Ah nevermind, I'll just post the relevant code.
VB Code:
Option Explicit On Option Strict On Imports System.Convert ''''' 'Create the structure for the record Public Structure sEventRec Public EventName As String Public EventDate As Date Public EventPrice As Double Public EventDesc As String End Structure Public Class frmDisplay Inherits System.Windows.Forms.Form ''''' 'Not really sure about this one... Private EventRec() As sEventRec Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click ''''''''''''''''''''''''''''''''''''''''''''''''''''' '' First check to make sure the Combo Box is empty ''''''''''''''''''''''''''''''''''''''''''''''''''''' If cboEvents.Items.Count = Nothing Then ''''' 'Create the streamreader, delimiter, lines, and fields Dim psrdDisplay As System.IO.StreamReader Dim pchrDelimiter() As Char = {ToChar(",")} Dim pstrline As String Dim pstrFields() As String Dim pintcount As Integer ''''' 'Use a preset fielname and read the line psrdDisplay = New System.IO.StreamReader("C:\Project\File.txt") pstrline = psrdDisplay.ReadLine() ''''' 'Stope reading when we hit a blank line Do Until pstrline = Nothing ''''' 'Otherwise spilt the line into fields ReDim Preserve EventRec(pintcount) pstrFields = pstrline.Split(pchrDelimiter) EventRec(pintcount).EventName = (pstrFields(0).ToString) cboEvents.Items.Add(EventRec(pintcount).EventName) EventRec(pintcount).EventDate = ToDateTime(pstrFields(1)) EventRec(pintcount).EventPrice = ToDouble(pstrFields(2)) EventRec(pintcount).EventDesc = (pstrFields(3).ToString) ''''' 'Increase the counter and read the next line pintcount += 1 pstrline = psrdDisplay.ReadLine() Loop ''''' 'Display a message bos when file is done opening and close 'the streamreader MsgBox("Done Opening File", MsgBoxStyle.OKOnly, "Event Tracker") psrdDisplay.Close() Else ''''' 'If the Combo Box is populated, don't re-open the file MsgBox("The File is already open", MsgBoxStyle.OKOnly, "Event Tracker Help") End If End Sub Private Sub cboEvents_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboEvents.SelectedIndexChanged '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '' When a selection is made, find that record and display '' all the info about it '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim pintcount As Integer Dim pstrRec As sEventRec ''''' 'First clear all labels lblName.Text = "" lblDate.Text = "" lblDescription.Text = "" lblPrice.Text = "" For pintcount = 0 To EventRec.GetUpperBound(0) ''''' 'Look for a match in the array If cboEvents.Text = EventRec(pintcount).EventName Then ''''' 'When match is found display cooresponding info pstrRec = EventRec(pintcount) lblName.Text = pstrRec.EventName lblDate.Text = (pstrRec.EventDate).ToShortDateString lblPrice.Text = FormatCurrency(pstrRec.EventPrice) lblDescription.Text = pstrRec.EventDesc End If Next End Sub
Hope this helps!