There is quite a lot wrong with your code ...
1. You have started a timer and stopped it but you are not actually
using it. Here is a link to
a timer tutorial[
^]
2. As a result of that your
For
loop runs very quickly straight through to the point where it sets Label3 text to "OK".
3. You are only incrementing the selected index in the Listbox on 6th iteration because that line is within the
If
statement
4. You are treating text like numbers - this doesn't always cause a failure but it is a very bad habit to get into. So do not do things like
Label2.Text += 1
Having pointed out the things are are wrong, I'll now walk you through a possible solution ...
Requirement: Given a list of numbers, send an SMS to each number on the list. Display progress on labels showing the number to send to, the count of messages and a final completed message.
Added Knowledge: Good idea to leave some time between each attempt to give the system a chance to send the message
So I'm going to need a timer control, a couple of labels and I'll use a ListBox to hold the list of phone numbers.
Every time the timer "ticks" I want to send a message, unless I've done them all in which case I just want to display a message and stop. So the Tick event needs to be something like ...
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
ListBox1.SelectedIndex += 1
SendSMS()
Else
Timer1.Stop()
Label1.Text = "OK!"
End If
End Sub
Note that I've used
SelectedIndex
not SelectedItem. This is my handy counter so I don't need to introduce a
For
loop anywhere.
Hm ... this isn't working when I try it ... nothing happens at all ... ah, I've forgotten to initialise anything! The most convenient place to do that is when I start my program - I'll use the Form
Load
event ...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.SelectedIndex = 0
Timer1.Enabled = True
Timer1.Interval = 10000
Timer1.Start()
End Sub
So that's sort of working now, but I haven't let the user know what's happening. I think the best place for that is when I select each number in the list ...
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Label1.Text = ListBox1.SelectedItem.ToString()
Label2.Text = (ListBox1.SelectedIndex + 1).ToString()
End Sub
Now when I run it I can see what's going on ... and I see that I'm not sending an SMS to the first number on the list. Now I could do that in the
Form1_Load
subroutine when I do my initialisation, but instead I'm going to refactor my code slightly and move the SendSMS out of the tick event into my ListBox event as I move through the list. So the whole thing becomes ...
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
ListBox1.SelectedIndex += 1
Else
Timer1.Stop()
Label1.Text = "OK!"
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.SelectedIndex = 0
Timer1.Enabled = True
Timer1.Interval = 10000
Timer1.Start()
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Label1.Text = ListBox1.SelectedItem.ToString()
Label2.Text = (ListBox1.SelectedIndex + 1).ToString()
SendSMS()
End Sub
Private Sub SendSMS()
MessageBox.Show("sending SMS to " + Label1.Text)
End Sub
It's not quite what you said in this version of your post, but hopefully you get the idea. If you want to see a countdown of the time left before the next message is sent then have a play with a second timer control (or my preference is to use the
System.Timer Class[
^])