vb.net - Random Numbers to Text in Label -
public class mainform private sub exitbutton_click(byval sender object, byval e system.eventargs) handles exitbutton.click me.close() end sub private sub gueststextbox_keypress(byval sender object, byval e system.windows.forms.keypresseventargs) handles gueststextbox.keypress ' allows numbers , backspace key if (e.keychar < "0" orelse e.keychar > "9") andalso e.keychar <> controlchars.back e.handled = true end if end sub private sub mainform_load(byval sender system.object, byval e system.eventargs) handles mybase.load 'fills list box , selects first item typelistbox.items.add("kid's birthday") typelistbox.items.add("21st birthday") typelistbox.items.add("40th birthday") typelistbox.items.add("other birthday") typelistbox.selectedindex = 0 end sub private sub calcbutton_click(byval sender object, byval e system.eventargs) handles calcbutton.click 'displays total charge dim guests integer dim typeindex integer dim guestprice integer dim totalcharge integer integer.tryparse(gueststextbox.text, guests) typeindex = typelistbox.selectedindex 'determine price per guest select case typeindex case 0 'kid's birthday guestprice = 11 case 1 '21st birthday guestprice = 20 case 2 '40th birthday guestprice = 25 case else 'other birthdays guestprice = 15 end select 'calculate , display total charge totalcharge = guests * guestprice totallabel.text = totalcharge.tostring("c0") end sub private sub testdatabutton_click(byval sender object, byval e system.eventargs) handles testdatabutton.click dim guests integer dim typeindex integer dim guestprice integer dim totalcharge integer dim randomgen new random dim setcounter integer = 1 testdatalabel.text = string.empty guests = randomgen.next(1, 51) typeindex = randomgen.next(0, 4) each object in typelistbox.selecteditems testdatalabel.text += i.tostring() & controlchars.newline next 'determine price per guest select case typelistbox.selectedindex case 0 guestprice = 11 case 1 guestprice = 20 case 2 guestprice = 25 case else guestprice = 15 end select 'calculate , display total charge totalcharge = guests * guestprice testdatalabel.text = testdatalabel.text & typeindex.tostring & " " & guests.tostring & " " & totalcharge.tostring("c0") & controlchars.newline setcounter += 1 loop until setcounter > 10 end sub private sub typelistbox_selectedindexchanged(sender system.object, e system.eventargs) handles typelistbox.selectedindexchanged end sub end class
when click on button named "generate test data" generates list of random numbers in label. want these numbers type of birthday instead of number.
0 being "kid's birthday"
1 being "21st birthday"
2 being "40th birthday"
and 3 being "other birthday"
how go doing this?
any appreciated!
if understood question correctly, can declare enum , have dictionary of enum string.
the enum takes care of dealing numbers in code, , rather use human readable constructs. dictionary make sure users see human readable constructs.
please see below code (needs brand new winforms project , listbox called listbox1
on main form):
option strict on public class form1 'declare enum avoid dealing naked numbers in code enum birthdaytypes btkids = 0 bt21st = 1 bt40th = 2 btother = 3 end enum private sub form1_load(sender system.object, e system.eventargs) _ handles mybase.load 'suppose input value number, 'but don't want deal numbers dim typeindex integer = 2 'you can convert number birthdaytypes, 'making input "correct" dim typeindexbt birthdaytypes = convertbirthdayindextotype(typeindex) 'calculation of guest price "human readable" dim guestprice integer = calculateguestprice(typeindexbt) 'you can create dictionary diplaying values dim displaydictionary new dictionary(of birthdaytypes, string) displaydictionary .add(birthdaytypes.btkids, "kid's birthday") .add(birthdaytypes.bt21st, "21st birthday") .add(birthdaytypes.bt40th, "40th birthday") .add(birthdaytypes.btother, "other birthday") end 'here how values listbox listbox1 .datasource = displaydictionary.tolist .valuemember = "key" .displaymember = "value" end 'now listbox displays strings, 'but selectedvalue return object of type birthdaytypes 'you can extract random values above dictionary index, 'and create new list debug.writeline(listbox1.selectedvalue) 'outputs btkids end sub private function calculateguestprice(bt birthdaytypes) integer select case bt case birthdaytypes.btkids : return 11 case birthdaytypes.bt21st : return 20 case birthdaytypes.bt40th : return 25 case birthdaytypes.btother : return 15 end select 'should never here throw new exception("unknown birthday type") end function private function convertbirthdayindextotype(index integer) birthdaytypes if index < 3 return ctype(index, birthdaytypes) return birthdaytypes.btother end function end class
disclaimer: code demo of can done, not meant used complete solution.
Comments
Post a Comment