AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Chris
Expertise
I can answer pretty much any question relating to VB.NET and its use in a Windows environment. I specialize in ASP.NET web development and MSSQL database access.

Experience
I have over 5 years of industry experience using VB.NET and other .NET technologies for web and database development.

Education/Credentials
I have some college education, but does it really matter in this field of work?

 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > Button Arrays

Topic: VB.NET



Expert: Chris
Date: 9/5/2007
Subject: Button Arrays

Question
I'm trying to create an application (VB 2005 Express) that loads individual wave files into 'slots' to use for sound effects.  Each slot has a play button, a stop button, and a pause button.  The program runs fine but I'm programming each button.  With 30 slots, that's a lot of editing!  What is the best way to consolidate the click of each button?  Here's the code I use for the play buttons (sb=secondary buffer):

Private Sub btnPlay1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay1.Click
       If Not sb(0) Is Nothing Then
           sb(0).Volume = vsVolume1.Value
           sb(0).Play(0, IIf(chkLoop1.Checked, BufferPlayFlags.Looping, BufferPlayFlags.Default))
       End If
   End Sub

Thanks for your help.

Mike

Answer
Sounds like the easiest thing for you to do here is to write a single event handler we'll call OnPlayButtonClick that will be shared by all of these play buttons, and fired whenever any of them is clicked.  You can take advantage of the Button's .Tag property, this Tag property is present on all Windows Forms controls, and is basically defined as being there for whatever you want, though it's not official.  Set the Tag property on each of these buttons to be the "sb" array index for the sound you want that button to play (eg: 0, 1, 2, ..., 29).  Then, to wire them up to your event handler, instead of double-clicking them in design mode to create 30 separate events, you can wire them manually with the AddHandler directive in Form_Load as I show below:

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 AddHandler btnPlay1.Click, AddressOf OnPlayButtonClick
 AddHandler btnPlay2.Click, AddressOf OnPlayButtonClick
 AddHandler btnPlay3.Click, AddressOf OnPlayButtonClick
 ' ... repeat as necessary ...
 AddHandler btnPlay30.Click, AddressOf OnPlayButtonClick
End Sub

So, btnPlay1.Tag should be set to 0, btnPlay2.Tag to 1, and so forth until btnPlay30 which should have a value of 29 in its .Tag property.  Whenever a user clicks any of these, because of the AddHandler directives, they'll end up firing off this event below:

Private Sub OnPlayButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
 Dim btnClicked As Button = DirectCast(sender, Button)
 Dim intSBIndex As Integer = Integer.Parse(btnClicked.Tag)

 If (intSBIndex >= 0) AndAlso (intSBIndex < sb.Length) Then
   If (Not sb(intSBIndex) Is Nothing) Then
     sb(intSBIndex).Volume = vsVolume1.Value
     sb(intSBIndex).Play(0, IIf(chkLoop1.Checked, BufferPlayFlags.Looping, BufferPlayFlags.Default))
   End If
 End If
End Sub

The only difference here from your original is that we're taking the "sender" parameter to the event, which should be the control that fired off the event.  If the user clicks btnPlay4, this is what will be passed to the event handler as the sender, and we'll cast that back to a Button object, get its Tag property, and convert it to an Integer.  Then, we'll use that as the index of the "sb" array, set the volume, and play the sound clip.

Hope this helps!

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.