vb.net - How do you calculate a result from a listbox selection in Visual Basic? (The data in the listbox is being read off a text file) -
so asked in school create railway ticketing system program allow buy either single or return ticket , based off cost displayed. information of destinations read via text file. text file given below:
knapford , 1 crosby , 8 weltworth , 15 maron , 23 cronk , 28 kildane , 31 keltthorpe road , 46 crovan's gate , 56 vicarstown , 76 barrow , 77
i have managed separate integers destination names via code below displayed in listbox:
public class purchasescreen dim filename string private sub form2_load(byval sender system.object, byval e system.eventargs) handles mybase.load filename = "stationfile.txt" dim sr new streamreader("stationfile.txt") dim word string = "" dim words(11) string dim integer = 0 until sr.peek = -1 'grab 1 word @ time text file word = sr.readline() 'place word array words(i) = word 'increment array counter = + 1 loop return end sub
the problem facing trying access numbers omitted code, since destination name can displayed in listbox. how go using numbers perform calculations?
i've edited post format of text file clearer users reading question
so make easier access data, better having station
structure , storing each station in list of these structures.
to populate list, iterate through list of stations , add names list.
when item selected, use selected item's index data in stations list , there go.
'obviously need change path match "stations.txt"file stored 'and possibly name of listbox you're populating dim filename string = "d:\visual studio 2015\projects\blank\blank\stations.text" dim selectedstation station 'a structure of station can used store data in stations.txt file 'in clearer more maintainable way structure station dim name string dim data single end structure 'list store data stations.txt dim stations new list(of station) 'your form's load event private sub form2_load(byval sender system.object, byval e system.eventargs) handles mybase.load stations.clear() readstationdata() populatelistbox() end sub private sub readstationdata() dim tempdataline string = "" 'read stations.txt data string using sr new streamreader(filename) while not sr.endofstream tempdataline = sr.readline dim newstation station dim tempsplit() string = tempdataline.split(new string() {" , "}, stringsplitoptions.removeemptyentries) newstation.name = tempsplit(0) newstation.data = csng(tempsplit(1)) stations.add(newstation) end while end using end sub 'clear listbox , populate stations list private sub populatelistbox() listbox1.items.clear() each stationitem station in stations listbox1.items.add(stationitem.name) next end sub private sub listbox1_selectedindexchanged(sender object, e eventargs) handles listbox1.selectedindexchanged 'dummy code show usage selectedstation = stations(listbox1.selectedindex()) messagebox.show("selected station = " & selectedstation.name & vbcrlf & "station price = " & selectedstation.data) end sub
Comments
Post a Comment