I'm re-posting this because I forgot to put code tags around the code.

I'm trying to make a form with a thermometer painted on it. I want the thermometer to go up
each second. I tried the code below and it sorta works, but not really. It does the For...
Next loop okay and shows the message box, but then does NOT Exit Sub or Me.Close. Instead
it starts the For... Next loop again. When it finishes the For... Next loop the second
time it shows the message box again, then appears to stop executing, but still does
not Me.Close. I guess I don't understand the paint event.

Public Class ThermometerForm
    Public intFillTop As Integer = 361
    Public intFillHeight As Integer = 28

    Private Sub ThermometerForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim TimeNow As Date
        Dim TimeLater As Date
        Dim intWaitTime As Integer = 1 'number of seconds each
        TimeNow = DateTime.Now
        TimeLater = DateTime.Now

        'Draw column of thermometer in blue
        e.Graphics.DrawRectangle(Pens.Blue, 410, 40, 10, 350)
        'e.Graphics.DrawRectangle(Pens.Blue, 410, 40, 10, 350)
        'Draw bulb of thermometer in blue
        e.Graphics.DrawEllipse(Pens.Blue, 400, 375, 30, 30)
        'Fill bulb of thermometer with red
        e.Graphics.FillEllipse(Brushes.Red, 401, 376, 28, 28)

        'Dim mp As Integer = intMeltingPoint / 10
        'Dim intCurrentTemp As Integer = 0

        'Make thermometer go up each second
        For t = 1 To 18 'mp '105
            TimeNow = DateTime.Now
            TimeLater = DateTime.Now
            'Put current temp into label
            'intCurrentTemp = t * 10
            'lblTemp.Text = "Current sample temperature: " & intCurrentTemp
            'Fill shapes after outline, so fill covers parts of shapes
            'Fill column of thermometer with red
            e.Graphics.FillRectangle(Brushes.Red, 411, intFillTop, 8, intFillHeight)
            'e.Graphics.FillRectangle(Brushes.Red, 411, 41, 8, 348)
            intFillTop = intFillTop - 3 '10
            intFillHeight = intFillHeight + 3 '10
            'Time Delay
            Do
                TimeLater = DateTime.Now
                Application.DoEvents()
            Loop Until DateDiff(DateInterval.Second, TimeNow, TimeLater) >= _
            intWaitTime
        Next t
        MessageBox.Show("Close form")
        Exit Sub
        Me.Close()

    End Sub

End Class

Thanks.

Put the Exit Sub after Me.Close or remove it and it'll work.

.........
.........
MessageBox.Show("Close form")
[B]Me.Close()
Exit Sub[/B]

Okay, by putting Exit Sub at bottom it finally closes the window, but here's what is still happening that I don't understand.

When the form loads the For... Next loop works and draws my thermometer going up 18 times, as it should. Then the message box shows. BUT THEN the loop runs again 18 more times, shows the message box again, then closes.

What's up with that?

Thanks

Your problem is because the Paint Event executes every time the form/control is painted on the screen.
You should not be doing the loop in the Paint event.
Tye using a Timer to increment a global integer to get the 'Temperature'.
Then call Refresh or Invalidate to force a re-Paint.
Then in Paint draw thermometer with the new value.

Thanks, I'll try that.

Thanks, I'll try that.

Dit it worked?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.