Imports System.IO
Imports System.Linq
Imports System.Data.Odbc
Imports System.ComponentModel
Imports System.Drawing.Imaging

Public Class Form1

    Dim con As OdbcConnection
    Dim counter As Integer = 0
    Dim image As Bitmap
    Dim dinfo As New DirectoryInfo(Directory.GetCurrentDirectory())
    Dim txtFile() As FileInfo = dinfo.GetFiles("*.txt")
    Dim dbFile() As FileInfo = dinfo.GetFiles("*.mdb")
    Dim bmpFile() As FileInfo = dinfo.GetFiles("*.bmp")
    Dim searchString As String

    Private Sub CreateConnection()
        con = New OdbcConnection()
        con.ConnectionString = "Dsn=WordsDatabase"
        con.Open()
    End Sub

    Private Function GetDataReader(ByVal query As String) As OdbcDataReader
        CreateConnection()
        Dim cmd As New OdbcCommand(query, con)
        Dim dr As OdbcDataReader
        dr = cmd.ExecuteReader
        Return dr
    End Function

    Private Sub ExecuteQuery(ByVal query As String)
        CreateConnection()
        Dim cmd As New OdbcCommand(query, con)
        cmd.ExecuteNonQuery()
    End Sub

    ''' <summary>
    ''' This Function Load All Words From File Lowercase.txt To ListBox
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadWordsFromFile()
        Try
            Dim tr As TextReader = File.OpenText(txtFile(0).ToString())
            While tr.ReadLine() <> Nothing
                ListBox1.Items.Add(tr.ReadLine())
            End While
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' This Function Load All Words From Database WordsDatabase To ListBox
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadWordsFromDatabase()
        Try
            Dim query As String = "Select * From Lowercase"
            Dim dr As OdbcDataReader = GetDataReader(query)
            While dr.Read()
                ListBox1.Items.Add(dr("Field1"))
            End While
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            LoadWordsFromFile() 'load allWords from file to listbox
            'LoadWordsFromDatabase() 'load allWords from Database
            Timer1.Interval = 100
            Timer1.Start()
            image = New Bitmap(bmpFile(0).ToString())
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            If (TextBox1.Text.Equals("")) Then
                ErrorProvider1.SetError(TextBox1, "Must Enter In TextBox")
            Else
                ErrorProvider1.Dispose()

                'Searching a single Letter using LINQ (Language Integerated Query)
                'Dim query As IEnumerable(Of Object) = _
                '    From obj As Object In ListBox1.Items _
                '    Where obj.ToString().ToUpper().StartsWith(TextBox1.Text.ToUpper()) _
                '    Select obj 'selecting that particular object items in listbox items collection

                'For Each obj As Object In query
                '    ListBox1.SelectedItem = obj 'it is highlighting that word which first letter you write in textbox
                'Next

                'Search using For Next Loop
                'Dim str As String = TextBox1.Text
                'For i As Integer = 0 To ListBox1.Items.Count - 1
                '    If (ListBox1.Items(i).ToString().Equals(str)) Then
                '        ListBox1.SelectedIndex = i
                '        'Stop
                '    End If
                'Next

                Dim index As Integer = ListBox1.FindString(TextBox1.Text)
                ListBox1.SetSelected(index, True)
                PictureBox1.Image = AddTextToImage(image, TextBox1.Text.ToLower(), TextBox1.Text.ToUpper())

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' This Function Writes Text On Image At Specified Location
    ''' </summary>
    ''' <param name="bImg">Of Type Bitmap</param>
    ''' <param name="smallLetters">Of Type String</param>
    ''' <param name="capitalLetters" >Of Type String</param>
    ''' <returns>Bitmap Object</returns>
    ''' <remarks></remarks>
    Private Function AddTextToImage(ByVal bImg As Bitmap, ByVal smallLetters As String, ByVal capitalLetters As String) As Bitmap
        Dim tmpImage As New Bitmap(bImg, New Size(bImg.Width, bImg.Height))
        Dim graphic As Graphics = Graphics.FromImage(tmpImage)
        Dim brush As New SolidBrush(Color.Black)
        graphic.DrawString(smallLetters, New Font("Times New Roman", 12, FontStyle.Bold), brush, New PointF(20, 25))
        graphic.DrawString(capitalLetters, New Font("Times New Roman", 12, FontStyle.Bold), brush, New PointF(20, 50))
        graphic.Dispose()
        Return tmpImage
    End Function

    'Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    '    Try
    '        searchString = TextBox1.Text
    '        Dim index As Integer = ListBox1.FindString(searchString) 'returns that index where word is located
    '        If (index <> -1) Then
    '            'If ((Asc(e.KeyChar) >= 65 And Asc(e.KeyChar) <= 90) Or (Asc(e.KeyChar) >= 90 And Asc(e.KeyChar) <= 122)) Then
    '            If (Asc(e.KeyChar) >= 65 AndAlso Asc(e.KeyChar) <= 122) Then 'ASCII From small 'a' to Capital 'Z'
    '                ListBox1.SetSelected(index, True) 'select that index where word is located
    '                PictureBox1.Image = AddTextToImage(image, searchString.ToLower(), searchString.ToUpper())
    '                counter += 1
    '            ElseIf (Asc(e.KeyChar) = 8) Then 'ASCII of BackSpace
    '                searchString = searchString.Remove(searchString.Length - 1)
    '                Dim index2 As Integer = ListBox1.FindString(searchString)
    '                ListBox1.SetSelected(index2, True)
    '                PictureBox1.Image = AddTextToImage(image, searchString.ToLower(), searchString.ToUpper())
    '                counter -= 1
    '                If (counter = 0) Then
    '                    ListBox1.SelectedIndex = 0
    '                    PictureBox1.Image = Nothing
    '                End If
    '            End If
    '        End If
    '    Catch ex As Exception
    '        MessageBox.Show(ex.Message)
    '    End Try
    'End Sub

    Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        PictureBox1.Image = AddTextToImage(image, ListBox1.SelectedItem.ToString().ToLower(), ListBox1.SelectedItem.ToString().ToUpper())
        TextBox1.Text = ListBox1.SelectedItem
        'Timer1.Stop()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        searchString = TextBox1.Text
        Dim index As Integer = ListBox1.FindString(searchString)
        If index <> -1 Then
            PictureBox1.Image = AddTextToImage(image, searchString.ToLower(), searchString.ToUpper())
            ListBox1.SetSelected(index, True)
        End If
    End Sub
End Class
