vb.net - Importing data from a text file -


ok, i'm doing total overhaul. got months working properly! said, should modifying try understand better. tried add 2 more list boxes test. program testing milestones (years old - 10 years old through 100). edited code don't know line check change testing month year? new list box added displays same information month test, versus i'm trying accomplish. example: john doe 4/9/2003 show in '10' milestone.

  private sub lbmonth_selectedindexchanged(byval sender object, byval e eventargs) handles lbmonth.selectedindexchanged     if lbmonth.selectedindex < 0 return     lbperson.items.clear()     dim index integer = lbmonth.selectedindex     each ele in birthdays(index + 1)         lbperson.items.add(ele)     next end sub  private sub lbmilestone_selectedindexchanged(byval sender object, byval e eventargs) handles lbmilestone.selectedindexchanged     if lbmilestone.selectedindex < 0 return     lbperson2.items.clear()     dim index integer = lbmilestone.selectedindex     each ele2 in birthdays2(index)         lbperson2.items.add(ele2)     next end sub  private birthdays(12) list(of string) private birthdays2(10) list(of string)  private sub form1_load(byval sender object, byval e eventargs) handles me.load     'initialize month list     lbmonth.items.clear()     lbmonth.items.add("january")     lbmonth.items.add("february")     lbmonth.items.add("march")     lbmonth.items.add("april")     lbmonth.items.add("may")     lbmonth.items.add("june")     lbmonth.items.add("july")     lbmonth.items.add("august")     lbmonth.items.add("september")     lbmonth.items.add("october")     lbmonth.items.add("november")     lbmonth.items.add("december")      lbmilestone.items.clear()     lbmilestone.items.add("10")     lbmilestone.items.add("20")     lbmilestone.items.add("30")     lbmilestone.items.add("40")     lbmilestone.items.add("50")     lbmilestone.items.add("60")     lbmilestone.items.add("70")     lbmilestone.items.add("80")     lbmilestone.items.add("90")     lbmilestone.items.add("100")     'initialize lists (instance required in order access each list-object)     integer = 0 12         birthdays(i) = new list(of string)     next      j integer = 0 10         birthdays2(j) = new list(of string)     next      'load birthdays     dim filename string = application.startuppath + "\birthday.txt"     if not my.computer.filesystem.fileexists(filename) throw new exception("filename """ + filename + """ not exist!")      dim filecontent string = my.computer.filesystem.readalltext(filename)     dim lines() string = split(filecontent, vbcrlf)     each ele string in lines         dim line string = ele.trim         dim datepos integer = line.lastindexof(vbtab) 'find last space between name , date         if datepos < 5 continue 'if full name less 5 chars, not line entry         dim datestring string = mid(line, datepos + 2) 'all after last space date         dim name string = mid(line, 1, datepos).trim ' before last space name         'dim birthday date = convert.todatetime(parts(1).trim) ' used conversion before, lets try other way         dim birthday date         try             birthday = datetime.parseexact(datestring, "m/d/yyyy", system.globalization.cultureinfo.getcultureinfo("en-us"))         catch ex exception             continue         end try          dim month integer = birthday.month         dim year integer = cint(date.now.subtract(birthday).totaldays / 365 / 10)         birthdays(month).add(name)         birthdays2(year).add(name)     next   end sub 

create new project , add 2 listboxes. name 1 listbox lbmonth, other lbperson

then insert following code:

private sub lbmonth_selectedindexchanged(sender object, e eventargs) handles lbmonth.selectedindexchanged     if lbmonth.selectedindex < 0 return     lbperson.items.clear()     dim index integer = lbmonth.selectedindex     each ele in birthdays(index + 1)         lbperson.items.add(ele)     next end sub  private birthdays(12) list(of string)  private sub form1_load(sender object, e eventargs) handles me.load     'initialize month list     lbmonth.items.clear()     lbmonth.items.add("january")     lbmonth.items.add("february")     lbmonth.items.add("march")     lbmonth.items.add("april")     lbmonth.items.add("may")     lbmonth.items.add("june")     lbmonth.items.add("july")     lbmonth.items.add("august")     lbmonth.items.add("september")     lbmonth.items.add("october")     lbmonth.items.add("november")     lbmonth.items.add("december")      'initialize lists (instance required in order access each list-object)     integer = 0 12         birthdays(i) = new list(of string)     next      'load birthdays     dim filename string = application.startuppath + "\names.txt"     if not my.computer.filesystem.fileexists(filename) throw new exception("filename """ + filename + """ not exist!")      dim filecontent string = my.computer.filesystem.readalltext(filename)     dim lines() string = split(filecontent, vbcrlf)     each ele string in lines         dim line string = ele.trim         dim datepos integer = line.lastindexof(" ") 'find last space between name , date         if datepos < 5 continue 'if full name less 5 chars, not line entry         dim datestring string = mid(line, datepos + 2) 'all after last space date         dim name string = mid(line, 1, datepos).trim ' before last space name         'dim birthday date = convert.todatetime(parts(1).trim) ' used conversion before, lets try other way         dim birthday date         try             birthday = datetime.parseexact(datestring, "m/d/yyyy", system.globalization.cultureinfo.getcultureinfo("en-us"))         catch ex exception             continue         end try         dim month integer = birthday.month         birthdays(month).add(name)     next  end sub 

you need rename birthday filename "names.txt" , place project/bin/debug or anywhere else run program executable.

the exception should find correct path.

update

and make sure both listboxes relative large (can show 12 months).

test program. should see happen when select month 7 or 8 (if use 3 entries listed)

update2

i have modified code adding try..catch block fix exceptions, if file contains empty lines/lines invalid data in them.

just replace code in project.

also please make sure " " space, not tab, used in text file. if tabs used, replace

dim datepos integer = line.lastindexof(" ") 

with

dim datepos integer = line.lastindexof(vbtab) 

good luck time :) if still fails, can make string ate conversion function , test bit more, or more manually getting 3 numbers, , creating new date day, month, , year (each integer)

update3

changed birthday array have 13 fields (0 12, 0 not used anymore)

changed initialize 13 fields

changed read index+1, since january index 0, february index 1 etc.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -