VB.NEt SpVoice / SAPI always first in action - vb.net

Hello guys i want to get some help.. because my program "BINGO caller" the SAPI/SpVoice is always speaking first before displaying the value in my textbox. and other problem is while speaking the SAPI all buttons and commands are not functioning they become ok when the SAPI is done speaking and the number they speak is showing in texbox after the speak. please help me.. i want to my program is displaying the number while SAPI speak the value in my textbox. please help me. this is my code in displaying the Number and letter in textbox and this is the code in SAPI
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
nos = randomarray(index)
index = index + 1
If nos <= 15 And nos >= 1 Then
col = "B"
ElseIf nos <= 30 And nos >= 16 Then
col = "I"
ElseIf nos <= 45 And nos >= 31 Then
col = "N"
ElseIf nos <= 60 And nos >= 46 Then
col = "G"
ElseIf nos <= 75 And nos >= 61 Then
col = "O"
End If
lbl_no.Text = nos
lbl_let.Text = col
sVar = Mid(nos, 1, 1)
sVar2 = Mid(nos, 2)
CType(Me.Controls("lbl_" & nos.ToString), Label).BackColor = Color.Yellow
If index = randomarray.Count Then
Timer1.Stop()
btn_pause.Enabled = False
btn_clear.Show()
btn_exit.Hide()
End If
voice()
txt_count.Text = index - 1
End Sub
This is the SAPI code
Sub voice()
Dim VObj As Object
VObj = CreateObject("SAPI.SpVoice")
With VObj
.Volume = 100
.Rate = -2
.Speak(col & " " & nos)
.Speak(col & " " & sVar & " " & sVar2)
End With
End Sub
This is my Screenshot of my program

instead of calling voice() on the ui thread just call it on a thread pool thread.
'voice()
System.Threading.ThreadPool.QueueUserWorkItem(AddressOf voice)

Related

VB.NET Bubble Sorting not sorting

the problem given here is an error I have obtained when trying to sort my set values via bubble sorting method. Below is the code and the error i would obtain would be 'System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' after the first pass. Below is the entire code
Public Class Form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ourArray(20) As Integer
Dim i As Integer
ourArray(0) = 20
ourArray(1) = 12
ourArray(2) = 1
ourArray(3) = 15
ourArray(4) = 5
ourArray(5) = 9
ourArray(6) = 10
ourArray(7) = 4
ourArray(8) = 3
ourArray(9) = 8
ourArray(10) = 2
ourArray(11) = 6
ourArray(12) = 13
ourArray(13) = 14
ourArray(14) = 7
ourArray(15) = 17
ourArray(16) = 11
ourArray(17) = 16
ourArray(18) = 18
ourArray(19) = 19
Do While ourArray(i) > ourArray(i + 1)
Dim iTemp As Integer
For iPass = 0 To 1
For i = 0 To 19
If ourArray(i) > ourArray(i + 1) Then
iTemp = ourArray(i)
ourArray(i) = ourArray(i + 1)
ourArray(i + 1) = iTemp
End If
Next
Next iPass
Dim assist As String
For i = 0 To 19
assist = assist & ourArray(i) & vbNewLine
Next
MsgBox(assist)
Loop
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
End Class
The goal is to press the given button when the program starts, and have the program sort the given random values in the correct order with each pass/button press. Example: start program, press sort button, values 4,5,3,2,1 appear in a message box. press button again, one of the values will be in the correct spot. 5,4,3,2,1 -> 1,4,3,2,5 -> 1,2,4,3,5 -> 1,2,3,5,4 -> 1,2,3,4,5

Lottery Program - Visual Basic

Have to create a lottery program, getting the random numbers and such easily enough. However I'm stumped. Essentially I have 2 buttons. One to display a checked list box with numbers 1-100, along with the 5 lotto numbers. I have a 2nd button that checks 2 things, to make sure that more than 5 numbers are not checked matching numbers. I'm lost on how to check for a match between the selected numbers between the RNG numbers.
Public Class Form1
Private Sub displayBtn_Click(sender As Object, e As EventArgs) Handles displayBtn.Click
Dim lottoNumbers(5) As Integer
Dim counter As Integer = 0
Dim number As Integer
Dim randomGenerator As New Random(Now.Millisecond)
'This will randomly select 5 unique numbers'
Do While counter < 5
number = randomGenerator.Next(0, 98)
If Array.IndexOf(lottoNumbers, number) = -1 Then
lottoNumbers(counter) = number
counter += 1
End If
Loop
'Display the lottery numbers in the label.'
Label1.Text = String.Empty
Array.Sort(lottoNumbers)
For Each num As Integer In lottoNumbers
Label2.Text = "Lottery Numbers"
Label1.Text &= CStr(num) & " "
Next num
For x = 0 To 98
CheckedListBox1.Items.Add(x + 1)
Next
End Sub
Private Sub checkBtn_Click(sender As Object, e As EventArgs) Handles checkBtn.Click
Dim count As Integer = 0
Dim x As Integer
'Checks to see if user checked more than 5'
For x = 0 To CheckedListBox1.Items.Count - 1
If (CheckedListBox1.CheckedItems.Count > 5) Then
MessageBox.Show("You cannot select more than 5 numbers.")
Return
Else
If (CheckedListBox1.GetItemChecked(x) = True) Then
count = count + 1
ListBox1.Items.Add(CheckedListBox1.Items(x))
End If
End If
Next
End Sub

if, elseif, searching, comparison, greater, smaller

I have this code on VB. I get no syntax error but when running and testing the numbers i get "number is not valid" or a wrong answer. Would you please help me what is wrong with this code? Thank you.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox2.Text = ""
Dim Num As Integer
Dim str As String
str = TextBox1.Text
Num = Integer.Parse(str)
If Num >= 100 And Num <= 199 Then
TextBox2.Text = "Basement"
ElseIf Num >= 200 And Num <= 500 And Num >= 900 Then
TextBox2.Text = "Main Floor"
ElseIf Num <= 700 And Num >= 501 And Num >= 750 And Num <= 900 Then
TextBox2.Text = "Upper Floor"
ElseIf Num <= 750 And Num >= 700 Then
TextBox2.Text = "Archives"
Else
TextBox2.Text = "Number is not valid"
End If
End Sub
This is impossible
ElseIf Num >= 200 And Num <= 500 And Num >= 900 Then
Here you are asking that a number be greater-equal than 200 (ok), lower-equal than 500 (ok) AND greater-equal than 900 (not ok since you already asked for a number lower-equal than 500). A number can't both be lower than 500 and greater than 900.
Without having the requirements, it's hard to know what your if statement should look like. I would suggest you run through your code manually "on paper" or start with one if statement at a time instead of writing all of them.
I would not mix to many checks in one line as it can get confusing.
Do somthing like this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox2.Text = ""
Dim Num As Integer
Dim str As String
str = TextBox1.Text
Num = Integer.Parse(str)
If Num >= 100 And Num <= 199 Then
TextBox2.Text = "Basement"
ElseIf Num >= 200 And Num <= 500 Then
TextBox2.Text = "Main Floor"
ElseIf Num >= 501 And Num <= 700 then
TextBox2.Text = "Upper Floor"
ElseIf Num ..... and so on
Else
TextBox2.Text = "Number is not valid"
End If
End Sub
Also it's good practice to name your TextBox controls and any other controls:
textbox1 could be something like txtfloornumber
textbox2 could be something like txtfloorname
This makes coming back to your code later in life easier.

Dynamically Changing Two Checkboxes ForeColor During Runtime

So i have this program in visual studio 2008 that will let the user select a month and a year and then dynamically changes all the checkboxes fore color which texts corresponds to the date of the selected month's Saturdays and Sundays.
lets say "Let i = 1" on the first run that is the date of first sunday and the very first day of January 2017 ((UTC-08:00) Pacific Time (US & Canada))
I know using for each will let the program loop to each control in Me.Controls so I don't think it would not read the first IF statement
If cb.Text = i-1
and that only the ELIF would exit the FOR LOOP.
but if I change the first statement into
if cb.Text = i+1
it worked. Anyone please tell me whats wrong with it.. I just don't get any errors or warnings just this logical error I think?
Dim cb
While i < 31
For Each cb In Me.Controls
If TypeOf cb Is CheckBox Then
If cb.Text = i - 1 Then
cb.ForeColor = Color.Blue
ElseIf cb.Text = i Then
cb.ForeColor = Color.Red
Exit For
End If
End If
Next
i += 7
End While
so this is how I solved my own problem, let the user select a month and a year, calculate the days and the first sunday then go on....
it is because the FOR EACH LOOP starts with the last control you added on the group of controls or in your form so I used the normal FOR LOOP and direct cast the object so it will look on the names of the control, names must be arranged in a manner of array. like c1,c2,c3....
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
On Error Resume Next
Dim nMonths As Integer = System.DateTime.DaysInMonth(ComboBox2.Text, ComboBox1.SelectedIndex + 1)
Dim i As Integer = 1
Dim x As Integer = 0
Dim customDate As Date = FormatDateTime(CDate(ComboBox1.SelectedIndex + 1 & "/1/" & ComboBox2.Text), DateFormat.ShortDate)
While customDate.DayOfWeek <> DayOfWeek.Sunday
customDate = customDate.AddDays(1)
i += 1
End While
x = i - 1
Dim cb
'set visible
For n = 1 To nMonths
cb = DirectCast(Me.Controls("chk" + n.ToString()), CheckBox)
cb.Visible = True
Next
If nMonths = 31 Then
'set sundays and saturdays checkboxes fore color
While i <= 31
Dim cb1 = DirectCast(Me.Controls("chk" + x.ToString()), CheckBox)
cb1.ForeColor = Color.Blue
Dim cb2 = DirectCast(Me.Controls("chk" + i.ToString()), CheckBox)
cb2.ForeColor = Color.Red
i += 7
x += 7
End While
ElseIf nMonths = 30 Then
'set sundays and saturdays checkboxes fore color
While i <= 30
Dim cb1 = DirectCast(Me.Controls("chk" + x.ToString()), CheckBox)
cb1.ForeColor = Color.Blue
Dim cb2 = DirectCast(Me.Controls("chk" + i.ToString()), CheckBox)
cb2.ForeColor = Color.Red
i += 7
x += 7
End While
ElseIf nMonths = 29 Then
'set sundays and saturdays checkboxes fore color
While i <= 29
Dim cb1 = DirectCast(Me.Controls("chk" + x.ToString()), CheckBox)
cb1.ForeColor = Color.Blue 'satudays
Dim cb2 = DirectCast(Me.Controls("chk" + i.ToString()), CheckBox)
cb2.ForeColor = Color.Red 'sundays
i += 7
x += 7
End While
ElseIf nMonths = 28 Then
'set sundays and saturdays checkboxes fore color
While i <= 28
Dim cb1 = DirectCast(Me.Controls("chk" + x.ToString()), CheckBox)
cb1.ForeColor = Color.Blue
Dim cb2 = DirectCast(Me.Controls("chk" + i.ToString()), CheckBox)
cb2.ForeColor = Color.Red
i += 7
x += 7
End While
End If
End Sub

I couldn't change color of diagonal line in 16*16 label matrix. What's my issue here?

16*16 matrix is coming to my screen when I start the program. But when I click diagonal button, diagonal line isn't red. That does not change.
my code :
Public Class Form1
Dim etk As New Label 'i define the matrix as etk
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i = 0 To 15
For j = 0 To 15
Dim etk As New Label
Me.Panel.Controls.Add(etk)
etk.Name = i
etk.Tag = j
etk.Size = New Size(26, 26)
etk.BackColor = Color.Black
etk.Location = New Point(30 * i + 10, 30 * j + 10)
Next
Next
End Sub
Private Sub diagonal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Timer1.Enabled = True
For i = 0 To 15
For j = 0 To 15
etk.Name = i
etk.Tag = j
If i = j Then
etk.BackColor = Color.Red
End If
Next
Next
End Sub
End Class
thanks for your interests..
First, the names of your dynamically loaded labels are not unique. You need to concatenate the x and the y to make it unique:
For i = 0 To 15
For j = 0 To 15
Dim etk As New Label
Me.Panel.Controls.Add(etk)
etk.Name = i & "," & j
etk.Tag = j
etk.Size = New Size(26, 26)
etk.BackColor = Color.Black
etk.Location = New Point(30 * i + 10, 30 * j + 10)
Next
Next
Then, when you are looping through in the click event, you need to find the existing label by name. Simply setting the name property on an existing label doesn't do anything to find a control. All that does is alter the one that's already there. To find a control by name, you can look it up using the name as the key value of the Controls property:
For i = 0 To 15
For j = 0 To 15
If i = j Then
Dim etk As Control = Me.Panel.Controls(i & "," & j)
etk.BackColor = Color.Red
End If
Next
Next
Or, more simply:
For i = 0 To 15
Dim etk As Control = Me.Panel.Controls(i & "," & i)
etk.BackColor = Color.Red
Next