Results 1 to 19 of 19

Thread: Mathematics

  1. #1

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Mathematics

    Hello there,

    Okay, my school has put me in charge of making a program, for the maths department. So i went out and bought vb6 (as i have been wanting to for a while now). Ive got the GUI and everything, its just i want to know how to do "sums"

    As in, there are three text boxes box, going:

    TB1 + TB2 = TB3

    Now, i need a code or something, which will create a random number in TB1 and TB2. And when the user enters the answer (in TB3), it will display a tick, and "correct" or a cross and "incorrect"

    I would like to achieve the above for multiplication, addition, subtraction and division.

    Many Thanks

    Adam

  2. #2
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: Mathematics

    Create a text box on the form
    Text1.Text=TB1 + TB2
    Create a random number generator
    Private Sub Command1_Click()
    Dim TB1 As Integer: Dim TB2 As Integer
    Randomize Timer
    TB1 = Rnd(0 + 1) * 100
    Text1.Text = TB1
    TB2 = Rnd(0 + 1) * 100
    Text2.Text = TB2
    End Sub

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Mathematics

    Make sure the textboxes for operands are locked since they're auto generated

  4. #4

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Mathematics

    Many thanks! I get an Invalid outside procedure with that. :s Sorry, i am pretty new to VB.

    Also, i will makesure to lock them, thanks for the heads up

  5. #5
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: Mathematics

    Timer, guess game...maybe helpful
    Attached Files Attached Files

  6. #6

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Mathematics

    Ah wow, many thanks! Ive got the randomizing working perfectly

    How would i go about the IF tb1 + tb2 = tb3 it says "output1" if not it says "output2"

  7. #7
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Mathematics

    Quote Originally Posted by adamlonsdale
    Ah wow, many thanks! Ive got the randomizing working perfectly

    How would i go about the IF tb1 + tb2 = tb3 it says "output1" if not it says "output2"
    Assuming you have a TextBox for the output named txtOutput:

    Code:
    txtOutput.Text = Iif(tb1 + tb2 = tb3, "output1", "output2")
    Is that what you mean?

  8. #8

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Mathematics

    Yeah, but would that be able to pop up in a box? And alert. Well i suppose i could show form2 when commandVerify is clicked, and have txtOutput in there, then when they hit "OK" it hides it again?

  9. #9

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Mathematics

    Aha, yes that is working sort of, how do i sort of link to form1? with the
    vb Code:
    1. txtOutput.Text = IIf(TB1 + TB2 = TB3, "output1", "output2")

    Oh, and should the IIF have 2 I's?

  10. #10

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Mathematics

    Anybody? Also, is it possible to make a textbox transparrent? Or show the result in a label?

    Edit: And is it possible to make a textbox clear when clicked?

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Mathematics

    Code:
    lblOutput.Caption = IIf(TB1 + TB2 = TB3, "output1", "output2")
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  12. #12

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Mathematics

    Ah, thanks! I have got it working sort of, when i click "check" it always comes out with label2. Ive attatched my project.
    Attached Files Attached Files
    Last edited by adamlonsdale; Mar 10th, 2007 at 05:33 AM.

  13. #13

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Mathematics

    Okay this is getting confusing, with saving an what not. So ive added my updated project. I have tried many methods, but i still cannot fix my problem. When i click the check button, it says incorrect no matter what the answer is.

    Also here is the code:

    Form 1:

    Code:
    Private Sub Form_Load()
    Dim TB1 As Integer: Dim TB2 As Integer
    Text1.Caption = TB1 + TB2
    Randomize Timer
    TB1 = Rnd(0 + 1) * 100
    Text1.Caption = TB1
    TB2 = Rnd(0 + 1) * 100
    Text2.Caption = TB2
    End Sub
    Private Sub Command1_Click()
    Dim TB1 As Integer: Dim TB2 As Integer
    Randomize Timer
    TB1 = Rnd(0 + 1) * 100
    Text1.Caption = TB1
    TB2 = Rnd(0 + 1) * 100
    Text2.Caption = TB2
    End Sub
    Private Sub Command2_Click()
    Form2.Show
    End Sub
    Form 2:

    Code:
    Private Sub Form_Load()
    Dim form1 As New form1
    lblOutput.Caption = IIf(form1.Text1 + form1.Text2 = form1.Text2, "Correct", "Incorrect")
    End Sub
    Private Sub Command1_Click()
    Form2.Hide
    End Sub
    Attached Files Attached Files
    Last edited by adamlonsdale; Mar 10th, 2007 at 06:38 AM.

  14. #14
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Mathematics

    string + string results in stringstring and not the sum of the numbers in string.

    Val(textbox.text) + Val(textbox.Text) results in their sum

  15. #15

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Mathematics

    pardon? Sorry, i do not understand

    edit: ive got:
    Code:
    lblOutput.Caption = IIf(Val(form1.Text1.Caption) + Val(form1.Text2.Caption) = Val(form1.Text3.Text), "Correct", "Incorrect")
    edit: just so you know, the numbers are generated in labels

    label1 is called Text1
    label2 is called Text2
    Last edited by adamlonsdale; Mar 10th, 2007 at 07:14 AM.

  16. #16
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Mathematics

    form1.Text1 + form1.Text2 does concatenation or
    "12" + "34" results in "1234"

    correct answer is derived with
    val("12") + val("34") results in 46, to check txtanswer.text then

    If Val(txtAnswer.Text) = val(txtValueA.Text) + val(txtValueB.Text) Then
    msgbox "Correct"
    Else
    msgbox "wrong"
    End IF

  17. #17
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Mathematics

    How about using a ScriptControl?
    Code:
    Debug.Print ScriptControl1.Eval("12+34") 'returns 46

  18. #18

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Mathematics

    Oh my god you are a genius!! Thank you so much! I have set it to display in a label in form 2, and it is working like a charm

    One last question, then im done. How do i

    a) close a form (.net was close())
    b) when form1 is closed, it closes all other forms

    Thanks

  19. #19
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Mathematics

    in form1 unload event

    Code:
    Dim oFrm As Form
    
       For Each oFrm in Forms
          Unload oFrm
          Set oFrm = Nothing
       Next

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