Results 1 to 12 of 12

Thread: [RESOLVED] need help for multi display picturebox when selecting datagridview in vb.net

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Resolved [RESOLVED] need help for multi display picturebox when selecting datagridview in vb.net

    Dear All Master,
    please provide a solution to display multi picturebox when selecting datagridview.
    and is it possible to display images from subfolders?
    and can I hide datatable in datagridview in column FILENAME1,FILENAME2,FILENAME,FILENAME 4 on my select and show image in multi picturebox?
    for information i use visual studio 2010.

    Code:
    Public Class Form1
        Dim Path As String = "C:\Users\Administrator\Desktop\DBF"
        Dim Pathimage As String = "C:\Users\Administrator\Desktop\CATALOG"
        Dim cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
    Private Sub PopulateDataGridView()
            Try
                Dim dt = New DataTable()
    
                Dim query = "select ITM,ITC,QOH,PRS,FILENAME1,FILENAME2,FILENAME3,FILENAME4 FROM ITEM"
    
                Using adapter As New OleDbDataAdapter(query, cn.ToString)
                    adapter.Fill(dt)
                End Using
    
                Me.DataGridView1.DataSource = dt
     Catch myerror As OleDbException
                MessageBox.Show("Error: " & myerror.Message)
            Finally
            End Try
        End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            Dim dgv As DataGridView = CType(sender, DataGridView)
            Dim thisCell As DataGridViewCell = dgv.SelectedCells(0)
            Dim selCell As Integer = thisCell.ColumnIndex
            TextBox2.Text = Pathimage & "\" & DataGridView1.CurrentRow.Cells(selCell).Value.ToString()
            If Not (pbAdder.Image Is Nothing) Then
                pbAdder.Image.Dispose()
                pbAdder.Image = Nothing
    
            End If
    
            pbAdder.Image = Image.FromFile(TextBox2.Text)
        End Sub
    Attached Images Attached Images  

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: need help for multi display picturebox when selecting datagridview in vb.net

    Code:
    Private Sub PopulateDataGridView()
        Try
            Dim dt = New DataTable()
    
            Dim query = "select ITM,ITC,QOH,PRS,FILENAME1,FILENAME2,FILENAME3,FILENAME4 FROM ITEM"
    
            Using adapter As New OleDbDataAdapter(query, cn.ToString)
                adapter.Fill(dt)
            End Using
    
            Me.DataGridView1.DataSource = dt
    
            For x as integer = 4 to 7
               Me.DataGridView1.Columns(x).Visible = False 
            Next
    
            PictureBox1.DataBindings.Add("ImageLocation", dt, "FILENAME1")
            PictureBox2.DataBindings.Add("ImageLocation", dt, "FILENAME2")
            PictureBox3.DataBindings.Add("ImageLocation", dt, "FILENAME3")
            PictureBox4.DataBindings.Add("ImageLocation", dt, "FILENAME4")
    
         Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
        End Try
    End Sub

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: need help for multi display picturebox when selecting datagridview in vb.net

    To display images from a different folder, based on the filenames you have in your database, i'd use extended PictureBoxes...
    Add the class to your project, then rebuild. You'll find exPictureBox at the top of your toolbox.

    Code:
    Public Class exPictureBox
        Inherits PictureBox
    
        Private _prePath As String
        Public Property PrePath() As String
            Get
                Return _prePath
            End Get
            Set(ByVal value As String)
                _prePath = value
            End Set
        End Property
    
        Private _fileName As String
        Public Property FileName() As String
            Get
                Return _fileName
            End Get
            Set(ByVal value As String)
                _fileName = value
                MyBase.ImageLocation = If(value <> "", PrePath & value, "")
            End Set
        End Property
    
    End Class
    Then you can use databinding as i showed you...

    Code:
    Public Class Form1
    
        Dim Path As String = "C:\Users\Administrator\Desktop\DBF"
        Dim Pathimage As String = "C:\Users\Administrator\Desktop\CATALOG"
        Dim cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
    
        Private Sub PopulateDataGridView()
            Try
                Dim dt = New DataTable()
    
                Dim query = "select ITM,ITC,QOH,PRS,FILENAME1,FILENAME2,FILENAME3,FILENAME4 FROM ITEM"
    
                Using adapter As New OleDbDataAdapter(query, cn.ToString)
                    adapter.Fill(dt)
                End Using
    
                Me.DataGridView1.DataSource = dt
    
                For x as integer = 4 to 7
                    Me.DataGridView1.Columns(x).Visible = False 
                Next
    
                exPictureBox1.PrePath = Pathimage & "\" 
                exPictureBox2.PrePath = Pathimage & "\"
                exPictureBox3.PrePath = Pathimage & "\"
                exPictureBox4.PrePath = Pathimage & "\"
    
                exPictureBox1.DataBindings.Add("FileName", dt, "FILENAME1")
                exPictureBox2.DataBindings.Add("FileName", dt, "FILENAME2")
                exPictureBox3.DataBindings.Add("FileName", dt, "FILENAME3")
                exPictureBox4.DataBindings.Add("FileName", dt, "FILENAME4")
    
            Catch myerror As OleDbException
                MessageBox.Show("Error: " & myerror.Message)
            Finally
            End Try
        End Sub
    
    End Class

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: need help for multi display picturebox when selecting datagridview in vb.net

    Quote Originally Posted by .paul. View Post
    To display images from a different folder, based on the filenames you have in your database, i'd use extended PictureBoxes...
    Add the class to your project, then rebuild. You'll find exPictureBox at the top of your toolbox.

    Code:
    Public Class exPictureBox
        Inherits PictureBox
    
        Private _prePath As String
        Public Property PrePath() As String
            Get
                Return _prePath
            End Get
            Set(ByVal value As String)
                _prePath = value
            End Set
        End Property
    
        Private _fileName As String
        Public Property FileName() As String
            Get
                Return _fileName
            End Get
            Set(ByVal value As String)
                _fileName = value
                MyBase.ImageLocation = If(value <> "", PrePath & value, "")
            End Set
        End Property
    
    End Class
    Then you can use databinding as i showed you...

    Code:
    Public Class Form1
    
        Dim Path As String = "C:\Users\Administrator\Desktop\DBF"
        Dim Pathimage As String = "C:\Users\Administrator\Desktop\CATALOG"
        Dim cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
    
        Private Sub PopulateDataGridView()
            Try
                Dim dt = New DataTable()
    
                Dim query = "select ITM,ITC,QOH,PRS,FILENAME1,FILENAME2,FILENAME3,FILENAME4 FROM ITEM"
    
                Using adapter As New OleDbDataAdapter(query, cn.ToString)
                    adapter.Fill(dt)
                End Using
    
                Me.DataGridView1.DataSource = dt
    
                For x as integer = 4 to 7
                    Me.DataGridView1.Columns(x).Visible = False 
                Next
    
                exPictureBox1.PrePath = Pathimage & "\" 
                exPictureBox2.PrePath = Pathimage & "\"
                exPictureBox3.PrePath = Pathimage & "\"
                exPictureBox4.PrePath = Pathimage & "\"
    
                exPictureBox1.DataBindings.Add("FileName", dt, "FILENAME1")
                exPictureBox2.DataBindings.Add("FileName", dt, "FILENAME2")
                exPictureBox3.DataBindings.Add("FileName", dt, "FILENAME3")
                exPictureBox4.DataBindings.Add("FileName", dt, "FILENAME4")
    
            Catch myerror As OleDbException
                MessageBox.Show("Error: " & myerror.Message)
            Finally
            End Try
        End Sub
    
    End Class
    Dear Mr. .paul.
    Thank you for your reply. I've tried if it's only in 1 parent folder then it appears at once in the expicturebox. I tried to create a subfolder and it contained an image file but it didn't appear in the expicturebox. example path "C:\Users\Administrator\Desktop\CATALOG\test" & "C:\Users\Administrator\Desktop\CATALOG\test\test1".

    Thanks
    roy88

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: need help for multi display picturebox when selecting datagridview in vb.net

    exPictureBox PrePath has to contain the correct path, and end with “\”

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: need help for multi display picturebox when selecting datagridview in vb.net

    Quote Originally Posted by .paul. View Post
    exPictureBox PrePath has to contain the correct path, and end with “\”
    Dear Mr. .paul.
    if so it means I have to have 1 column in the datagriview which is "helper path" so that it can be used for images in subfolders.
    Thanks
    roy88

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: need help for multi display picturebox when selecting datagridview in vb.net

    Ok. So assuming you have...

    Code:
    Dim Pathimage As String = "C:\Users\Administrator\Desktop\CATALOG"
    as your base folder, you can enumerate sub folders, to find the path for the 'FileName'

    Code:
    Public Class exPictureBox
        Inherits PictureBox
    
        Private _prePath As String
        Public Property PrePath() As String
            Get
                Return _prePath
            End Get
            Set(ByVal value As String)
                _prePath = value
            End Set
        End Property
    
        Private _fileName As String
        Public Property FileName() As String
            Get
                Return _fileName
            End Get
            Set(ByVal value As String)
                _fileName = value
                ' You can search subfolders here to get the correct folder path for 'value' which is the filename in your datatable.
                MyBase.ImageLocation = If(value <> "", PrePath & value, "")
            End Set
        End Property
    
    End Class

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: need help for multi display picturebox when selecting datagridview in vb.net

    This will work, as long as you don't use duplicate filenames in your directory structure...

    Code:
    Imports System.IO.Directory
    
    Public Class exPictureBox
        Inherits PictureBox
    
        Private _prePath As String
        Public Property PrePath() As String
            Get
                Return _prePath
            End Get
            Set(ByVal value As String)
                _prePath = value
            End Set
        End Property
    
        Private _fileName As String
        Public Property FileName() As String
            Get
                Return _fileName
            End Get
            Set(ByVal value As String)
                _fileName = value
                Dim files As List(Of String) = SearchForFile(PrePath, value)
                If value <> "" AndAlso files.count > 0 Then
                    MyBase.ImageLocation = files.First
                Else
                    MyBase.ImageLocation = ""
                End If
            End Set
        End Property
    
        Function SearchForFile(ByVal RootFolder As String, ByVal searchFileName As String) As List(Of String)
            Dim ReturnedData As New List(Of String)                             'List to hold the search results
            Dim FolderStack As New Stack(Of String)                             'Stack for searching the folders
            FolderStack.Push(RootFolder)                                        'Start at the specified root folder
            Do While FolderStack.Count > 0                                      'While there are things in the stack
                Dim ThisFolder As String = FolderStack.Pop                      'Grab the next folder to process
                Try                                                             'Use a try to catch any errors
                    For Each SubFolder In GetDirectories(ThisFolder)            'Loop through each sub folder in this folder
                        FolderStack.Push(SubFolder)                             'Add to the stack for further processing
                    Next                                                        'Process next sub folder
                    ReturnedData.AddRange(GetFiles(ThisFolder, searchFileName)) 'Process searchFileName
                Catch ex As Exception                                           'For simplicity sake
                    'We'll ignore the errors
                End Try
            Loop                                                                'Process next folder in the stack
            Return ReturnedData                                                 'Return the list of files that match
        End Function
    
    End Class

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: need help for multi display picturebox when selecting datagridview in vb.net

    Quote Originally Posted by .paul. View Post
    Ok. So assuming you have...

    Code:
    Dim Pathimage As String = "C:\Users\Administrator\Desktop\CATALOG"
    as your base folder, you can enumerate sub folders, to find the path for the 'FileName'

    Code:
    Public Class exPictureBox
        Inherits PictureBox
    
        Private _prePath As String
        Public Property PrePath() As String
            Get
                Return _prePath
            End Get
            Set(ByVal value As String)
                _prePath = value
            End Set
        End Property
    
        Private _fileName As String
        Public Property FileName() As String
            Get
                Return _fileName
            End Get
            Set(ByVal value As String)
                _fileName = value
                ' You can search subfolders here to get the correct folder path for 'value' which is the filename in your datatable.
                MyBase.ImageLocation = If(value <> "", PrePath & value, "")
            End Set
        End Property
    
    End Class
    Dear .paul. ,

    I attach a screenshot of what I mean.

    if the subfolder is like in the screenshot, please provide an example of the class code
    thanks
    roy88
    Attached Images Attached Images  

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: need help for multi display picturebox when selecting datagridview in vb.net

    Quote Originally Posted by .paul. View Post
    This will work, as long as you don't use duplicate filenames in your directory structure...

    Code:
    Imports System.IO.Directory
    
    Public Class exPictureBox
        Inherits PictureBox
    
        Private _prePath As String
        Public Property PrePath() As String
            Get
                Return _prePath
            End Get
            Set(ByVal value As String)
                _prePath = value
            End Set
        End Property
    
        Private _fileName As String
        Public Property FileName() As String
            Get
                Return _fileName
            End Get
            Set(ByVal value As String)
                _fileName = value
                Dim files As List(Of String) = SearchForFile(PrePath, value)
                If value <> "" AndAlso files.count > 0 Then
                    MyBase.ImageLocation = files.First
                Else
                    MyBase.ImageLocation = ""
                End If
            End Set
        End Property
    
        Function SearchForFile(ByVal RootFolder As String, ByVal searchFileName As String) As List(Of String)
            Dim ReturnedData As New List(Of String)                             'List to hold the search results
            Dim FolderStack As New Stack(Of String)                             'Stack for searching the folders
            FolderStack.Push(RootFolder)                                        'Start at the specified root folder
            Do While FolderStack.Count > 0                                      'While there are things in the stack
                Dim ThisFolder As String = FolderStack.Pop                      'Grab the next folder to process
                Try                                                             'Use a try to catch any errors
                    For Each SubFolder In GetDirectories(ThisFolder)            'Loop through each sub folder in this folder
                        FolderStack.Push(SubFolder)                             'Add to the stack for further processing
                    Next                                                        'Process next sub folder
                    ReturnedData.AddRange(GetFiles(ThisFolder, searchFileName)) 'Process searchFileName
                Catch ex As Exception                                           'For simplicity sake
                    'We'll ignore the errors
                End Try
            Loop                                                                'Process next folder in the stack
            Return ReturnedData                                                 'Return the list of files that match
        End Function
    
    End Class
    Dear Mr. .paul.
    thank you very much for the code you provided
    it's going so perfectly
    I want to ask how to make a fit image in expicturebox 1-4?
    thanks
    roy88
    Last edited by roy88; Oct 27th, 2021 at 10:34 AM.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: need help for multi display picturebox when selecting datagridview in vb.net

    Set the exPictureBox SizeMode to StretchImage

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: need help for multi display picturebox when selecting datagridview in vb.net

    Quote Originally Posted by .paul. View Post
    Set the exPictureBox SizeMode to StretchImage
    Dear Mr. .paul.
    thank you very much. You are my teacher. Sorry I'm late to reply. I mark my post resolved and rate my post to you.
    Thanks
    roy88

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width