Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear All, I am making a software in visual basic 2008 that send sms to mobile through com. I have a list box with mobile number such as 03336241445 etc. I have 3 labels in Form and a timer. I want, when user click the SEND SMS button, then our code do following steps.

1 - start from item1 listbox and show item1 text on label1.Text
2 - After showing item1 text in label1 then timer start and show counting 1 to 5 on label2.Text
3 - when timer counting reached 5 then timer stop and label3 show result sending
4 - After listbox item1, code start again and do all the procedure to all list box items one by one.
5 - After completing All listbox items,Code show us Confirmation message that Completed Work.

Thanks to all for my Help.

I am using following code but this code only show label2 counting and when reached 5 label3 show Ok! text
VB
for i = 0 to listbox1.items.count -1
   listbox1. selecteditem = i
   label1.text = listbox1.selecteditem
   timer1.start()
   label2.text + = 1
   if label2.text = 5 then
      label3.text = "OK!"
      timer1.stop()
      listbox1.selecteditem = listbox1.selecteditem + 1
   end if
next i


This is not repost. The software that I am making, sending sms to one number easily. But I want of using listbox for sending sms more then one numbers. I am trying that my software send sms through Listbox one by one. Thanks
Posted
Updated 28-May-13 23:02pm
v4
Comments
_Amy 29-May-13 0:08am    
Sure we'll help you. But before let us know, if you helped yourself? Did you try anything? Where you stuck? Click on Improve question button and let us know "what you tried?".
Sergey Alexandrovich Kryukov 29-May-13 0:54am    
First of all, it looks like a re-post. If so, don't do it, work on the page of your original question.

This post is not any certain question, because you did not tag the UI library or application type you are using. For example, if you are talking about ListBox, you should specify its exact fully-qualified type name. There are several different types under this simple name. Also, there are several different timer types. Which one do you use?

—SA

1 solution

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 ...
VB
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'Do I have any numbers left to process...
    If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
        ListBox1.SelectedIndex += 1   'Highlight the next one in the list
        SendSMS()                     'Send a message to the new number
    Else
        Timer1.Stop()              
        Label1.Text = "OK!"     'Let the user know we're done
    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 ...
VB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ListBox1.SelectedIndex = 0  'Select the first item on the list...
    Timer1.Enabled = True       'Enable my timer...
    Timer1.Interval = 10000     'Set to send a message every 10 seconds...
    Timer1.Start()              'And we're off ....!!
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 ...
VB
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()
    'Note Label2 is showing the number of messages sent
    'so I'm adding 1 to the selected index
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 ...
VB
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  'Select the first item on the list
    Timer1.Enabled = True       'Enable my timer
    Timer1.Interval = 10000     'Send a message every 10 seconds
    Timer1.Start()              'And we're off ....!!
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()
    'Note Label2 is showing the number of messages sent
    'so I'm adding 1 to the selected index
    SendSMS()   'Send an SMS to the new number selected
End Sub
Private Sub SendSMS()
    'Put some code in here to send the SMS
    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[^])
 
Share this answer
 
v2
Comments
Maciej Los 29-May-13 6:18am    
Well explained! My 5!
CHill60 29-May-13 6:21am    
Thank you! Can you tell that I don't have a lot to do at the moment :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900