[SOLVED] Visual Basic Help Please

This is a discussion on "[SOLVED] Visual Basic Help Please" within the Scripts and Online Services section. This forum, and the thread "[SOLVED] Visual Basic Help Please are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Scripts and Online Services

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 16th, 2007, 16:17
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
[SOLVED] Visual Basic Help Please

Not sure if this is the correct place for this-sorry if not.

I have just started learning visual basic to try and add to my coding knowledge. I've been working on a simple calculator application that could be used in schools for young children. Basically, it asks the kids to enter 2 numbers into 2 seperate boxes. They then have to predict what the answer will be if the numbers are added/multiplied/subtracted/divided. They then click a button. There are 4 buttons (Add, multiply, divide, subtract) and they click one. The correct answer is displayed. That all works. I then tried to add a msgbox that would appear telling them whether they guessed right or not. This is where my problem lies. I have managed to right all the code, but I am getting an error 'End of Statement Expected) on the 'End If' text.

Code: Select all
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbAdd.Click
Label4.Text = " " + Str(Val(Text1.Text) + Val(Text2.Text))
If (guess.Text = Label4.Text) Then MsgBox("Well done, you guessed right") Else MsgBox("Unlucky, you guessed wrong")EndIf
 
EndSub
many thanks for your help.

Jack
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)

  #2 (permalink)  
Old Sep 16th, 2007, 16:56
Rob's Avatar
Rob Rob is offline
Head Admin & CEO

SuperMember
Join Date: Jul 2003
Location: at my desk
Age: 34
Posts: 2,952
Blog Entries: 7
Thanks: 7
Thanked 4 Times in 4 Posts
Send a message via MSN to Rob Send a message via Skype™ to Rob
Re: Visual Basic Help Please

To be honest, I think the compiler is confused.

I think if you space out the code, it will be fine. As a matter of coding practice, you should always use indentation on if else end if statements so you can clearly see whats going on. Always try to have lines of code that are not too long to aid readability. In the case of visual basic, the code is compiled so it doesnt matter how spaced out your source code is... make it as pretty as you like, the executable will always be the same size.

here is an amended code block:-
Code: Select all
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbAdd.Click

    Label4.Text = " " + Str(Val(Text1.Text) + Val(Text2.Text))
    If (guess.Text = Label4.Text) Then
        MsgBox("Well done, you guessed right")
    Else 
        MsgBox("Unlucky, you guessed wrong")
    End If
End Sub
As a sidse note, please rememeber that syntax is crucial. Privatesub, endif and endsub are NOT valid syntax.

Private sub, end if, end sub..... those are valid - note the spacing!

Good luck!
__________________
Rob - SEO Specialist
Owner & Founder of Webforumz.com

I am currently unavailable for private work

Last edited by Rob; Sep 16th, 2007 at 17:44.
  #3 (permalink)  
Old Sep 16th, 2007, 17:12
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Visual Basic Help Please

Thank you! That got rid of the error. However, it then wasn't giving the right message box-it would always give an incorrect answer. For those who would like to know, here is the correct code:

Code: Select all

Label4.Text = Val(Text1.Text) + Val(Text2.Text)
If (guess.Text = Label4.Text) Then
MsgBox("Well done, you guessed right")
Else
MsgBox("Unlucky, you guessed wrong")
EndIf

Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Closed Thread

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Basic database to call information hausjellp PHP Forum 30 May 1st, 2008 09:29
[SOLVED] Basic Positioning Issue Fearpig Web Page Design 2 Apr 15th, 2008 12:30
Web Developer - Visual Basic PHP MySQL Web JobBot Job Opportunities 0 Feb 1st, 2007 11:20
Visual Basic Help Needed DJB MASTER Other Programming Languages 0 Nov 16th, 2006 12:47


All times are GMT. The time now is 19:47.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43