473,383 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Use a randomizer to trigger certain sounds

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
1 1057
IronRazer
83 64KB
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

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

Similar topics

6
by: Dave C. | last post by:
Hello, I have created the following trigger: CREATE TRIGGER tr_UDFTest ON UserDefinedFields FOR INSERT, UPDATE AS DECLARE @foobar varchar(100) SELECT @foobar= foobar FROM inserted IF (...
4
by: matt | last post by:
I have an image randomizer that was working fine. I added five images to it today, and none of the new five are ever called. Here's the script: <!-- Begin var theImages = new Array() ...
3
by: dataguy | last post by:
I can't find it anywhere in the manual, but I have a developer that wants to know if we can code a trigger to capture the data that has changed in a table only if certain columns have changed. It...
2
by: 73blazer | last post by:
Perhaps my thinking is wrong but this is what I have: 1 table (Tab1) with 1 attribute (Attr1) Attr1 char(16) for bit data ----------------------------------------------- create trigger...
6
by: Jchick | last post by:
Im a newbie with a sql table in a MSDE database that contains fields of CustName, BrokerName, Type, Status. I am trying to write a trigger that does this: 1. When a new record in a table is...
2
by: dba_222 | last post by:
Dear Experts, I'm an Oracle guy, who is being given more SQL Server assignments lately. I've been looking for things on the web about this, but I can't anything so far. In Oracle, I you...
4
by: mtgrizzly52 | last post by:
Good evening all. I have a challenge that I have no clue on how to accomplish it. I am so close to completing a challenging database it's scary, but through beta testing we discovered a need for...
3
by: PaulR | last post by:
Hi, DB2 LUW 8.2 We would like to implement a trigger to stop an insert or "empty row" (under certain conditions), but not to throw an exception back to calling application i.e the calling...
0
by: jehrich | last post by:
Hi Everyone, I am a bit of a hobby programmer (read newbie), and I have been searching for a solution to a SQL problem for a recent pet project. I discovered that there are a number of brilliant...
22
by: DreamersDelight | last post by:
Hi, I'm stuck on this problem and I can't find a sollution. I'm going to try and explain this step by step. 1 After certain rows get updated with a certain value. I don't know wich rows in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.