Name: wille Date: May 3, 2008 at 08:05:02 Pacific Subject: vb9 keypress OS: WINDOWS XP Home Edition CPU/Ram: 3.2GHz
Comment:
Hi, I'm having some trouble with this program. I want a function to execute when a textbox contains certain text and the user presses the return key. I tried the keypress function but then the function that I wanna execute on keypress return is executed by whatever key I press.
Like this: say I write "exit" in the textbox and then press enter. then the code would be: Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If TextBox1.Text = "exit" Then End End Sub
but the program will end even if I press some other key like if I write "exits" the program'll check when the textbox contains "exit" on keypress "s" and then it will execute then command End and close. Any help will be appreciated.
You need to be looking at what calls the function and what happens when the function returns.
You should never end a VB application with the End statement, it will cause all kinds of problems which this seems to be one of them. End is a throwback to DOS Basic and should only be used in conjunction with Stop as a debugging tool.
The correct way to terminate a VB application is to close all active forms then the application will terminate. That way you can ensure that all open files are properly closed and you don't end up with corrupted data and bits of the application being left in memory. End does just that, kills the application stone dead without any cleanup operations.
OK thanks alot for the end advice. Just, how do I check what calls the function? I meen, I do know since I press the button that executes the function but the program doesn't. Is there a variable for the key that executed the function? Because that would be great. Thanks again.
if I write "exits" the program'll check when the textbox contains "exit" on key press "s" and then it will execute then command End and close. I'm not seeing an issue here; you want to close the application when the textBox = "exit", and your scenario does indeed fulfill your test. How is this not the behavior you want?
Is there a variable for the key that executed the function? Boilerplate code exists for a reason. I'd check the documentation on TextBox.KeyPress(Object, KeyPressEventArgs).
that isn't the behavior becaus if I want to type exits or exit and somthing avther that then it'll close. however I'll check that site thanks very much.
that isn't the behavior becaus if I want to type exits or exit and somthing avther that then it'll close. Then I suggest you use a different event. Possibly TextBox.Leave
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE