[Help] Updating label with integer

Posts 1–4 of 4 · Page 1 of 1
[Help] Updating label with integer
Hi guys. I am having what I feel should be a really simple problem. However, I have not been able to fix it after changing my method of doing this several times.

I usually code in C++, but I decided to try VB for this. Basically, the point of the program is to send a spam message to an email address (or cell phone ). I need label9 to display the integer value stored in sent, updating frequently.

I originally put the lines:
Code:
sent = sent + 1
label9.text = sent
within the send loop of the fuction bw_dowork, but the label did not change at all. Then I changed it to display sent in label9 whenever label9 was clicked on or hovered over. That made label9 display the number 0 even long after many messages had been sent (even though I clicked AFTER multiple messages had been sent). The current version starts a new thread just for updating label9. However, it won't display anything. I added in a change of stored text in label9 to "Started Counting...", and that worked. I have not had any build errors, the application just doesn't display sent in label9.

Tell me what you think. (And don't be too hard on me, this is my first project in VB)

Code:
Imports System.Net.Mail
Imports System
Imports System.ComponentModel
Public Class Form1
    Dim x As Integer
    Dim sent As Integer = 0
    Dim bw As BackgroundWorker = New BackgroundWorker
    Dim bw2 As BackgroundWorker = New BackgroundWorker


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        bw.WorkerSupportsCancellation = True
        bw.WorkerReportsProgress = False
        Label8.Text = "Sending..."
        bw****nWorkerAsync()
    End Sub
    Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim AnEmailMessage As New MailMessage
        Dim TheTelephoneNumber As String = TextBox3.Text
        Dim MyCarrier As String = TextBox4.Text
        Dim at As String = "@"
        Dim delaytext As String = TextBox7.Text
        Dim delay As Integer = Val(delaytext)
        x = 1

        AnEmailMessage.From = New MailAddress(TextBox1.Text)
        AnEmailMessage.To.Add(TheTelephoneNumber + at + MyCarrier)
        AnEmailMessage.Subject = (TextBox5.Text)
        AnEmailMessage.Body = (TextBox6.Text)
        AnEmailMessage.Priority = MailPriority.High

        Dim SimpleSMTP As New SmtpClient("smtp.live.com")
        SimpleSMTP.Port = 25
        SimpleSMTP.EnableSsl = True
        SimpleSMTP.Credentials = New System.Net.NetworkCredential(TextBox1.Text, TextBox2.Text)
        Do While x = 1
            SimpleSMTP.Send(AnEmailMessage)
            sent = sent + 1
            System.Threading.Thread.Sleep(delay)
        Loop
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        x = 0
        Label8.Text = "Stopped"
    End Sub

    Private Sub Label9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label9.Click
        bw.WorkerSupportsCancellation = True
        bw.WorkerReportsProgress = False
        Label9.Text = "Started Counting..."
        bw2****nWorkerAsync()
    End Sub
    Private Sub bw2_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Do While 1 = 1
            Label9.Text = sent
            System.Threading.Thread.Sleep(500)
        Loop
    End Sub
End Class
The parts that mpgh censored are bw. runworkerasync() and bw2. runworkerasync() (Without the spaces)
Code:
        Do While x = 1
            SimpleSMTP.Send(AnEmailMessage)
            sent += 1
            Label9.text += 1
            System.Threading.Thread.Sleep(delay)
        Loop
Why don't you just set Label9's text to 0 and have it increment?
Quote Originally Posted by Lolland View Post
Code:
        Do While x = 1
            SimpleSMTP.Send(AnEmailMessage)
            sent += 1
            Label9.text += 1
            System.Threading.Thread.Sleep(delay)
        Loop
Why don't you just set Label9's text to 0 and have it increment?
I've tried that and it does not change to anything past 0.
Quote Originally Posted by Lolland View Post
Code:
        Do While x = 1
            SimpleSMTP.Send(AnEmailMessage)
            sent += 1
            Label9.text += 1
            System.Threading.Thread.Sleep(delay)
        Loop
Why don't you just set Label9's text to 0 and have it increment?
Treating string as an integer is just asking for trouble.

@OP, have you tried setting a breakpoint at the start of that section of code (the dowork method) and then stepping through line by line to make sure everything is correct. With your current code you're not actually assigning "sent" to label9, but you said you already tried that so that's probably not the problem.

[php]
Do While x = 1
SimpleSMTP.Send(AnEmailMessage)
sent += 1
Label9.Text = sent.ToString
System.Threading.Thread.Sleep(delay)
Loop
[/php]

Oh, as a little side note, you don't need to explicitly create the variable "x" to start an infinite loop. You can simply use 1 of either of these 2 statements.

[php]
While True
'infinite loop
End While
[/php]

[php]
Do
'infinite loop
Loop
[/php]

Anyway, try setting a breakpoint and make sure everything is assigning the way it should.

Also, you might wanna make some sort of check to ensure sent hasn't reached the maximum integer value (which is unlikely to occur, but if the program was run with a very short delay and left for a while it could reach the maxvalue and crash.
Posts 1–4 of 4 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Need help?