Simple VB .Net question


H

HardySpicer

I have the following call from a Button1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strMp3 As String

strMp3 = "chop2.mp3"

Me.AxWindowsMediaPlayer1.URL = strMp3
End Sub
This works fine as part of a larger program and the Mp3 file plays.

However, if I want to automatically push the button in the code how do
I do this?

I tried

Button1_click ()

but it complains tyhat a declaration is expected? How do I do this...

Also tried
Button1.Enabled=True

didn't work either. Also had a look here

http://msdn2.microsoft.com/en-us/library/system.windows.forms.button.performclick.aspx

where it says

Visual Basic (Declaration)

Public Sub PerformClick

Visual Basic (Usage)

Dim instance As Button

instance.PerformClick

but I find this confusing.

regards

Hardy
 
Ad

Advertisements

A

Armin Zingler

HardySpicer said:
I have the following call from a Button1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strMp3 As String

strMp3 = "chop2.mp3"

Me.AxWindowsMediaPlayer1.URL = strMp3
End Sub
This works fine as part of a larger program and the Mp3 file plays.

However, if I want to automatically push the button in the code how
do I do this?
Call Sub Play whenever you want:

Public sub Play

Dim strMp3 As String

strMp3 = "chop2.mp3"

Me.AxWindowsMediaPlayer1.URL = strMp3

End Sub

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

Play

End Sub



Armin
 
C

Cor Ligthert[MVP]

Hardy,

There are a lot of answers on your question, my favorite is

Button1_Click(me,nothing)

(You don't use the sender object or the eventArgs, so there is not any
problem)

Cor
 
Ad

Advertisements

P

Phill W.

HardySpicer said:
I have the following call from a Button1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strMp3 As String

strMp3 = "chop2.mp3"

Me.AxWindowsMediaPlayer1.URL = strMp3
End Sub
This works fine as part of a larger program and the Mp3 file plays.

However, if I want to automatically push the button in the code how do
I do this?
Don't.

You /could/ use ...

Me.Button1_Click( Nothing, EventArgs.Empty )

.... but then you'd have to worry about null references in the click
handling routine that you wouldn't do normally and, while this approach
does work quite easily with, say, clicking a button, trying to invoke
/other/ Control events this way - like the Mouse* ones - is horribly
fiddly.


You /could/ use ...

Me.Button1.PerformClick()

.... which simulates the user clicking the button *but* it is subject to
the same limitations as the user would be; the call /only/ succeeds if
the button is both Visible and Enabled, which it might not be and, if it
/doesn't/ work, VB doesn't /tell/ you that it didn't work.


The best solution is to extract the logic into a common routine and call
it from anywhere that you need to [re-]use it.

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

Me.Play( "chop2.mp3" )

End Sub

Private Sub Play( ByVal sFilename as String )

Me.AxWindowsMediaPlayer1.URL = sFilename

End Sub

Private Sub SomeOtherRoutineThatNeedsToPlayStuff()

Me.Play( "stuff.mp3" )

End Sub

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top