melanie

New member
Joined
Mar 19, 2010
Messages
2
Programming Experience
Beginner
My friend and I are taking a visual basic online class. We are total beginners and are having trouble with a programming assignment. We have to create a arithmetic calculator with an enter button multiplication button and an addition button.
What we can't seem to code properly is the event handler that allows us to use the enter button when we type in an integer in the textbox.

Would anyone be able to help us out? This assignment is detrimental to our grade!
 
First up, please provide meaningful titles for your threads. Everyone who posts here wants help with Visual Basic so that tells us nothing.

As for the question, you would handle the KeyDown event of your TextBox and test the e.KeyCode property to see if it was Keys.Enter.
 
I am sorry, I am new at this. I will try to be more specific next time.

We don't know what the KeyDown event is...that isn't for another 3 or 4 chapters in our textbook.

Very sorry for our amateurism!
 
We don't know what the KeyDown event is...that isn't for another 3 or 4 chapters in our textbook.
That would explain why you didn't think of it in the first place. I don't really see another way to do it though, so I suggest that you read ahead. Maybe this assignment isn't due until you will have covered that part. Seems odd otherwise.

Anyway, you can access all events from the two drop-down lists at the top of the code window. Select the object on the left and then the event on the right. As the name suggests, KeyUp is the event that's raised when a key on the keyboard is released while that control has focus. It allows you to execute code when a key is released, which is what you want.

As with all events, the event handler (i.e. the method that executes when the event is raised) has two parameters: 'sender' and 'e'. The sender is the object that raised the event; in this case, your TextBox. The 'e' contains data for the event. In this case 'e' is type KeyEventArgs, which has a KeyCode property that contains the code of the key that was released.

In this way, you can detect when the Enter key is released while your TextBox has focus and respond accordingly.
 
I'm thinking they mean enter, as in calculation total in a command button not so much as a key event in a text box.
 
We have to create a arithmetic calculator with an enter button multiplication button and an addition button.
What we can't seem to code properly is the event handler that allows us to use the enter button when we type in an integer in the textbox.
You don't need any code for this. In Designer select the form and in Properties window assign the enter/calculate button to the AcceptButton property. You select the button in the dropdown list for that property. Form.AcceptButton Property (System.Windows.Forms)

This will also work for Multiline textboxes, but when AcceptButton is set you have to Ctrl+Enter in textbox to force a linefeed.
 
The way I see it, you start with two textboxes for the user to enter two numbers. You need to use a form-level variable for the total.

When the + or * button is pressed, it adds or multiplies the two numbers and saves the result to the form-level total.

The Enter key is the same as the = key. When it's pressed, it takes the form-level total and displays the result in a label.

Your requirements may be different, but you didn't describe the problem adequately.
 

Latest posts

Back
Top