I need some help here. I am writing a little desktop app that monitors my ToDo database and whilst it works great when run once, it kind of freaks out when I run it from a timer. As in all the labels start repeating. Below is the entire code.
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Data
Imports System.Data.SqlClient
Public Class ToDoWatcher
Dim WatchEnabled As Integer
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
Const NL As String = vbCrLf 'simple hack to remember what the code for newline is :P
Dim ServerName As String = "my server"
Dim CatName As String = "my database"
Dim DBUserName As String = "my login"
Dim Password As String = "my password"
Dim Now As DateTime = DateTime.Now
Dim RoWCount As Integer
Private Sub CloseLbl_Click(sender As Object, e As EventArgs) Handles CloseLbl.Click
Me.Close()
End Sub
Private Sub ToDoWatcher_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
drag = True
mousex = Windows.Forms.Cursor.Position.X - Me.Left
mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End Sub
Private Sub ToDoWatcher_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub ToDoWatcher_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
drag = False
End Sub
Private Sub ToDoWatcher_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ToDos()
'Timer1.Start()
End Sub
Private Sub ToDos()
Dim X As Integer = 5
Dim Y As Integer = 20
Dim LB As Label = Nothing
Dim LBC As Label = Nothing
Dim Connection As String = "Data Source=" & ServerName & ";Initial Catalog=" & CatName & ";Persist Security Info=True;User ID=" & DBUserName & ";Password=" & Password
Using con As New SqlConnection(Connection)
Dim sqlCmd As New SqlCommand("SELECT * FROM TODO ORDER By TDID DESC", con)
Dim sqlDa As New SqlDataAdapter(sqlCmd)
Dim dt As New DataTable()
Dim i As Integer
sqlDa.Fill(dt)
RoWCount = dt.Rows.Count.ToString
If RoWCount > 0 Then
For i = 0 To RoWCount - 1
Dim isDone As String = dt.Rows(i)("TDDone").ToString()
LB = New Label()
Panel1.Controls.Add(LB)
LB.Visible = True
LB.Location = New System.Drawing.Point(X, Y)
LB.Size = New Size(310, 20)
If isDone = 1 Then
LB.Text = dt.Rows(i)("ToDo").ToString()
LB.Font = New Font(FontFamily.GenericSerif, 9, FontStyle.Strikeout)
LB.ForeColor = Color.LemonChiffon
Else
LB.ForeColor = Color.Yellow
LB.Text = dt.Rows(i)("ToDo").ToString()
End If
Y = Y + 18
Next
End If
con.Close()
con.Dispose()
sqlCmd = Nothing
sqlDa = Nothing
dt = Nothing
i = Nothing
LB = Nothing
End Using
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ToDos()
End Sub
End Class
I can't figure out how to properly dispose of the current info and have it reload on the timer tick :(
What am I missing and if you could be so kind SHOW me what I am missing instead of using a bunch of words I probably would not understand anyway.