'This source code has been developed by Cuong Pham (cuong@nmsu.edu) & Mary Lucero
'Any use of this software MUST contain this message.
Public Class FrPrimerSeq
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TxPrimerID As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents TxSequence As System.Windows.Forms.TextBox
    Friend WithEvents ButAdd As System.Windows.Forms.Button
    Friend WithEvents ButUpdate As System.Windows.Forms.Button
    Friend WithEvents ButDelete As System.Windows.Forms.Button
    Friend WithEvents ButQuit As System.Windows.Forms.Button
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents TxReference As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.TxPrimerID = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.TxSequence = New System.Windows.Forms.TextBox
        Me.ButAdd = New System.Windows.Forms.Button
        Me.ButUpdate = New System.Windows.Forms.Button
        Me.ButDelete = New System.Windows.Forms.Button
        Me.ButQuit = New System.Windows.Forms.Button
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.Label3 = New System.Windows.Forms.Label
        Me.TxReference = New System.Windows.Forms.TextBox
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 264)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(88, 24)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Primer ID"
        '
        'TxPrimerID
        '
        Me.TxPrimerID.Location = New System.Drawing.Point(120, 264)
        Me.TxPrimerID.Name = "TxPrimerID"
        Me.TxPrimerID.Size = New System.Drawing.Size(280, 20)
        Me.TxPrimerID.TabIndex = 2
        Me.TxPrimerID.Text = ""
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 296)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(112, 24)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "5' to 3' sequence"
        '
        'TxSequence
        '
        Me.TxSequence.Location = New System.Drawing.Point(120, 296)
        Me.TxSequence.Name = "TxSequence"
        Me.TxSequence.Size = New System.Drawing.Size(280, 20)
        Me.TxSequence.TabIndex = 4
        Me.TxSequence.Text = ""
        '
        'ButAdd
        '
        Me.ButAdd.Location = New System.Drawing.Point(408, 8)
        Me.ButAdd.Name = "ButAdd"
        Me.ButAdd.Size = New System.Drawing.Size(96, 32)
        Me.ButAdd.TabIndex = 5
        Me.ButAdd.Text = "Add Sequence"
        '
        'ButUpdate
        '
        Me.ButUpdate.Location = New System.Drawing.Point(408, 56)
        Me.ButUpdate.Name = "ButUpdate"
        Me.ButUpdate.Size = New System.Drawing.Size(96, 32)
        Me.ButUpdate.TabIndex = 6
        Me.ButUpdate.Text = "Update"
        '
        'ButDelete
        '
        Me.ButDelete.Location = New System.Drawing.Point(408, 104)
        Me.ButDelete.Name = "ButDelete"
        Me.ButDelete.Size = New System.Drawing.Size(96, 32)
        Me.ButDelete.TabIndex = 7
        Me.ButDelete.Text = "Delete"
        '
        'ButQuit
        '
        Me.ButQuit.Location = New System.Drawing.Point(408, 152)
        Me.ButQuit.Name = "ButQuit"
        Me.ButQuit.Size = New System.Drawing.Size(96, 32)
        Me.ButQuit.TabIndex = 8
        Me.ButQuit.Text = "Quit"
        '
        'Panel1
        '
        Me.Panel1.ForeColor = System.Drawing.SystemColors.Desktop
        Me.Panel1.Location = New System.Drawing.Point(8, 248)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(392, 8)
        Me.Panel1.TabIndex = 9
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = "Primer_sequences"
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(8, 8)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(392, 232)
        Me.DataGrid1.TabIndex = 10
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(8, 328)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(104, 24)
        Me.Label3.TabIndex = 11
        Me.Label3.Text = "Reference"
        '
        'TxReference
        '
        Me.TxReference.Location = New System.Drawing.Point(120, 328)
        Me.TxReference.Name = "TxReference"
        Me.TxReference.Size = New System.Drawing.Size(384, 20)
        Me.TxReference.TabIndex = 12
        Me.TxReference.Text = "TextBox1"
        '
        'FrPrimerSeq
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.InactiveBorder
        Me.ClientSize = New System.Drawing.Size(512, 366)
        Me.Controls.Add(Me.TxReference)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.DataGrid1)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.ButQuit)
        Me.Controls.Add(Me.ButDelete)
        Me.Controls.Add(Me.ButUpdate)
        Me.Controls.Add(Me.ButAdd)
        Me.Controls.Add(Me.TxSequence)
        Me.Controls.Add(Me.TxPrimerID)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.ForeColor = System.Drawing.SystemColors.Highlight
        Me.Name = "FrPrimerSeq"
        Me.Text = "FrPrimerSeq"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Mode As Integer
    Private strSQL As String
    Private ID As String
    Private Sub FrPrimerSeq_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_PrimerSeq()
    End Sub

    Private Sub Load_PrimerSeq()
        On Error Resume Next
        ' create a connection string 
        Init()
        Dim connString As String = strConn
        Dim myConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection
        myConnection.ConnectionString = connString
        ' create a data adapter 
        Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("Select PrimerID,[5' to 3' sequence], Reference from primer_sequences", myConnection)
        ' create a new dataset 
        Dim ds As DataSet = New DataSet
        ' fill dataset 
        da.Fill(ds, "Primer_sequences")

        ' write dataset contents to an xml file by calling WriteXml method 
        ' Attach DataSet to DataGrid

        DataGrid1.DataSource = ds.DefaultViewManager
        DataGrid1.ColumnHeadersVisible = True
        TxPrimerID.Text() = ""
        TxSequence.Text() = ""
        TxReference.Text = ""

        DataGrid1.Refresh()
    End Sub

    Private Sub ButQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButQuit.Click
        Me.Hide()
        'CloseConn()
    End Sub

    Private Sub DataGrid1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
        Dim i As Integer
        Dim t As Boolean
        t = False
        Mode = 1
        i = 0
        Flush()
        On Error Resume Next
        While (t = False)
            If DataGrid1.IsSelected(i) Then
                ID = DataGrid1.Item(i, 0)
                TxPrimerID.Text() = DataGrid1.Item(i, 0)
                TxSequence.Text() = DataGrid1.Item(i, 1)
                TxReference.Text = DataGrid1.Item(i, 2)
                ButUpdate.Enabled = True
                ButDelete.Enabled = True
                t = True
            End If
            i = i + 1
        End While
    End Sub

    Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAdd.Click
        TxPrimerID.Text() = ""
        TxSequence.Text() = ""
        TxReference.Text = ""
        TxPrimerID.Focus()
        ButUpdate.Enabled = True
        ButDelete.Enabled = False
        Mode = 0
    End Sub
    Private Sub Flush()
        TxPrimerID.Text() = ""
        TxSequence.Text() = ""
        TxReference.Text = ""
        TxPrimerID.Focus()

    End Sub

    Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate

    End Sub

    Private Sub ButUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButUpdate.Click
        On Error Resume Next
        OpenConn()
        If Mode = 0 Then
            If TxPrimerID.Text = "" Then
                MsgBox("Primer ID can not be left blank, pls enter Primer ID")
                TxPrimerID.Focus()
                Exit Sub
            End If
            strSQL = "INSERT INTO Primer_sequences VALUES ('" & TxPrimerID.Text & "','" & TxSequence.Text & "','" & TxReference.Text & "')"
            Dim myCommand As New OleDb.OleDbCommand(strSQL)
            myCommand.Connection = myConnection
            myCommand.ExecuteNonQuery()

            myCommand.Dispose()

            Init()
            Dim myCon As OleDb.OleDbConnection = New OleDb.OleDbConnection
            myCon.ConnectionString = strConn
            ' create a data adapter 
            Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("Select PrimerID,[5' to 3' sequence], Reference from primer_sequences", myConnection)
            ' create a new dataset 
            Dim ds As DataSet = New DataSet
            ' fill dataset 
            da.Fill(ds, "Primer_sequences")

            ' write dataset contents to an xml file by calling WriteXml method 
            ' Attach DataSet to DataGrid

            DataGrid1.DataSource = ds.DefaultViewManager
            DataGrid1.ColumnHeadersVisible = True

            DataGrid1.Refresh()
            da.Dispose()
            ds.Dispose()
            myCon.Close()
        End If
        If Mode = 1 Then
            If TxPrimerID.Text = "" Then
                MsgBox("Primer ID can not be left blank, pls enter Primer ID")
                TxPrimerID.Focus()
                Exit Sub
            End If
            strSQL = "UPDATE Primer_sequences SET PrimerID = '" & TxPrimerID.Text & _
            "',[5' to 3' sequence]= '" & TxSequence.Text & _
            "',Reference= '" & TxReference.Text & _
            "' WHERE PrimerID='" & ID & "'"

            Dim myCommand As New OleDb.OleDbCommand(strSQL)
            myCommand.Connection = myConnection

            myCommand.ExecuteNonQuery()
            DataGrid1.Item(DataGrid1.CurrentRowIndex, 0) = TxPrimerID.Text
            DataGrid1.Item(DataGrid1.CurrentRowIndex, 1) = TxSequence.Text
            DataGrid1.Item(DataGrid1.CurrentRowIndex, 2) = TxReference.Text
            myCommand.Dispose()

        End If

        myConnection.Close()
    End Sub

    Private Sub ButDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButDelete.Click
        Mode = 2
        On Error Resume Next


        Dim msg As String
        Dim title As String
        Dim style As MsgBoxStyle
        Dim response As MsgBoxResult
        msg = "Do you  really want to delete the Primer :" & ID & " ?"  ' Define message.
        style = MsgBoxStyle.DefaultButton2 Or _
           MsgBoxStyle.Critical Or MsgBoxStyle.YesNo
        title = "Delete Comfirmation"   ' Define title.
        ' Display message.
        response = MsgBox(msg, style, title)
        If response = MsgBoxResult.Yes Then   ' User chose Yes.
            strSQL = "DELETE FROM Primer_sequences WHERE PrimerID = '" & ID & "'"
            OpenConn()
            Dim myCommand As New OleDb.OleDbCommand(strSQL)
            myCommand.Connection = myConnection
            myCommand.ExecuteNonQuery()
            DataGrid1.Refresh()
            myCommand.Dispose()
            myConnection.Close()
            Load_PrimerSeq()
            ' Perform some other action.
        End If
        myConnection.Close()
    End Sub

End Class
