How to Click button by code

  • Thread starter Thread starter Thammarat charoenchai.
  • Start date Start date
T

Thammarat charoenchai.

Hi, I'm try to learn vb.net.

in delphi have .click method for click button by coding.
Can I do that with vb.net

Thank you very much.
 
Hi, I'm try to learn vb.net.

in delphi have .click method for click button by coding.
Can I do that with vb.net

Thank you very much.

myButton.PerformClick()

Thanks,

Seth Rowe
 
Call the method that handles the Click event of the button you want to click
and pass it sender and e.

If the function/sub that you want to call the button click from isn't
already being passed send As Object and e as System.EventArgs then you will
have to Dim/declare out your own.

Dim sender as Object
Dim e as System.EventArgs
btnAdd_Click( sender, e)

'This is the sub/method that gets called when the button is clicked
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click

' Some code goes here

End Sub

'Here is an example that already is being passed sender and e As
System.EventArgs
Private Sub ContextMenuStrip1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ContextMenuStrip1.Click

'Some code

'Click the button
btnAdd_Click( sender, e)
End Sub

Hope that helps,

Chris
 
This works perfectly - but what if you want to actually see the button
visually being pressed?

Cheers,
Johnny J.
 
It's working, but have some problem. This is my code

Private Sub frmProducts_FormClosing(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles
MyBase.FormClosing
Dim ee As System.EventArgs

btnSave_ItemClick(sender, ee)

End Sub


when build project it's show

Warning 1 Variable 'ee' is used before it has been assigned a value. A
null reference exception could result at runtime.
C:\Project\HiStore\HiStore\frmProducts.vb 30 35 HiStore

What does it mean ?

Thank you very much
 
Try

Dim ee As System.EventArgs=New System.EventArgs

/Johnny J.

Or he can just do btnSave_ItemClick(sender EventArgs.Empty) and avoid
the variable declaration.

Thanks,

Seth Rowe
 
This works perfectly - but what if you want to actually see the button
visually being pressed?

Good Question! Perhaps you could do this by passing the appropriate
windows messages to the button via the windows api, but I don't know
if that would work. If you need this behavior you will probably need
to do a userdrawn button and just draw the "pressed" image before you
call the PerformClick method.

Thanks,

Seth Rowe
 
Thammarat charoenchai. said:
It's working, but have some problem. This is my code

Private Sub frmProducts_FormClosing(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles
MyBase.FormClosing
Dim ee As System.EventArgs

btnSave_ItemClick(sender, ee)

=> 'btnSave_ItemClick(sender, EventArgs.Empty)'.
 
Back
Top