Failed_Noob
Failed_Noob

Reputation: 1357

How to create different instances of a form

The below Code is written inside the Timer Tick event. It works fine as long as the Time and Date are not same. When the Time and Date are same it only displays 1 message. How can I show multiple message for the same date and Time ? I think this can be done If I create different instances for a form. But I don't know how many same time and Date will be there in the database. So I can't do that. Is there any work around ?

Dim frm As New frmMessage
Dim nowDate As String = String.Format("{0:M/d/yyyy}", DateTime.Now)
Dim nowTime As String = String.Format("{0:h:mm tt}", DateTime.Now)
Dim mySelectQuery As String = "SELECT ReminderID, Date, Time, Subject, Reminder FROM Reminder WHERE Date LIKE '" & nowDate & "' AND Time LIKE '" & nowTime & "'"
Dim myConnString As String = "Data Source=" & Application.StartupPath & "\Database\SimpleDB.db3"
Dim sqConnection As New SQLiteConnection(myConnString)
Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)

sqConnection.Open()

Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()

Try
    If sqReader.HasRows = True Then

        While sqReader.Read()
            frm.Show()

            If (Not sqReader.IsDBNull(0)) Then
                frm.txtID.Text = sqReader.GetInt32(0)
            End If

            If (Not sqReader.IsDBNull(1)) Then
                frm.txtDate.Text = sqReader.GetString(1)
            End If

            If (Not sqReader.IsDBNull(2)) Then
                frm.txtTime.Text = sqReader.GetString(2)
            End If

            If (Not sqReader.IsDBNull(3)) Then
                frm.txtSubject.Text = sqReader.GetString(3)
            End If

            If (Not sqReader.IsDBNull(4)) Then
                frm.txtReminderText.Text = sqReader.GetString(4)
            End If

        End While

    End If

    sqReader.Close()
    sqConnection.Close()

Catch ex As Exception
    MsgBox("Error:" & ex.Message, vbExclamation)
End Try

Upvotes: 0

Views: 3111

Answers (1)

Menahem
Menahem

Reputation: 4144

if i understood correctly , you want to show a form for each row retrunring from the database. i`m not sure this sort of UI practice is best if you have more than a couple of rows , but you can instanciate a new form as you said for each row, instead of using the same instance all you need to do is replace:

frm.Show() 

with

Dim newForm As New frmMessage()
newForm.Show() 

Upvotes: 2

Related Questions