By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,040 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,040 IT Pros & Developers. It's quick & easy.

Use a randomizer to trigger certain sounds

P: 1
I am working on a small program project in Visual Basic 2013. I have a little program I want to make but I'm confused as to what code to use. I have a random generator running through a timer to generate a new random number from 1-10. I would like to have the program say the name of each number as it is randomized. I have the sound clips and I have it set up to display the numbers being randomized. I am just unsure how to make detect number changes and have it say something each time. This is the code I have so far:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.         Timer1.Start()
  3.  
  4.     End Sub
  5.  
  6.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  7.         Dim generator As New Random
  8.         Dim myRand As Integer
  9.         myRand = generator.Next(1, 11)
  10.         TextBox1.Text = myRand
  11.  
  12.         If Not TextBox1.Text = 1 Then
  13.             My.Computer.Audio.Play(My.Resources.One, AudioPlayMode.WaitToComplete)
  14.  
  15.         Else
  16.             My.Computer.Audio.Play(My.Resources.Silence, AudioPlayMode.Background)
  17.  
  18.         End If
  19.  
  20.         End Sub
Oct 10 '14 #1
Share this Question
Share on Google+
1 Reply


IronRazer
P: 82
Hi,

I would recommend turning on Option Strict to help you catch errors of Type Conversion such as where you are trying to assign an Integer Type to the Text property of a TextBox which is a String Type.

Also, you only want to create the new Random class one time when your application is starting.

You could use the Select Case pretty easy in this situation like this.

Expand|Select|Wrap|Line Numbers
  1. Option Strict On
  2.  
  3. Public Class Form1
  4.     Dim generator As New Random 'only create the new random class once
  5.  
  6.     Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
  7.         Timer1.Start()
  8.     End Sub
  9.  
  10.     Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
  11.         Dim myRand As Integer = generator.Next(1, 11)
  12.         TextBox1.Text = myRand.ToString
  13.  
  14.         Select Case myRand
  15.             Case 1
  16.                 My.Computer.Audio.Play(My.Resources.One, AudioPlayMode.WaitToComplete)
  17.             Case 2
  18.                 My.Computer.Audio.Play(My.Resources.Two, AudioPlayMode.WaitToComplete)
  19.             Case 3
  20.                 My.Computer.Audio.Play(My.Resources.Three, AudioPlayMode.WaitToComplete)
  21.  
  22.                 'add the rest of your "Case n" statements for the rest of your numbers
  23.  
  24.         End Select
  25.     End Sub
  26. End Class
  27.  
Oct 10 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.