From: Salad on
I'm just getting started. How does one call another button's code? In
Button2's code, I want to call Button1's click event. I guess I need to
pass parameters. The Button1_Click() statement is incorrect. So how
does one call code contained in another button?

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("I pressed a button")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
Button1_Click()
End Sub

I corrected it by using
Button1_Click(sender, e)
in Button2's code. What exactly am I sending as a parameter to
Button1's code?
From: Family Tree Mike on
On 4/15/2010 10:48 AM, Salad wrote:
> I'm just getting started. How does one call another button's code? In
> Button2's code, I want to call Button1's click event. I guess I need to
> pass parameters. The Button1_Click() statement is incorrect. So how does
> one call code contained in another button?
>
> Private Sub Button1_Click(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles Button1.Click
> MsgBox("I pressed a button")
> End Sub
>
> Private Sub Button2_Click(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles Button2.Click
> TextBox1.Text = ""
> Button1_Click()
> End Sub
>
> I corrected it by using
> Button1_Click(sender, e)
> in Button2's code. What exactly am I sending as a parameter to Button1's
> code?

Sender is the button that was clicked. This can be useful if you have
one handler for several controls, and you need to check the control that
was used.

I assume your goal is something more significant, but you could have one
handler for both buttons. Something like this:

Private Sub Buttons_Click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click, Button2.Click

Dim btn As Button = DirectCast(sender, Button)
MessageBox.Show("You pressed button " & btn.Text)

End Sub

The EventArgs parameter can be significant for other controls and
handlers, but in a button click can often be ignored.

--
Mike
From: Mr. Arnold on
Salad wrote:
> I'm just getting started. How does one call another button's code? In
> Button2's code, I want to call Button1's click event. I guess I need to
> pass parameters. The Button1_Click() statement is incorrect. So how
> does one call code contained in another button?
>
> Private Sub Button1_Click(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles Button1.Click
> MsgBox("I pressed a button")
> End Sub
>
> Private Sub Button2_Click(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles Button2.Click
> TextBox1.Text = ""
> Button1_Click()
> End Sub
>
> I corrected it by using
> Button1_Click(sender, e)
> in Button2's code. What exactly am I sending as a parameter to
> Button1's code?

You're sending an object that can be cast back to a 'Button', the sender
object is going to be Button2's object properties I suspect.

I don't recall how to do a cast in VB but here is a C# take on it

var button = (Button)sender;

string buttext = button.text;


If you were doing the cast to a button in Button1_Click normally by
pressing Button1 on the screen, the Button1 is the sender object and you
can address the properties of Button1 when you cast 'sender' to be a
'Button' type.
From: Armin Zingler on
Am 15.04.2010 16:48, schrieb Salad:
> I'm just getting started. How does one call another button's code? In
> Button2's code, I want to call Button1's click event. I guess I need to
> pass parameters. The Button1_Click() statement is incorrect. So how
> does one call code contained in another button?
>
> Private Sub Button1_Click(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles Button1.Click
> MsgBox("I pressed a button")
> End Sub
>
> Private Sub Button2_Click(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles Button2.Click
> TextBox1.Text = ""
> Button1_Click()
> End Sub
>
> I corrected it by using
> Button1_Click(sender, e)
> in Button2's code. What exactly am I sending as a parameter to
> Button1's code?

The msgbox in Button1_Click says you've pressed a button, but to be exact,
it's Button1, which makes the difference in this case. If you change
it to "I pressed Button1", you see why you shouldn't call a click event
handler if nothing has been clicked.

So my answer is:

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("I pressed a button") 'or "Button1" :-)
DoSomething()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
DoSomething()
End Sub

private sub DoSomething
'doing something
end sub

Or, if you really just want to output you've pressed a button:
(note the change with the "Handles" clause)

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click

MsgBox("I pressed a button")

End Sub



--
Armin
From: Rich P on
Greetings (from the Access NG :),

It sounds like you want to call the same function but with different
parameters. If this is the case -- that is called function overloading.
This is a feature of OOP (that is not available in VBA) where you can
declare a function multiple times with the same name but with different
parameters for each declaration. Here is an example:

Private Sub btnOL1_Click(...) Handles btnOL1.Click
Console.WriteLine(OLtest())
End Sub

Private Sub btnOL2_Click(...) Handles btnOL2.Click
Console.WriteLine(OLtest(123))
End Sub

Private Sub btnOL3_Click(...) Handles btnOL3.Click
Console.WriteLine(OLtest(#4/15/2010#))
End Sub

Function OLtest() As String
Return "test1"
End Function

Function OLtest(ByVal x As Integer) As Integer
Return x
End Function

Function OLtest(ByVal d As Date) As Date
Return d
End Function

This is useful for organization of large projects. Basically, VB.Net is
kind of like VBA/VB6 on some major steroids. You still have a lot of
VBA/VB6 features but they have added OOP to the mix. If you are working
in a non-integrated development environment you will get used to this
very quickly.

Just know that in OOP the big features are Inheritance, Overloading, and
Polymorphysm. Note: these features are fairly involved, and will
require some reading to get the full benefit since these features
include other features like Interfaces (how you can communicate between
.Net and com), Base Classes (Inheritance), Abstract/Virtual classes
(polymorphysm) and a ton of other stuff.

HTH

Rich

*** Sent via Developersdex http://www.developersdex.com ***