VB.NET tutorial: Image Viewer Application(final)


ADO.NET

Image Viewer Application

The Image Viewer Application in this part modified the Image Viewer Application in the previous section to enable the user to manage images list (Add new image, delete image, and update image). The Image Viewer Application stores image data (ID and Path) in a table call tblimages in dbimages Ms. Access database. The application is dived in to four main parts. The first part is the Clsdatabase class that handles database connectivity. The second part is the Form1 (main form) that displays images paths in a listview (Lstimages) control. When the user selects the image from the list, it will be viewed in a picturebox(PicBox) control. This form also provide three buttons (cmdnew,cmddelete, and cmdedit) to image the images list. The third part is Addnewimage form that lets the user to add a new image to the database. This form is displayed when the user clicks on cmdnew button on Form1 form. The final part is Updateimate is displayed when the user clicks cmdedit button on Form1 form. It lets the user to update the selected image. To delete a selected image, you just click on cmddelete button on Form1 form. It is no need to open a new window. In Addnewimage or Updateimate, we need to add OpenFileDialog component that lets the user chooses an image file when the cmdbrowse button is clicked.

 

Public Class Clsdatabase

    Private cn As New OleDb.OleDbConnection

    Sub startconnection()

        'check connection state for reopening

        If cn.State = ConnectionState.Open Then

            cn.Close()

        End If

        cn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\dbimages.mdb"

        cn.Open()

    End Sub

 

    Sub stopconnection()

        cn.Close()

        cn = Nothing

    End Sub

 

    Public ReadOnly Property connection() As OleDb.OleDbConnection

        Get

            Return cn

        End Get

    End Property

 

End Class

…………………………………………………

 

Public Class Form1

    Private cn As New OleDb.OleDbConnection

    Private cmd As OleDb.OleDbCommand

    Private dbcon As New Clsdatabase

 

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed

        dbcon.stopconnection()

 

    End Sub

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

        'Populate listview

        displaydata()

        'disable delete and edit buttons

        disablebuttons()

        

    End Sub

    Sub displaydata()

        Dim ds As New DataSet

        Dim ad As OleDb.OleDbDataAdapter

        Dim i As Integer

      

        dbcon.startconnection()

        'create DataAdapter object

        ad = New OleDb.OleDbDataAdapter("Select * From tblimages", dbcon.connection)

        'Fill data to DataSet object

        ad.Fill(ds, "tblimages")

        'clear listview

        Lstimages.Clear()

        'add columns

        addcolumns()

        For i = 0 To ds.Tables(0).Rows.Count - 1

            addDataToList(i, ds.Tables(0).Rows(i).Item(0), ds.Tables(0).Rows(i).Item(1))

        Next

 

    End Sub

    Sub deleteimage(ByVal id As Integer, ByVal cn As OleDb.OleDbConnection, ByVal cmd As OleDb.OleDbCommand)

        'delete image

        cmd = New OleDb.OleDbCommand("Delete * From tblimages Where ID=" & id, cn)

        cmd.ExecuteNonQuery()

        'update listview

        Lstimages.Items.Remove(Lstimages.SelectedItems(0))

    End Sub

    Sub addDataToList(ByVal index As Integer, ByVal id As Integer, ByVal path As String)

        On Error Resume Next

        Lstimages.Items.Add(id)

        Lstimages.Items.Item(index).SubItems.Add(path)

 

    End Sub

 

    Private Sub Lstimages_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lstimages.SelectedIndexChanged

        On Error Resume Next

        'view selected image in stretch mode

        Dim listitem As ListViewItem

        listitem = Lstimages.SelectedItems(0)

        Picbox.SizeMode = PictureBoxSizeMode.StretchImage

        Picbox.Image = Image.FromFile(listitem.SubItems.Item(1).Text)

        'enable delete and edit buttons

        enablebuttons()

 

    End Sub

    Sub addcolumns()

        'add 2 columns to listview--ID and Path

        Lstimages.View = View.Details

        Lstimages.MultiSelect = False

        Lstimages.Columns.Add("ID", 30, HorizontalAlignment.Center)

        Lstimages.Columns.Add("Path", 300, HorizontalAlignment.Center)

 

    End Sub

 

    Private Sub cmddelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmddelete.Click

        deleteimage(Lstimages.SelectedItems(0).Text, cn, cmd)

    End Sub

 

    Private Sub cmdnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdnew.Click

        'display a new form to add image

        Addnew.Show()

    End Sub

  

 

   

    Private Sub cmdedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdedit.Click

        Updateimage.Show()

    End Sub

 

    Private Sub Picbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Picbox.Click

 

    End Sub

 

    Sub disablebuttons()

        cmdedit.Enabled = False

        cmddelete.Enabled = False

 

    End Sub

    Sub enablebuttons()

        cmdedit.Enabled = True

        cmddelete.Enabled = True

 

    End Sub

End Class

………………………………………………………..

 

Public Class Addnewimage

 

    Private dbcon As New Clsdatabase

    Private cmd As OleDb.OleDbCommand

    Private Sub cmdbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdbrowse.Click

        Dim result As DialogResult

        'set filter

        OpenFileDialog1.Filter = "JPEG Files (*.jpg)|*.jpg|Bitmap Files(*.bmp)|*.bmp| Gif files(*.gif)|*.gif"

        'Enable multi-file names selection

        OpenFileDialog1.Multiselect = False

        'Open file dialog

        result = OpenFileDialog1.ShowDialog

        If result = DialogResult.OK Then

            txtpath.Text = OpenFileDialog1.FileName

            'View selected image in  Stretch mode

            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

            PictureBox1.Image = Image.FromFile(txtpath.Text)

        End If

 

    End Sub

 

    Private Sub Addnewimage_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed

       

        dbcon.stopconnection()

        'Requery listview to display the new added image

        Form1.displaydata()

       

    End Sub

 

 

 

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

      

        dbcon.startconnection()

    End Sub

 

 

    Private Sub cmdok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdok.Click

        'call addnewimage method to save the new image

        addnewimage(txtpath.Text, dbcon.connection, cmd)

 

    End Sub

    Sub addnewimage(ByVal path As String, ByVal cn As OleDb.OleDbConnection, ByVal cmd As OleDb.OleDbCommand)

        'save image

        cmd = New OleDb.OleDbCommand("Insert Into tblimages(Path) Values('" & path & "')", cn)

        cmd.ExecuteNonQuery()

 

    End Sub

End Class

 

……………………………………………………………………………………….

 

Public Class Updateimage

 

    Private cmd As OleDb.OleDbCommand

    Private dbcon As New Clsdatabase

    Private Sub Updateimage_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed

        dbcon.stopconnection()

        Form1.displaydata()

    End Sub

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

     

        dbcon.startconnection()

    End Sub

 

    Private Sub cmdbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdbrowse.Click

        Dim result As DialogResult

        'set filter

        OpenFileDialog1.Filter = "JPEG Files (*.jpg)|*.jpg|Bitmap Files(*.bmp)|*.bmp| Gif files(*.gif)|*.gif"

        'Enable multi-file names selection

        OpenFileDialog1.Multiselect = False

        'Open file dialog

        result = OpenFileDialog1.ShowDialog

        If result = DialogResult.OK Then

            txtpath.Text = OpenFileDialog1.FileName

            'View selected image in  Stretch mode

            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

            PictureBox1.Image = Image.FromFile(txtpath.Text)

        End If

 

    End Sub

 

    Private Sub cmdupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupdate.Click

        updateimage()

    End Sub

    Sub updateimage()

        'save image

        cmd = New OleDb.OleDbCommand("Update tblimages Set Path='" & txtpath.Text & "' Where ID=" & Form1.Lstimages.SelectedItems(0).Text, dbcon.connection)

        cmd.ExecuteNonQuery()

    End Sub

  

End Class




Comments

CAPTCHA image




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.