在visual basic 2008中带有标签的列表框和计时器 [英] Listbox and Timer with label in visual basic 2008

查看:102
本文介绍了在visual basic 2008中带有标签的列表框和计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,我正在使用visual basic 2008制作软件,通过com向手机发送短信。我有一个带有手机号码的列表框,如03336241445等。我在Form和计时器中有3个标签。我想,当用户点击发送短信按钮,然后我们的代码执行以下步骤。



1 - 从item1列表框开始并在label1.Text
2 - 在label1中显示item1文本后,计时器开始并在label2上显示计数1到5.Text

3 - 当计时器计数达到5时,计时器停止并且label3显示结果发送

4 - 在列表框item1之后,代码再次开始,逐个对所有列表框项执行所有操作。

5 - 完成所有列表框项后,代码显示我们完成工作的确认消息。



感谢所有人的帮助。



我正在使用以下代码但是这段代码只显示了label2计数,当达到5 label3时显示确定! text

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

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





这不是重新发布。我正在制作的软件,可以轻松地将短信发送到一个号码。但我想使用listbox发送短信多于一个数字。我正在尝试我的软件逐个通过列表框发送短信。谢谢



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

推荐答案

你的代码有很多错误...



1.你有启动一个计时器并停止它但你实际上并没有使用它。以下是计时器教程的链接[ ^ ]



2.因此,您的 For 循环可以非常快速地直接运行到了将Label3文本设置为OK的程度。



3.您只是在第6次迭代时在列表框中递增所选索引,因为该行在如果声明



4.您正在处理文字,如数字 - 这并不总是导致失败,进入是一个非常坏的习惯。所以不要做像 Label2.Text + = 1



指出事情是错的,我现在将引导您完成一个可能的解决方案...



要求:给定一个数字列表,发送短信到列表中的每个号码。显示标签上的进度,显示要发送的号码,消息的数量和最终完成的消息。

已添加知识:最好在每次尝试之间留出一些时间让系统有机会发送消息



所以我需要一个定时器控件,几个标签,我会使用ListBox来保存电话号码列表。



每次计时器滴答我想发送一条消息,除非我已经完成了所有这些,在这种情况下我只想显示一条消息并停止。所以Tick事件需要像......

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
    '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



请注意,我使用过选择索引 未选中EM。这是我方便的计数器,所以我不需要在任何地方引入 For 循环。

嗯......这不行当我尝试它......根本没有任何事情发生......啊,我忘记了任何事情!最方便的地方是当我开始我的程序时 - 我将使用Form 加载事件...


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  '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 ...

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


现在,当我运行它时,我可以看到发生了什么......我看到我没有向列表中的第一个号码发送短信。现在我可以在初始化时在 Form1_Load 子例程中执行此操作,但我将稍微重构我的代码并将SendSMS从tick事件中移出当我在列表中移动时,我的ListBox事件。所以整个事情变成......


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  '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





这不是你在这篇文章中所说的,但希望你明白了。如果你想看到在发送下一条消息之前剩下的时间倒计时,那就玩一个带有第二个定时器控件的游戏(或者我的首选是使用 System.Timer Class [ ^ ])


这篇关于在visual basic 2008中带有标签的列表框和计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆