My first program needs bigtime help!
I am having trouble with my first program. I have two input boxes for numeric entry only. When the button is clicked it should divide the second text box by 2 and add that number to the first text box. The result should be posted in the third text box on my form. I am sooo stuck. Please try not to laugh at my inabilities! ~L~
Public
Class SphereEquivPrivateSub Calculate_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Calculate.ClickDim sphereAsStringDim cylinderAsStringDim equivAsDecimalDim show_boxAsBoolean =FalseDim equiv2AsDecimalsphere = textbox2.Text
cylinder = textbox3.Text
TextBox4.Text = 0
DoUntil show_box =TrueIf IsNumeric(sphere) =FalseThenshow_box =
TrueMsgBox(
"Please enter a correct number for spherical power!")EndIfLoopshow_box =
FalseDoUntil show_box =TrueIf IsNumeric(cylinder) =FalseThenshow_box =
TrueMsgBox(
"Please enter a correct number for cylinderical power!")EndIfLoopIf show_box =TrueThenequiv = Val(textbox3.Text)
equiv2 = Val(textbox2.Text)
TextBox4.Text = ((equiv / 2) + equiv2)
EndIfEndSubEnd
Class
My first advice (even though it wouldn't help you here) is to set Option Strict On. This will help you find many simple mistakes that can be really tricky to pin-point otherwise (yeah, I know that there are die-hard VB coder out there that think that Option Strict:ness is for wimps - I guess I'm one of those wimps

)
The next piece of advice is to look at what is happening here. I would assume that you have typed valid numerical values in textboxes 2 and 3 and clicked on the Calculate button, and nothing happens, right?
Let's take a look at what's happening here (some steps omitted).
- The user clicks on the Calculate button.
- You put the text of textbox2 into the sphere variable.
- You create a boolean variable show_box and assign false to it.
- Then you loop until the show_box variable is set to true. Inside the loop you check if this is a numerical value. But when do you ever set the show_box to true? Unless you set it to true, you are never going to exit this loop, right? Which means that your program will "hang"
When trying this, I simply ran the application and realized that it was hanging somewhere (I must admit that I didn't really try to read the code more than to make sure that there wasn't anything that would be harmful to my computer before running it)
Next, I pressed Ctrl+Break to break into the debugger to see where the program was hanging. Looking at the loop where the debugger told me I was executing, I could spot the problem...
I hope that this helps you get a bit further...
Best regards,
Johan Stenberg
Not saying this is ideal but this does what you say you want.
There is no need to do the loops for validation. If when you processing this calculation the values are not numeric and you can determine that with a simple message display the message and exit out of the sub as you can't process anything.
With option strict on you need to ensure that the data types are correct and hence there are a number of ctypes statements to ensure this is correct.
The organization of the code is broken down into
Validation
Calculation
Display
These are all wrapped up within a try ... catch block to capture any unanticipated errors.
Option
Explicit On
Option Strict On
Public Class Form1Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
TextBox3.Text = "" '//Blank Initial Result textbox
'//Display Error if TextBox1 is not numeric
If Not IsNumeric(TextBox1.Text) Then
MsgBox("Please enter a correct number for spherical power!")
Exit Sub
End If '//Display Error if TextBox2 is not numeric
If Not IsNumeric(TextBox2.Text) Then
MsgBox("Please enter a correct number for cylinderical power!")
Exit Sub
End If '//Get the values converting to a decimal type - Is this really the type you want
Dim equiv As Decimal = CType(TextBox2.Text, Decimal)
Dim equiv2 As Decimal = CType(TextBox1.Text, Decimal) '//Calculate a result and display it in Textbox3
TextBox3.Text = CType((equiv / 2) + equiv2, String) Catch ex As Exception
'//Catch any errors
MsgBox(ex.Message)
End Try End Sub End
Class