'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 FrPCRPrimerCombination
    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 DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents cbForward As System.Windows.Forms.ComboBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents cbReverse As System.Windows.Forms.ComboBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents TxTarget As System.Windows.Forms.TextBox
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents TxAnnealingTemp As System.Windows.Forms.TextBox
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents TxExpectedFragmentSize 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
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.cbForward = New System.Windows.Forms.ComboBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.cbReverse = New System.Windows.Forms.ComboBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.TxTarget = New System.Windows.Forms.TextBox
        Me.Label4 = New System.Windows.Forms.Label
        Me.TxAnnealingTemp = New System.Windows.Forms.TextBox
        Me.Label5 = New System.Windows.Forms.Label
        Me.TxExpectedFragmentSize = 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
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 200)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(96, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Forward Primer"
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(0, 0)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(512, 184)
        Me.DataGrid1.TabIndex = 1
        '
        'cbForward
        '
        Me.cbForward.Location = New System.Drawing.Point(113, 197)
        Me.cbForward.Name = "cbForward"
        Me.cbForward.Size = New System.Drawing.Size(200, 21)
        Me.cbForward.TabIndex = 2
        Me.cbForward.Text = "ComboBox1"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 233)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(96, 24)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Reverse Primer"
        '
        'cbReverse
        '
        Me.cbReverse.Location = New System.Drawing.Point(112, 232)
        Me.cbReverse.Name = "cbReverse"
        Me.cbReverse.Size = New System.Drawing.Size(200, 21)
        Me.cbReverse.TabIndex = 4
        Me.cbReverse.Text = "ComboBox1"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(8, 266)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(96, 22)
        Me.Label3.TabIndex = 5
        Me.Label3.Text = "Target"
        '
        'TxTarget
        '
        Me.TxTarget.Location = New System.Drawing.Point(112, 264)
        Me.TxTarget.Name = "TxTarget"
        Me.TxTarget.Size = New System.Drawing.Size(200, 20)
        Me.TxTarget.TabIndex = 6
        Me.TxTarget.Text = "TextBox1"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(8, 296)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(152, 24)
        Me.Label4.TabIndex = 7
        Me.Label4.Text = "Annealing Temperature"
        '
        'TxAnnealingTemp
        '
        Me.TxAnnealingTemp.Location = New System.Drawing.Point(168, 296)
        Me.TxAnnealingTemp.Name = "TxAnnealingTemp"
        Me.TxAnnealingTemp.Size = New System.Drawing.Size(144, 20)
        Me.TxAnnealingTemp.TabIndex = 8
        Me.TxAnnealingTemp.Text = "TextBox1"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(8, 328)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(144, 24)
        Me.Label5.TabIndex = 9
        Me.Label5.Text = "Expected Fragment Size"
        '
        'TxExpectedFragmentSize
        '
        Me.TxExpectedFragmentSize.Location = New System.Drawing.Point(168, 328)
        Me.TxExpectedFragmentSize.Name = "TxExpectedFragmentSize"
        Me.TxExpectedFragmentSize.Size = New System.Drawing.Size(144, 20)
        Me.TxExpectedFragmentSize.TabIndex = 10
        Me.TxExpectedFragmentSize.Text = "TextBox1"
        '
        'ButAdd
        '
        Me.ButAdd.Location = New System.Drawing.Point(328, 192)
        Me.ButAdd.Name = "ButAdd"
        Me.ButAdd.Size = New System.Drawing.Size(176, 40)
        Me.ButAdd.TabIndex = 11
        Me.ButAdd.Text = "Add a PCR Primer Combination"
        '
        'ButUpdate
        '
        Me.ButUpdate.Location = New System.Drawing.Point(328, 239)
        Me.ButUpdate.Name = "ButUpdate"
        Me.ButUpdate.Size = New System.Drawing.Size(176, 40)
        Me.ButUpdate.TabIndex = 12
        Me.ButUpdate.Text = "Update"
        '
        'ButDelete
        '
        Me.ButDelete.Location = New System.Drawing.Point(328, 288)
        Me.ButDelete.Name = "ButDelete"
        Me.ButDelete.Size = New System.Drawing.Size(176, 40)
        Me.ButDelete.TabIndex = 13
        Me.ButDelete.Text = "Delete"
        '
        'ButQuit
        '
        Me.ButQuit.Location = New System.Drawing.Point(328, 336)
        Me.ButQuit.Name = "ButQuit"
        Me.ButQuit.Size = New System.Drawing.Size(176, 40)
        Me.ButQuit.TabIndex = 14
        Me.ButQuit.Text = "Quit"
        '
        'FrPCRPrimerCombination
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(512, 382)
        Me.Controls.Add(Me.ButQuit)
        Me.Controls.Add(Me.ButDelete)
        Me.Controls.Add(Me.ButUpdate)
        Me.Controls.Add(Me.ButAdd)
        Me.Controls.Add(Me.TxExpectedFragmentSize)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.TxAnnealingTemp)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.TxTarget)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.cbReverse)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.cbForward)
        Me.Controls.Add(Me.DataGrid1)
        Me.Controls.Add(Me.Label1)
        Me.Name = "FrPCRPrimerCombination"
        Me.Text = "PCR Primer Combinations"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim Mode As Integer
    Dim Forward As String
    Dim Reverse As String

    Private Sub FrPCRPrimerCombination_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim toolTip1 As New ToolTip

        ' Set up the delays for the ToolTip.
        toolTip1.AutoPopDelay = 5000
        toolTip1.InitialDelay = 1000
        toolTip1.ReshowDelay = 500
        ' Force the ToolTip text to be displayed whether or not the form is active.
        toolTip1.ShowAlways = True

        ' Set up the ToolTip text for the Button and Checkbox.
        toolTip1.SetToolTip(Me.Label1, "Forward primer comes from the Primer_Sequences table")


        Load_PCRPrimerCombination()
    End Sub
    Private Sub Load_PCRPrimerCombination()
        On Error Resume Next
        Init()
        Dim SQLs As String
        Dim t As String

        Dim connString As String = strConn
        Dim myConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection
        myConnection.ConnectionString = connString
        ' create a data adapter 
        SQLs = "SELECT * FROM PCRprimer_Combinations"


        Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(SQLs, myConnection)
        ' create a new dataset 
        Dim ds As DataSet = New DataSet
        ' fill dataset 
        da.Fill(ds, "PCRprimer_Combinations")

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

        DataGrid1.DataSource = ds.DefaultViewManager
        DataGrid1.DataMember = "PCRprimer_Combinations"
        DataGrid1.ColumnHeadersVisible = True
        DataGrid1.Refresh()
        myConnection.Close()

        cbForward.Items.Clear()
        FillComboBox(cbForward, "Primer_sequences", "PrimerId")
        cbForward.Text = ""
        cbReverse.Items.Clear()
        FillComboBox(cbReverse, "Primer_sequences", "PrimerId")
        cbReverse.Text = ""
        TxTarget.Text = ""
        TxAnnealingTemp.Text = ""
        TxExpectedFragmentSize.Text = ""

    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
        Flush()
        Mode = 1
        i = 0
        On Error Resume Next
        While (t = False)
            If DataGrid1.IsSelected(i) Then
                Forward = DataGrid1.Item(i, 0)
                Reverse = DataGrid1.Item(i, 1)
                cbForward.Text = DataGrid1.Item(i, 0)
                cbReverse.Text = DataGrid1.Item(i, 1)
                TxTarget.Text = DataGrid1.Item(i, 2)
                TxAnnealingTemp.Text = DataGrid1.Item(i, 3)
                TxExpectedFragmentSize.Text = DataGrid1.Item(i, 4)

                ButUpdate.Enabled = True
                ButDelete.Enabled = True
                t = True
            End If
            i = i + 1
        End While
    End Sub
    Private Sub ButQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButQuit.Click
        Me.Hide()
    End Sub

    Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAdd.Click
        cbForward.Text = ""
        cbReverse.Text = ""
        TxTarget.Text = ""
        TxAnnealingTemp.Text = "0.0"
        TxExpectedFragmentSize.Text = "0"
        Mode = 0
    End Sub
    Private Sub Flush()
        cbForward.Text = ""
        cbReverse.Text = ""
        TxTarget.Text = ""
        TxAnnealingTemp.Text = "0.0"
        TxExpectedFragmentSize.Text = "0"
    End Sub

    Private Sub ButUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButUpdate.Click
        Dim strSQL As String
        Dim sSQL As String

        On Error Resume Next

        OpenConn()
        If Mode = 0 Then

            strSQL = "INSERT INTO PCRprimer_Combinations (ForwardPrimer,ReversePrimer,Target,AnnealingTemp,ExpectedFragmentSize) " & _
            "  VALUES ('" & cbForward.Text & _
            "','" & cbReverse.Text & _
            "','" & TxTarget.Text & _
            "'," & CDbl(TxAnnealingTemp.Text) & _
            "," & CInt(TxExpectedFragmentSize.Text) & ")"


            Dim myConnection As New OleDb.OleDbConnection(strConn)
            Dim myCmd As New OleDb.OleDbCommand(strSQL, myConnection)
            myConnection.Open()
            myCmd.Connection = myConnection
            myCmd.ExecuteNonQuery()
            myCmd.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 ForwardPrimer,ReversePrimer,Target,AnnealingTemp,ExpectedFragmentSize FROM PCRprimer_Combinations", myConnection)

            ' create a new dataset 
            Dim ds As DataSet = New DataSet
            ' fill dataset 
            da.Fill(ds, "PCRprimer_Combinations")

            ' 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

            strSQL = "UPDATE PCRprimer_Combinations SET ForwardPrimer='" & cbForward.Text & _
            "',ReversePrimer='" & cbReverse.Text & _
            "',Target='" & TxTarget.Text & _
            "',AnnealingTemp=" & CDbl(TxAnnealingTemp.Text) & _
            ",ExpectedFragmentSize=" & CInt(TxExpectedFragmentSize.Text) & _
            " WHERE ForwardPrimer= '" & Forward & "' AND ReversePrimer='" & Reverse & "'"

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

            DataGrid1.Item(DataGrid1.CurrentRowIndex, 0) = cbForward.Text
            DataGrid1.Item(DataGrid1.CurrentRowIndex, 1) = cbReverse.Text
            DataGrid1.Item(DataGrid1.CurrentRowIndex, 2) = TxTarget.Text
            DataGrid1.Item(DataGrid1.CurrentRowIndex, 3) = TxAnnealingTemp.Text
            DataGrid1.Item(DataGrid1.CurrentRowIndex, 4) = TxExpectedFragmentSize.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
        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 PCR primer combination in which forward :" & Forward & " and reverse: " & Reverse & " ?"  ' 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 PCRprimer_Combinations  WHERE ForwardPrimer='" & Forward & "' AND ReversePrimer='" & Reverse & "'"
            OpenConn()

            Dim myCommand As New OleDb.OleDbCommand(strSQL)
            myCommand.Connection = myConnection
            myCommand.ExecuteNonQuery()
            DataGrid1.Refresh()
            myCommand.Dispose()
            myConnection.Close()

            Load_PCRPrimerCombination()

        Else
            ' Perform some other action.
        End If
    End Sub
End Class
