Results 1 to 8 of 8

Thread: Need help trying to figure out how to write the code

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    95

    Need help trying to figure out how to write the code

    I hope I'm explaining this correctly. Is there a way to immediately update a user form as the user answer's the form. i.e. Lets say the user form has two text boxes, four radio buttons (marked with labels + - * /) and a label used for the answer. After the user enters a number in textbox1 as soon as they start to type a number in textbox2, depending on which mathematical operation they chose, the label(answer) would change as soon as they type a digit. Hope that makes sense. Thanks for your help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help trying to figure out how to write the code

    Handle the TextChanged event of TextBox2. You can perform the entire calculation and display the result in the Label each time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    95

    Re: Need help trying to figure out how to write the code

    First let me say thank you for your help. Second let me also say I'm knew to Visual Basic. I hate to ask this but would you mind explaining how I would go about doing this? thank you again.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help trying to figure out how to write the code

    You should work your way through a good beginners tutorial, e.g.

    http://www.homeandlearn.co.uk/net/vbnet.html

    Once you've been through that, you should know how to do everything you need to for this problem and more besides.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    95

    Re: Need help trying to figure out how to write the code

    Thank you again for your help. I'll try it out.

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Need help trying to figure out how to write the code

    Just a word of warning, if the user selects "/" and enters a zero as the first character in TextBox2 you're going to have to ignore it otherwise you'll get a 'Division by Zero' error when you attempt the calculation. It may be better to ignore the first character if it's a zero whatever operator the user has chosen.

    Also, if you're going to handle Real Numbers (as opposed to just Integers) you'll have to cater for the decimal point. You'll also have to cater for a sign (e.g. "+" or "-") being entered as the first character.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    95

    Re: Need help trying to figure out how to write the code

    just curious, but which handle for textbox two would I use to achieve this? Could come in handy for other things. Also, are there any drawbacks to doing this?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help trying to figure out how to write the code

    Quote Originally Posted by pooky View Post
    just curious, but which handle for textbox two would I use to achieve this? Could come in handy for other things.
    I assume you mean to ask which event you should handle and, if so, I answered that question in post #2.
    Quote Originally Posted by pooky View Post
    Also, are there any drawbacks to doing this?
    The good thing about doing it that way is that the result is there for the user as soon as they finish typing the value. The bad thing is that you're wasting your time performing the calculation as the user is typing because they aren't interested in those results. There's no way to know for sure that the user has finished entering data unless you make them initiate the calculation, which you'd normally do by using a Button. The calculation and subsequent display is going to be very quick in your case, so it's no big deal. If what you had to do was a bit time-consuming then it would be a bad idea to do it on every change of the Text. In that case what you can do is start/restart a Timer with a fairly small Interval on each TextChanged event and only do whatever it is that you have to do when it Ticks. That means that there's no wasted effort as the user types as long as they don't pause along the way. When they stop typing, their data will be used after a small delay.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width