Home All Groups Group Topic Archive Search About

Simple VB .Net question

Author
20 Nov 2007 5:39 PM
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

Author
20 Nov 2007 5:50 PM
Armin Zingler
Show quote Hide quote
"HardySpicer" <gyansor***@gmail.com> schrieb
> 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
Author
20 Nov 2007 7:32 PM
Terry
How about  ....
Button1.PerformClick
--
Terry


Show quoteHide quote
"HardySpicer" wrote:

> 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
>
Author
20 Nov 2007 7:54 PM
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

Show quoteHide quote
"HardySpicer" <gyansor***@gmail.com> schreef in bericht
news:0dd27053-6185-455c-88be-432d0cba9ca4@d4g2000prg.googlegroups.com...
>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
Author
21 Nov 2007 12:30 PM
Phill W.
HardySpicer wrote:

Show quoteHide quote
> 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.