vb.net - Visual Basic Error Message -
i'm new visual basic realize question remedial... when try run program using following code following error message:
an unhandled exception of type 'system.invalidcastexception' occurred in microsoft.visualbasic.dll
additional information: conversion string "" type 'double' not valid.
i realize similar questions have been asked want display code folowing:
private sub txtgrade1_textchanged(sender object, e eventargs) handles txtgrade1.textchanged dim numgrade decimal numgrade = cdec(txtgrade1.text * 0.15) + cdec(txtgrade2.text * 0.25) + cdec(txtgrade3.text * 0.2) + cdec(txtgrade4.text * 0.4) msgbox("your grade is" + numgrade) end sub
i have no idea going on here. program allow me put in 1 digit in text box when runs error message
well, solve type mismatches. may not want show message box every time txtgrade1.text
changes...
private sub txtgrade_textchanged(sender object, e eventargs) handles txtgrade1.textchanged dim grade1, grade2, grade3, grade4 double if double.tryparse(txtgrade1.text, grade1) _ andalso double.tryparse(txtgrade2.text, grade2) _ andalso double.tryparse(txtgrade3.text, grade3) _ andalso double.tryparse(txtgrade4.text, grade4) dim grade = grade1 * 0.15 + grade2 * 0.25 + grade3 * 0.2 + grade4 * 0.4 msgbox(string.format("your grade {0}", grade)) end if end sub
to improve upon code, make happen when of textboxes
validated instead of changed, since if typing 99, change event fires after first 9 typed. validated handler fires when move out of textbox
.
private sub txtgradesvalidated(sender object, e eventargs) _ handles txtgrade1.validated, txtgrade2.validated, txtgrade3.validated, txtgrade4.validated dim grade1, grade2, grade3, grade4 double if double.tryparse(txtgrade1.text, grade1) _ andalso double.tryparse(txtgrade2.text, grade2) _ andalso double.tryparse(txtgrade3.text, grade3) _ andalso double.tryparse(txtgrade4.text, grade4) dim grade = grade1 * 0.15 + grade2 * 0.25 + grade3 * 0.2 + grade4 * 0.4 msgbox(string.format("your grade {0}", grade)) end if end sub
still not ideal, see if test it. consider keypress or keyup , perform logic on enter press changes logic , design of program it's you.
Comments
Post a Comment