' The source code has been developed by Cuong Pham (cuong@nmsu.edu) & Mary Lucero
' Any use of this code MUST contain this message

Public Class frLoadseqfile
    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 Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Text2 As System.Windows.Forms.TextBox
    Friend WithEvents CbTissue As System.Windows.Forms.ComboBox
    Friend WithEvents ButAddFile As System.Windows.Forms.Button
    Friend WithEvents ButRemoveFile As System.Windows.Forms.Button
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents CbListFile As System.Windows.Forms.ComboBox
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents TxDate As System.Windows.Forms.TextBox
    Friend WithEvents List1 As System.Windows.Forms.ListBox
    Friend WithEvents TxSearch As System.Windows.Forms.TextBox
    Friend WithEvents ButLoadListFile As System.Windows.Forms.Button
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.TxSearch = New System.Windows.Forms.TextBox
        Me.Text2 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.CbTissue = New System.Windows.Forms.ComboBox
        Me.ButAddFile = New System.Windows.Forms.Button
        Me.ButRemoveFile = New System.Windows.Forms.Button
        Me.Label3 = New System.Windows.Forms.Label
        Me.CbListFile = New System.Windows.Forms.ComboBox
        Me.Label4 = New System.Windows.Forms.Label
        Me.TxDate = New System.Windows.Forms.TextBox
        Me.List1 = New System.Windows.Forms.ListBox
        Me.ButLoadListFile = New System.Windows.Forms.Button
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(552, 24)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Enter the path that contains sequence files (should not contain any directory wit" & _
        "hin it)"
        '
        'TxSearch
        '
        Me.TxSearch.Location = New System.Drawing.Point(8, 32)
        Me.TxSearch.Name = "TxSearch"
        Me.TxSearch.Size = New System.Drawing.Size(480, 20)
        Me.TxSearch.TabIndex = 1
        Me.TxSearch.Text = "TextBox1"
        '
        'Text2
        '
        Me.Text2.Location = New System.Drawing.Point(496, 32)
        Me.Text2.Name = "Text2"
        Me.Text2.Size = New System.Drawing.Size(64, 20)
        Me.Text2.TabIndex = 2
        Me.Text2.Text = "TextBox2"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(8, 63)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(232, 32)
        Me.Button1.TabIndex = 4
        Me.Button1.Text = "Load sequence files  to Database"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(256, 64)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(152, 32)
        Me.Button2.TabIndex = 5
        Me.Button2.Text = "Exit"
        '
        'CbTissue
        '
        Me.CbTissue.Location = New System.Drawing.Point(319, 127)
        Me.CbTissue.Name = "CbTissue"
        Me.CbTissue.Size = New System.Drawing.Size(233, 21)
        Me.CbTissue.TabIndex = 7
        Me.CbTissue.Text = "ComboBox1"
        '
        'ButAddFile
        '
        Me.ButAddFile.Location = New System.Drawing.Point(230, 169)
        Me.ButAddFile.Name = "ButAddFile"
        Me.ButAddFile.Size = New System.Drawing.Size(72, 32)
        Me.ButAddFile.TabIndex = 8
        Me.ButAddFile.Text = "Add   >>"
        '
        'ButRemoveFile
        '
        Me.ButRemoveFile.Location = New System.Drawing.Point(230, 214)
        Me.ButRemoveFile.Name = "ButRemoveFile"
        Me.ButRemoveFile.Size = New System.Drawing.Size(72, 32)
        Me.ButRemoveFile.TabIndex = 9
        Me.ButRemoveFile.Text = "<< Remove"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(267, 127)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(41, 24)
        Me.Label3.TabIndex = 11
        Me.Label3.Text = "Tissue"
        '
        'CbListFile
        '
        Me.CbListFile.Location = New System.Drawing.Point(8, 192)
        Me.CbListFile.Name = "CbListFile"
        Me.CbListFile.Size = New System.Drawing.Size(208, 21)
        Me.CbListFile.TabIndex = 12
        Me.CbListFile.Text = "ComboBox1"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(6, 124)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(112, 24)
        Me.Label4.TabIndex = 13
        Me.Label4.Text = "Date of creating files"
        '
        'TxDate
        '
        Me.TxDate.Location = New System.Drawing.Point(111, 124)
        Me.TxDate.Name = "TxDate"
        Me.TxDate.Size = New System.Drawing.Size(104, 20)
        Me.TxDate.TabIndex = 14
        Me.TxDate.Text = "TextBox1"
        '
        'List1
        '
        Me.List1.Location = New System.Drawing.Point(24, 296)
        Me.List1.Name = "List1"
        Me.List1.Size = New System.Drawing.Size(120, 43)
        Me.List1.TabIndex = 15
        Me.List1.Visible = False
        '
        'ButLoadListFile
        '
        Me.ButLoadListFile.Location = New System.Drawing.Point(8, 152)
        Me.ButLoadListFile.Name = "ButLoadListFile"
        Me.ButLoadListFile.Size = New System.Drawing.Size(208, 32)
        Me.ButLoadListFile.TabIndex = 16
        Me.ButLoadListFile.Text = "Load sequence files to List"
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(320, 160)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(232, 208)
        Me.DataGrid1.TabIndex = 17
        '
        'frLoadseqfile
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(568, 382)
        Me.Controls.Add(Me.DataGrid1)
        Me.Controls.Add(Me.ButLoadListFile)
        Me.Controls.Add(Me.List1)
        Me.Controls.Add(Me.TxDate)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.CbListFile)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.ButRemoveFile)
        Me.Controls.Add(Me.ButAddFile)
        Me.Controls.Add(Me.CbTissue)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Text2)
        Me.Controls.Add(Me.TxSearch)
        Me.Controls.Add(Me.Label1)
        Me.Name = "frLoadseqfile"
        Me.Text = "FrLoadSeqFile"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim ID As Integer
    Dim FileName As String
    Dim D As String
    Dim DateSearch As String

    Private Sub Load_SequenceFile()
        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 FileName  FROM Sequences WHERE TissueID=0 AND Mid(trim(DateCreated),1,10) LIKE '" & DateSearch & "%'", myConnection)
        ' create a new dataset 
        Dim ds As DataSet = New DataSet
        ' fill dataset 
        da.Fill(ds, "sequences")
        CbListFile.DataSource = ds.DefaultViewManager
        CbListFile.DisplayMember = "Sequences.FileName"
        CbListFile.Refresh()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Hide()
    End Sub

    Private Sub frLoadseqfile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TxSearch.Text = ""
        D = CStr(Now)
        Text2.Text = "*.*"

        DateSearch = Mid(Trim(D), 1, 10)
        TxDate.Text = DateSearch
        FillComboBox(CbTissue, "Tissues", "Description")
        CbTissue.Text = ""
        Load_SequenceFile()
    End Sub

    Private Sub ButAddFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAddFile.Click
        Dim t As String, strSQL As String
        'On Error Resume Next


        t = getID("tissues", "tissueID", "Description", CbTissue.Text)
        If Len(t) > 0 Then
            ID = CInt(t)
        Else
            ID = 0
        End If

        DateSearch = Mid(Trim(TxDate.Text), 1, 10)

        OpenConn()
        strSQL = "UPDATE Sequences SET TissueID = " & ID & _
                 " WHERE FileName='" & CbListFile.Text & "'"

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

        myCommand.ExecuteNonQuery()
        Load_DataGrid()
        DataGrid1.Refresh()
    End Sub
    Private Sub Load_DataGrid()
        Dim sSQL As String, t As String
        On Error Resume Next
        t = getID("tissues", "tissueID", "Description", CbTissue.Text)
        If Len(t) > 0 Then
            ID = CInt(t)
        Else
            ID = -1
        End If

        sSQL = "SELECT FileName FROM Sequences WHERE TissueID= " & ID

        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(sSQL, myConnection)
        ' create a new dataset 
        Dim ds As DataSet = New DataSet
        ' fill dataset 
        da.Fill(ds, "sequences")

        DataGrid1.DataSource = ds.DefaultViewManager
        DataGrid1.DataMember = "Sequences"
        DataGrid1.Refresh()
    End Sub

    Private Sub ButRemoveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButRemoveFile.Click

        Dim t As String, strSQL As String

        ID = 0
        OpenConn()

        strSQL = "UPDATE Sequences SET TissueID = " & ID & _
                 " WHERE FileName='" & FileName & "'"

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

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SearchPath As String, FindStr As String
        Dim FileSize As Long
        Dim NumFiles As Integer, NumDirs As Integer
        On Error Resume Next
        List1.Items.Clear()
        SearchPath = Trim(TxSearch.Text)
        If Len(SearchPath) = 0 Then
            MsgBox("You must enter the path")
            TxSearch.Focus()
            Exit Sub
        End If
        FindStr = Text2.Text
        FileSize = FindFiles(SearchPath, FindStr, NumFiles, NumDirs, List1)
        List1.Visible = False
    End Sub

    Private Sub ButLoadListFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButLoadListFile.Click
        'CbListFile.Items.Clear()
        DateSearch = Mid(Trim(TxDate.Text), 1, 10)
        Load_SequenceFile()
    End Sub

    Private Sub CbTissue_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CbTissue.SelectedIndexChanged
        Dim sSQL As String, t As String
        On Error Resume Next
        t = getID("tissues", "tissueID", "Description", CbTissue.Text)
        If Len(t) > 0 Then
            ID = CInt(t)
        Else
            ID = -1
        End If

        sSQL = "SELECT FileName FROM Sequences WHERE TissueID= " & ID

        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(sSQL, myConnection)
        ' create a new dataset 
        Dim ds As DataSet = New DataSet
        ' fill dataset 
        da.Fill(ds, "sequences")

        DataGrid1.DataSource = ds.DefaultViewManager
        DataGrid1.DataMember = "Sequences"
        DataGrid1.Refresh()

    End Sub
    Private Sub DataGrid1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
        Dim i As Integer
        Dim b As Boolean
        b = False
        i = 0
        On Error Resume Next
        While (b = False)
            If DataGrid1.IsSelected(i) Then
                FileName = DataGrid1.Item(i, 0)
                b = True
            End If
            i = i + 1
        End While
    End Sub
End Class
