Monday, May 24, 2010

How can I check two letters pressed on text box using Visual Basic?

I'm working on Visual Basic , I went to check two characters pressed on a text box, I use the following code


switch case keyascii


case asc("A")


case asc("B")


Label1.caption="AB are pressed"


case asc("C")


case asc("B)


Label1.caption="BC Pressed"


end select


But It can't work only it displays AB are pressed. I think it only checks the asc("B"), PLs help me with this

How can I check two letters pressed on text box using Visual Basic?
There are essentially problems in your code :


I think you worked with C-Based Languages before .


In VB,after each "case" block that returns true,program jumps to "End Select" against C-Based Langs.


"Switch" must be replaced by "SELECT"


AND A VERY IMPORTANT NOTE : In Each Call Event of KEYs(for ex.keypress,...) , only 1 main key(but ALT,SHIFT,CONTROL) is passed to event function . So you must save first 'keypress' keyascii and then check the second :


dim prevKey as integer


prevKey=-1;


Private Sub Form1_KeyPress(KeyAscii As Integer)


if prevKey=-1 then


if keyAscii=asc("A") OR keyAscii=asc("B") OR _


keyAscii=asc("C") then


prevKey=keyascii


end if


else


keySum=prevKey+keyAscii


select case keySum


case asc("A")+asc("B")


Label1.caption="AB are pressed"


case asc("B")+asc("C")


Label1.caption="BC are pressed"


end select


prevKey=-1


end if


End Sub


No comments:

Post a Comment