vb.net - VB - TextChanged not triggering properly after the backspace key is hit? -
my problem boils down this:
i have 6 textboxes expect value between 0 , given number. trying achieve is:
- if number entered between 0 , specified number (as label), text remain black
- if number entered exceeds specified number, text turn red
the problem here if specified number "10", , user enters 11, turns red, should, however, if hit backspace key (the number entered 1) number remains red, not intended functionality - number 1 should black since it's between 0 , specified number.
all of specified numbers hard-coded (i'm in beginner course , i'm doing fun increase functionality of program , haven't gotten adding classes each "assignment" yet) , can technically input negative numbers, don't care @ moment.
this subroutine gets added handler of textboxes within groupbox
' handler gets added textboxes in "grpgrades" groupbox private sub txtgradepoints_textchanged(sender object, e eventargs) ' take in generic sender (textbox) , convert textbox (necessary due strict mode) dim textbox = ctype(sender, textbox) try ' value of current textbox being checked dim val = decimal.parse(textbox.text) select case textbox.name case "txtpostpoints" if val > 10 textbox.forecolor = color.red case "txtch1testpoints", "txtch2testpoints", "txtch3testpoints" if val > 50 textbox.forecolor = color.red case "txtch2tutpoints", "txtch3tutpoints" if val > 25 textbox.forecolor = color.red case else textbox.forecolor = color.black end select catch textbox.forecolor = systemcolors.controltext end try end sub
this onload handler gets appropriate textbox controls "grpgrades" groupbox , adds aforementioned textchanged handler each one.
private sub form1_load(sender object, e eventargs) handles mybase.load ' array of textbox controls "grpgrades" groupbox dim textboxes = grpgrades.controls.oftype(of textbox)() ' go through array of textboxes , add textchanged handler each textchanged event each txt in textboxes addhandler txt.textchanged, addressof txtgradepoints_textchanged next 'addhandler txtpostpoints.textchanged, addressof txtgradepoints_textchanged 'addhandler txtch1testpoints.textchanged, addressof txtgradepoints_textchanged 'addhandler txtch2testpoints.textchanged, addressof txtgradepoints_textchanged 'addhandler txtch3testpoints.textchanged, addressof txtgradepoints_textchanged 'addhandler txtch2tutpoints.textchanged, addressof txtgradepoints_textchanged 'addhandler txtch3tutpoints.textchanged, addressof txtgradepoints_textchanged end sub
the last part of subroutine commented out code , how had handlers added, in case went wrong new method.
edit: necessary downvote? reason?
your code never test valid value. case else sets current textbox black never hit when current textbox returns valid value. cannot happen because select case match current name of textbox, test again invalid value , exits select case block. need set color valid value in appropriate case current text box name. if conditional operator reduce single line
private sub txtgradepoints_textchanged(sender object, e eventargs) ' take in generic sender (textbox) , convert textbox (necessary due strict mode) dim textbox = ctype(sender, textbox) try ' value of current textbox being checked dim val = decimal.parse(textbox.text) select case textbox.name case "txtpostpoints" textbox.forecolor = if(val > 10, color.red, color.black) case "txtch1testpoints", "txtch2testpoints", "txtch3testpoints" textbox.forecolor = if(val > 50, color.red, color.black) case "txtch2tutpoints", "txtch3tutpoints" textbox.forecolor = if(val > 25, color.red, color.black) case else ' not sure if needed other textboxes.... textbox.forecolor = color.black end select catch textbox.forecolor = systemcolors.controltext end try end sub
Comments
Post a Comment