Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 Subbut 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.Live the life as you know it
/ Wille

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.
Stuart

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.Live the life as you know it
/ Wille

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.
Live the life as you know it
/ Wille

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

Never mind I've found out how it's done but however thanks a million for all help. :D
Live the life as you know it
/ Wille

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |