click events, help with code


Hi,
Here is some code that i am working on. I wanted to know if i can have one event but have a radio button event and a toolmenu event with the same code. So if the menu is used it will use the same code that the radio button uses. Maybe use a IF statement? May a better way to write this? Any advise would be greatly appreicated.
Thanks for the help,
Pat



Function first:

Public Class Form1
Private Function RightTriangleArea(ByVal height As Decimal, ByVal base As Decimal) As Decimal
Return ((height * base) / 2)
End Function

Now the Radio button code:

Private Sub areaTriRadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles areaTriRadioButton1.CheckedChanged
Dim height As Decimal
Dim base As Decimal
Dim areatriangle As Decimal

With Me
Try
height = Decimal.Parse(.heightTextBox1.Text)
base = Decimal.Parse(.baseTextBox2.Text)

areatriangle = RightTriangleArea(height, base)


.areaTriTextBox5.Text = areatriangle.ToString("N")

Catch ex As Exception
MessageBox.Show("Amount needs to be numberic.", _
"Data entry error", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
With .heightTextBox1
.Clear()
.Focus()
End With

End Try
End With
End Sub



And last the ToolMenu:

Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs ) Handles MenuStrip1.ItemClicked
Dim height As Decimal
Dim base As Decimal
Dim areatriangle As Decimal

With Me
Try
height = Decimal.Parse(.heightTextBox1.Text)
base = Decimal.Parse(.baseTextBox2.Text)

areatriangle = (height * base) / 2

.areaTriTextBox5.Text = areatriangle.ToString("N")

Catch ex As Exception
MessageBox.Show("Amount needs to be numberic.", _
"Data entry error", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
With .heightTextBox1
.Clear()
.Focus()
End With

End Try
End With
End Sub

Comments

  • :
    : Hi,
    : Here is some code that i am working on. I wanted to know if i can
    : have one event but have a radio button event and a toolmenu event
    : with the same code. So if the menu is used it will use the same code
    : that the radio button uses. Maybe use a IF statement? May a better
    : way to write this? Any advise would be greatly appreicated.
    : Thanks for the help,
    : Pat
    :

    Sometimes it's possible, when they have the same arguments. But in this case they don't. What one often does in such a situation is create a Sub to do the shared code and then call it from both events.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion