jeudi 19 mars 2015

Reading Text File populate it into Array ( Please I need a deep help )


Here the full question:


A set of four text-files are supplied, each file: is specific to a particular “food group” contains a list of “food items” and related calorie count information per 100gram portion of the listed food items


Program Specification Develop a Visual Basic Express 2012 program which allows the listing and selection option of one of the four “food groups”, in the form of a combo-box (when the program starts)


the population of appropriate listings of “food items” into another combo-box, when a food group is selected o the food item combobox should re-populate when a different food group is chosen o the food item combobox should be populated by


(i) reading the textfile via the TextFileReader, and then populating two separate arrays


(ii) populating the combobox via one of the previous arrays


when a food item is selected from the food item combo-box, the individual calorie information of that one food item to be displayed, by way of a label, on the form itself


the option to add the information of the selected food item as entries in two listboxes o one listbox display the food item name o one listbox displaying the food item calories


the addition of up to a maximum of 8 food items to the listboxes


the display of o the number of items in the food name listbox o the total calories of all the food item calorie listbox entries


both by way of a label on the form itself.


My problem I stopped till the part how can I deal with the calories part and make it calculate and please I am new in VB try use primitive coding.


One example of the text file : Cheese average,440 Cheddar types average reduced fat,260 Cheese spreads average,270



Public strFileLocation As String

Public strFileName As String

Public strFullFilePath As String

Public strReadRow() As String

Public strReadFoodItems(7) As String

Public intReadCalories(7) As Integer


Public intCountFoodItems As Integer

Public strEntry As String

Public intCalorieEntry As Integer




Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

'Fill in ComboBoxFoodGroups with information

cboFoodGroups.Items.Add("Dairy")
cboFoodGroups.Items.Add("Fat & Sugars")
cboFoodGroups.Items.Add("Fruits & Veg")
cboFoodGroups.Items.Add("Meat & Fish")



End Sub

Private Sub ReadTextFiles()

FoodGroupsChoice()

strFullFilePath = strFileLocation & strFileName

Using MyReader As New mvb.FileIO.TextFieldParser(strFullFilePath)

MyReader.TextFieldType = FileIO.FieldType.Delimited

MyReader.SetDelimiters(",")


Do While (Not MyReader.EndOfData)


strReadRow = MyReader.ReadFields()

strReadFoodItems(7) = strReadRow(0)
intReadCalories(7) = strReadRow(1)

cboFoodItems.Items.Add(strReadFoodItems(7))

Loop

End Using

End Sub

Private Sub FoodGroupsChoice()

If cboFoodGroups.Text = "Dairy" Then
cboFoodItems.Items.Clear()
strFileLocation = "C:\Users\M7edShin\Desktop\VB Programs\TextFiles\"
strFileName = "Dairy.txt"

ElseIf cboFoodGroups.Text = "Fat & Sugars" Then
cboFoodItems.Items.Clear()
strFileLocation = "C:\Users\M7edShin\Desktop\VB Programs\TextFiles\"
strFileName = "FatSugars.txt"

ElseIf cboFoodGroups.Text = "Fruits & Veg" Then
cboFoodItems.Items.Clear()
strFileLocation = "C:\Users\M7edShin\Desktop\VB Programs\TextFiles\"
strFileName = "FruitsVeg.txt"

ElseIf cboFoodGroups.Text = "Meat & Fish" Then
cboFoodItems.Items.Clear()
strFileLocation = "C:\Users\M7edShin\Desktop\VB Programs\TextFiles\"
strFileName = "MeatFish.txt"

End If

End Sub

Private Sub cboFoodGroups_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboFoodGroups.SelectedIndexChanged
ReadTextFiles()
End Sub

Private Sub cboFoodItems_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboFoodItems.SelectedIndexChanged

intCountFoodItems = lstFoodItems.Items.Count

If intCountFoodItems = 8 Then
MsgBox("Full")
Exit Sub
End If

strEntry = cboFoodItems.Text

strReadFoodItems(intCountFoodItems) = strEntry

intCountFoodItems = intCountFoodItems + 1

lstFoodItems.Items.Add(strEntry)


For i = 0 To intReadCalories.Count - 1
lstFoodCalories.Items.Add(intReadCalories(7))
lblItemsAndTotalCalories.Text = intCountFoodItems
Next

End Sub




Aucun commentaire:

Enregistrer un commentaire