Call Statement Difficulty???

  • Thread starter Thread starter Radith Silva
  • Start date Start date
R

Radith Silva

Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdCalculate.Click
If txtName.Text <> "" Then
If txtUnits.Text <> "" Then
If optOne.Checked Or optTwo.Checked Or optThree.Checked
Or optFour.Checked Then
'Data fine
Else
MessageBox.Show("Please choos an option button")
End If
Else
MessageBox.Show("txtUnits")
txtUnits.Focus()
End If
Else
MessageBox.Show("txtName")
txtName.Focus()
End If
End Sub

Private Sub cmdO_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdO.Click
Call cmdCalculate_Click()
End Sub

I have the two functions as stated above; now the call statement returns
the error:
C:\vbNet\beginning\nested_if\Form1.vb(141): Argument not specified for
parameter 'e' of 'Private Sub cmdCalculate_Click(sender As Object, e As
System.EventArgs)'.

PLS HELP

THANX
 
Radith said:
Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdCalculate.Click

Call cmdCalculate_Click()

I have the two functions as stated above; now the call statement returns
the error:
C:\vbNet\beginning\nested_if\Form1.vb(141): Argument not specified for
parameter 'e' of 'Private Sub cmdCalculate_Click(sender As Object, e As
System.EventArgs)'.

yea well, you omitted both parameters for cmdCalculate_Click. Obviosuly,
that is wrong.
Try

\\\
cmdCalculate_Click(Nothing, Nothing)
///

btw: why do you write "Call"?
 
Radith Silva said:
Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdCalculate.Click
If txtName.Text <> "" Then
If txtUnits.Text <> "" Then
If optOne.Checked Or optTwo.Checked Or
optThree.Checked
Or optFour.Checked Then
'Data fine
Else
MessageBox.Show("Please choos an option
button")
End If
Else
MessageBox.Show("txtUnits")
txtUnits.Focus()
End If
Else
MessageBox.Show("txtName")
txtName.Focus()
End If
End Sub

Private Sub cmdO_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdO.Click
Call cmdCalculate_Click()
End Sub

I have the two functions as stated above; now the call statement
returns the error:
C:\vbNet\beginning\nested_if\Form1.vb(141): Argument not specified
for parameter 'e' of 'Private Sub cmdCalculate_Click(sender As
Object, e As System.EventArgs)'.


Better approach (IMO):

private sub Calculate()
If txtName.Text <> "" Then
If txtUnits.Text <> "" Then
If optOne.Checked Or optTwo.Checked Or optThree.Checked Or
optFour.Checked Then
'Data fine
Else
MessageBox.Show("Please choos an option button")
End If
Else
MessageBox.Show("txtUnits")
txtUnits.Focus()
End If
Else
MessageBox.Show("txtName")
txtName.Focus()
End If
end sub

Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdCalculate.Click
Calculate()
end sub

Private Sub cmdO_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles cmdO.Click
Calculate
End Sub


- OR -

Delete Sub cmdO_Click and have Sub cmdCalculate_Click handle the click of
both controls:

Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdCalculate.Click, cmdO.Click

Notice the 2nd event after "Handles".


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Radith Silva said:
Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdCalculate.Click
If txtName.Text <> "" Then
If txtUnits.Text <> "" Then
If optOne.Checked Or optTwo.Checked Or
optThree.Checked
Or optFour.Checked Then
'Data fine
Else
MessageBox.Show("Please choos an option
button")
End If
Else
MessageBox.Show("txtUnits")
txtUnits.Focus()
End If
Else
MessageBox.Show("txtName")
txtName.Focus()
End If
End Sub

Private Sub cmdO_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdO.Click
Call cmdCalculate_Click()
End Sub

I have the two functions as stated above; now the call statement
returns the error:
C:\vbNet\beginning\nested_if\Form1.vb(141): Argument not specified
for parameter 'e' of 'Private Sub cmdCalculate_Click(sender As
Object, e As System.EventArgs)'.


Better approach (IMO):

private sub Calculate()
If txtName.Text <> "" Then
If txtUnits.Text <> "" Then
If optOne.Checked Or optTwo.Checked Or optThree.Checked Or
optFour.Checked Then
'Data fine
Else
MessageBox.Show("Please choos an option button")
End If
Else
MessageBox.Show("txtUnits")
txtUnits.Focus()
End If
Else
MessageBox.Show("txtName")
txtName.Focus()
End If
end sub

Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdCalculate.Click
Calculate()
end sub

Private Sub cmdO_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles cmdO.Click
Calculate
End Sub


- OR -

Delete Sub cmdO_Click and have Sub cmdCalculate_Click handle the click of
both controls:

Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdCalculate.Click, cmdO.Click

Notice the 2nd event after "Handles".


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Radith,
In addition to the other's comments, you can also use Button.PerformClick.
Private Sub cmdO_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdO.Click
cmdCalculate.PerformClick()

End Sub

This way if you have multiple handlers for a given button, then all the
handlers will be invoked.

Although I prefer to use Armin's first example. I normally have a single
handler both handle the same event.

Remember VB.NET event model is not your VB6 event model! You can have
multiple handlers for a given event, plus a given event handler can handle
events from multiple objects.

Notice in the following "Button_Click" handles the click event for all three
buttons, while each button also has their own specific handler. This would
be handy if you have some common logic that needed to occur for every click,
plus some specific logic for every click. Or the buttons occur in the base
form and the base form wants to handle the events, plus the derived form
wants to handle the events...

Private WithEvents Button1 As Button
Private WithEvents Button2 As Button
Private WithEvents Button3 As Button
Private Sub Button_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button3.Click
End Sub

Hope this helps
Jay
 
* "Konrad L. M. Rudolph said:
yea well, you omitted both parameters for
cmdCalculate_Click. Obviosuly, that is wrong.

Try

\\\
cmdCalculate_Click(Nothing, Nothing)
///

btw: why do you write "Call"?

I would call the button's 'PerformClick' method. Instead of passing
'Nothing', you should pass a reference to the button in the 1st
parameter and 'EventArgs.Empty' in the 2nd parameter.
 
Hi Radith,

A lot of answers mostly on click events, I give another approach however
more with the point to show you the purpose of the OrElse operator.

I also give another method for that click event (Keep in mind there is no
best and there are much more methods).

Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdCalculate.Click
If txtName.Text <> "" Then
If txtUnits.Text <> "" Then
If optOne.Checked OrElse optTwo.Checked OrElse
optThree.Checked

The OrElse stops evaluating when the condition is True.

OrElse optFour.Checked Then
'Data fine
Else
MessageBox.Show("Please choos an option button")
End If
Else
MessageBox.Show("txtUnits")
txtUnits.Focus()
End If
Else
MessageBox.Show("txtName")
txtName.Focus()
End If
End Sub

Private Sub cmdO_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdO.Click
cmdCalculate_Click(sender, e) 'Only when the signatures are equal
as in this case
End Sub
 
Back
Top