+ Reply to Thread
Results 1 to 7 of 7

Thread: Help in Visual Basic

  1. #1
    melanie is offline VB.NET Forum Newbie melanie is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0
    Join Date
    Mar 2010
    Posts
    2
    Reputation
    0

    Exclamation Help in Visual Basic

    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!

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    41
    Posts
    6,736
    Reputation
    653

    Default

    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.

  3. #3
    melanie is offline VB.NET Forum Newbie melanie is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0
    Join Date
    Mar 2010
    Posts
    2
    Reputation
    0

    Default

    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!

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    41
    Posts
    6,736
    Reputation
    653

    Default

    Quote Originally Posted by melanie View Post
    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.

  5. #5
    Tom
    Tom is offline VB.NET Forum Idol Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2005
    Posts
    746
    Reputation
    340

    Default

    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.

  6. #6
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Age
    37
    Posts
    10,843
    Reputation
    1443

    Default

    Quote Originally Posted by melanie View Post
    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.

  7. #7
    Solitaire is offline VB.NET Forum Master Solitaire probably authored a book by now Solitaire probably authored a book by now
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2004
    Posts
    257
    Reputation
    111

    Default

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts