Jump to content

Problem with my areas program [SOLVED]

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Bondi

Bondi

    Newbie

  • Members
  • Pip
  • 6 posts
I am trying to work out the area of a circle but when I type in the radius to calculate the area it comes up "Enter a Number!", which I only want to show if you type in text or leave the box blank.

Here is my code:

Public Class CircleForm


    Private Sub btnCircleCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCircleCalculate.Click

        Dim radius As Single

        Dim area As Double

        Try

            If txtRadius.Text <> "" And Char.IsNumber(txtRadius.Text) = True Then

                radius = Single.Parse(txtRadius.Text)

                area = Math.PI * radius

                lblCircleAnswer.Text = (" Enter a Number!")

            End If

        Catch ex As Exception

            MessageBox.Show("Not a Number")

        End Try

    End Sub


    Private Sub btnCircleExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCircleExit.Click

        Me.Close()

    End Sub


    Private Sub btnCircleMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCircleMenu.Click

        Dim MainForm As New MainForm

        Me.Hide()

        MainForm.ShowDialog()

    End Sub


    Private Sub btnCircleHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCircleHelp.Click

        Dim help As New Help

        help.ShowDialog()

    End Sub

End Class


Thanks

- Bondi

Edited by Bondi, 10 January 2011 - 07:59 AM.


#2
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
Then remove the line lblCircleAnswer.Text = (" Enter a Number!") as marked by (***) in the following code sample.


Public Class CircleForm


    Private Sub btnCircleCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCircleCalculate.Click

        Dim radius As Single

        Dim area As Double

        Try

            If txtRadius.Text <> "" And Char.IsNumber(txtRadius.Text) = True Then

                radius = Single.Parse(txtRadius.Text)

                area = Math.PI * radius

                lblCircleAnswer.Text = (" Enter a Number!") (***)

            End If

        Catch ex As Exception

            MessageBox.Show("Not a Number")

        End Try

    End Sub



#3
Bondi

Bondi

    Newbie

  • Members
  • Pip
  • 6 posts
When you do that though it just comes up blank, its something wrong with the actual formula to work out the area which I cannot work out. I am new to Visual Basic and this is one of my first programs.


- Bondi

#4
Bondi

Bondi

    Newbie

  • Members
  • Pip
  • 6 posts
I also tried adding the code below however it is giving inaccurate results.

lblCircleAnswer.Text = area


- Bondi


EDIT:

It works out that you have to square the radius for it to work, thanks for your help!

#5
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts

Bondi said:

When you do that though it just comes up blank

At least now you don't have the "Enter a Number!" problem. Lol! Seriously, you should assign the calculation result to lblCircleAnswer.Text instead of make it to show "Enter a Number!" constant.

Quote

I also tried adding the code below however it is giving inaccurate results.

Did you mean the area calculation was wrong or lblCircleAnswer did not the correct text (e.g. area was 2, while lblCircleAnswer showed 3)?

Anyway, I believe the equation to calculate the area of a circle is: Area = Phi x radius x radius. So change this line:

area = Math.PI * radius

into

area = Math.PI * radius * radius