Results 1 to 17 of 17

Thread: Can I Call a Class from my main form?

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    13

    Can I Call a Class from my main form?

    Hi,

    First of, I'm a beginner, so this is sloppy. My apologizes in advance.

    As of the moment I have 24 buttons that are keys to a piano.

    24 of the button codes have a call method that calls the note to be played.

    24 of the private subs that are called have an .wav audio playback command within them.

    I was hoping I could move the .wav private subs into a separate class, and just call them from my main form, therefore cleaning up the lines of code I have at the moment.

    Here's a break down:
    -----------------
    Button Code
    ----------------
    Select Case True
    Case sender Is cButton
    Call C()
    End Select

    ---------------
    Private Sub Code
    ---------------
    Private Sub C()
    My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
    End Sub

    ----------------

    That example only counts for one Button "The Key of C", and the audio file that is called with it. Multiply that by 24 and you get a big mess of repetitive junk.

    Can I move the audio subs into a class and call them from my main form?

    Thanks!

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Can I Call a Class from my main form?

    Wouldn't it be easier to just have one single play sub and pass in the name of the resource to play?
    Code:
    Private Sub PlaySound(resourceName As String)
      My.Computer.Audio.Play(My.Resources.ResourceManager.GetStream(resourceName), AudioPlayMode.Background)
    End Sub
    Now you could for example set the Tag property of each button to the name of the resource to play. In your example set the Tag property of the cButton to "C" and in the Click event simply call the PlaySound like this:
    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) 
        PlaySound(CType(sender, Button).Tag.ToString)
      End Sub
    Now use the above event handler for all your buttons and you're done.

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Can I Call a Class from my main form?

    Another idea would be to have the resource names the same as the button names without the word "Button" in them.
    For example, I have two buttons and two resources.

    Button names:
    cButton
    c_sharpButton

    Audio Resource names:
    c
    c_sharp

    Notice this one button Handles cButton.Click and c_sharpButton.Click, and any other button.clicks I add to it.

    Code:
    Private Sub cButton_Click(sender As System.Object, e As System.EventArgs) _
    	    Handles cButton.Click, c_sharpButton.Click
    
        ' get name of this button and remove "Button" from the name
        Dim keyname As String = DirectCast(sender, Button).Name
        keyname = keyname.Replace("Button", "")
        ' use the keyname to get the resource
        Dim mysound = My.Resources.ResourceManager.GetStream(keyname)
        ' play it
        My.Computer.Audio.Play(mysound, AudioPlayMode.Background)
    End Sub

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Can I Call a Class from my main form?

    Quote Originally Posted by Edgemeal View Post
    Another idea would be to have the resource names the same as the button names without the word "Button" in them.
    It's pretty much the same idea. The main goal here is to get the name of the resource file from the button somehow. If the Text property of the buttons already shows the names you should of course use that.

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Can I Call a Class from my main form?

    Quote Originally Posted by Joacim Andersson View Post
    It's pretty much the same idea. The main goal here is to get the name of the resource file from the button somehow. If the Text property of the buttons already shows the names you should of course use that.
    I thought of using the button text at first but couldn't have a resource named "C#", so just went with the button name instead, this lets you set the button text to whatever you like.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Can I Call a Class from my main form?

    Quote Originally Posted by Edgemeal View Post
    I thought of using the button text at first but couldn't have a resource named "C#"
    That's only because we're using VB here
    (Sorry, couldn't resist).

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    13

    Re: Can I Call a Class from my main form?

    Hmmmm, thanks for all the help guys.

    I guess I should've mentioned the other reason of mine for moving note subs to a seperate class/form/whatever would be that I also have programmed songs that play back.

    For example:

    Call F()
    System.Threading.Thread.Sleep(150)
    Call F()
    System.Threading.Thread.Sleep(150)
    Call A()
    System.Threading.Thread.Sleep(150)
    Call F()
    System.Threading.Thread.Sleep(150)

    This plays F, gives a pause (not very long) and goes to the next note. If my resource audio files are all in the button, I guess I'd have to call the notes for the song apart by literally putting

    "My.Computer.Audio.Play(My.Resources._1C, AudioPlayMode.Background)
    System.Threading.Thread.Sleep(150)"

    Again and again, instead of just Call F() for the song part, as well as a Call F() for the button. Hence why I would like to move the Call F(), Call C() functions to a different....page.

    Does that make sense?

    Other wise I would just place the resource in the button and not worry about a separate call function.

    Thanks!

  8. #8
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Can I Call a Class from my main form?

    Quote Originally Posted by Joacim Andersson View Post
    That's only because we're using VB here
    (Sorry, couldn't resist).
    If I may split hairs for a moment.

    Although the language name is pronounced "see sharp", the characters are explicitly defined in the specification to be the Latin Capital Letter C followed by the Number Sign (Unicode U+0023), not the Musical Notation Symbol (Unicode U+266F).

    See http://www.ecma-international.org/pu...s/Ecma-334.htm, section 6

    (The difference between the glyphs being that the number sign has sloping verticals, the music notation glyph has sloping horizontals)
    Last edited by Evil_Giraffe; Dec 15th, 2011 at 12:41 AM.

  9. #9
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Can I Call a Class from my main form?

    Look closer to the suggestions from Joacim Andersson.

    Besides the single Button_click code make a single Sub which plays the tone according to the value handed to that Sub.

    Code:
    Private Sub PlaySound(resourceName As String)
      My.Computer.Audio.Play(My.Resources.ResourceManager.GetStream(resourceName), AudioPlayMode.Background)
    End Sub
    Your playback would now look like:
    Code:
    PlaySound ("F")
    System.Threading.Thread.Sleep(150)
    PlaySound ("F")
    System.Threading.Thread.Sleep(150)
    PlaySound ("A")
    System.Threading.Thread.Sleep(150)
    PlaySound ("F")
    System.Threading.Thread.Sleep(150)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    13

    Re: Can I Call a Class from my main form?

    Quote Originally Posted by Joacim Andersson View Post
    Wouldn't it be easier to just have one single play sub and pass in the name of the resource to play?
    Code:
    Private Sub PlaySound(resourceName As String)
      My.Computer.Audio.Play(My.Resources.ResourceManager.GetStream(resourceName), AudioPlayMode.Background)
    End Sub
    Now you could for example set the Tag property of each button to the name of the resource to play. In your example set the Tag property of the cButton to "C" and in the Click event simply call the PlaySound like this:
    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) 
        PlaySound(CType(sender, Button).Tag.ToString)
      End Sub
    Now use the above event handler for all your buttons and you're done.
    Is the name of my .wav file being referred to as resourceName in this example code? I know I shouldn't literally copy your code in, because the variables are different, but even after adding in what I think was supposed to be subsituted, I can't figure out if resourceName is what you meant for me to replace or not. Either way it's not working yet.

    Here's what I have,

    Code:
     Private Sub PlaySound(ByVal C As String)
            My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
        End Sub
    
        Private Sub cButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            PlaySound(CType(sender, Button).Tag.ToString)
        End Sub
    I tagged the cButton as "C". Is that's what supposed to be carrying through as resourceName?

    I guess I'm just confused as to where my actual .wav file comes into play.

  11. #11
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Can I Call a Class from my main form?

    IMHO Yes, "My.Resources.ResourceManager.GetStream(resourceName)" with resourceName="C" should be the same as "My.Resources.C"
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Can I Call a Class from my main form?

    It doesn't matter what you call the parameter in the PlaySound method. Since you have you're wave files as resources in your app I thought resourceName was a fitting name. But you need to use it, what you have is this:
    Code:
    Private Sub PlaySound(ByVal C As String)
      My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
    End Sub
    That will play the C resource regardless what you pass in since you don't use the string you're passing in: My.Resources.C is only referring to the C resource. My.Resources.ResourceManager.GetStream is used so you can pass in a string with the name of the resource. So for your code to work you should use:
    Code:
    Private Sub PlaySound(ByVal C As String)
      My.Computer.Audio.Play(My.Resources.ResourceManager.GetStream(C), AudioPlayMode.Background)
    End Sub
    However now the name doesn't make sense. Since you could just as well call PlaySound like this:
    Code:
    PlaySound("D")
    So why is the argument named C in this case when you ask it to play the "D"? If you like you can do this:
    Code:
    Private Sub PlaySound(ByVal tone As String)
      My.Computer.Audio.Play(My.Resources.ResourceManager.GetStream(tone), AudioPlayMode.Background)
    End Sub
    Just name the parameter whatever you like that fits.
    Last edited by Joacim Andersson; Dec 15th, 2011 at 06:11 AM.

  13. #13

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    13

    Re: Can I Call a Class from my main form?

    Quote Originally Posted by Joacim Andersson View Post
    It doesn't matter what you call the parameter in the PlaySound method. Since you have you're wave files as resources in your app I thought resourceName was a fitting name. But you need to use it, what you have is this:
    Code:
    Private Sub PlaySound(ByVal C As String)
      My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
    End Sub
    That will play the C resource regardless what you pass in since you don't use the string you're passing in: My.Resources.C is only referring to the C resource. My.Resources.ResourceManager.GetStream is used so you can pass in a string with the name of the resource. So for your code to work you should use:
    Code:
    Private Sub PlaySound(ByVal C As String)
      My.Computer.Audio.Play(My.Resources.ResourceManager.GetStream(C), AudioPlayMode.Background)
    End Sub
    However now the name doesn't make sense. Since you could just as well call PlaySound like this:
    Code:
    PlaySound("D")
    So why is the argument named C in this case when you ask it to play the "D"? If you like you can do this:
    Code:
    Private Sub PlaySound(ByVal tone As String)
      My.Computer.Audio.Play(My.Resources.ResourceManager.GetStream(tone), AudioPlayMode.Background)
    End Sub
    Just name the parameter whatever you like that fits.
    I suppose that the argument is named C because I want it to play the key of C? In my first post on this thread I said that I was trying to get just C to work first, not D.

    I changed it back to GetStream and it fails to play C.wav when I click cButton.

    Was the second part of the code correct?

    Code:
    Private Sub PlaySound(ByVal C As String)
            My.Computer.Audio.Play(My.Resources.ResourceManager.GetStream(C), AudioPlayMode.Background)
        End Sub
    
        Private Sub cButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            PlaySound(CType(sender, Button).Tag.ToString)
        End Sub

  14. #14
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Can I Call a Class from my main form?

    The idea is that PlaySound should be able to play any key. Just pass in the name of the wave file (or rather the name you've given the resource).
    Code:
    PlaySound("C")
    PlaySound("D")
    PlaySound("whatever")
    The second part is correct as long as the Tag property of the button in question has the string value "C" (for the C key).

  15. #15
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Can I Call a Class from my main form?

    Quote Originally Posted by Evil_Giraffe View Post
    Although the language name is pronounced "see sharp", the characters are explicitly defined in the specification to be the Latin Capital Letter C followed by the Number Sign (Unicode U+0023), not the Musical Notation Symbol (Unicode U+266F).

    See http://www.ecma-international.org/pu...s/Ecma-334.htm, section 6

    (The difference between the glyphs being that the number sign has sloping verticals, the music notation glyph has sloping horizontals)
    Well thank you so much Mr. "I have no humour at all " for bringing attention to the difference between the two unicode characters. However, as you pointed out yourself, the language is called C-Sharp, not C-NumberCharacter, so it's obviously named after the music key.

    Now that we've established that maybe you, or someone else with some musical knowledge, could answer this:

    What's the difference between C-Sharp and B-Flat?
    Last edited by Joacim Andersson; Dec 17th, 2011 at 10:11 PM. Reason: Added smiles so everyone understand that this is a joke :)

  16. #16
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Can I Call a Class from my main form?

    Quote Originally Posted by Joacim Andersson View Post
    What's the difference between C-Sharp and B-Flat?
    2 semitones (B and C) are between them so there is a big difference unless you're totally tone deaf.

    Maybe you meant to say, D-flat and a C-sharp, which are the same.

  17. #17
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Can I Call a Class from my main form?

    Yes I meant d-flat, sorry.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width