update an image with a value from a listbox RRS feed

  • Question

  • I've seen this question asked several times and the answers are always 'code a finite list' for the listbox source.  Here's what I want to do, and why I cannot do this.

    I have a table called products, and a query based on that table.  The query is the source for the values in the listbox and has 4 fields: ID, SKU, Description and PictureFile (which is the path and filename of a picture).  There is a textbox that you can type in and select part of the description (like:  boat) and then click a button to search.  This then reruns the query with LIKE "*"&txtBox&"*" to give an updated list where 'boat' is in the description.  If there are 4 boats, I want the user to be able to select one of the 4 boats in the listbox and update an image outside of the listbox to reflect the picture stored in the PictureFile field.  I think where I have gone wrong at this part is the source for the image, so perhaps I need to build some sort of expression in there to select the selected PictureFile that is currently in the listbox?  Or would this be just much easier with a click event in the listbox to force the image (named outputImage) to be updated by the selected line (I am not allowing multi-selects in the listbox).

    Thursday, December 12, 2019 12:17 PM

All replies

  • Hello,

    • Is this MS-Access?
    • How are you interacting with the database e.g. Entity Framework, DataAdapter, TableAdapter, just using a connection and command?

    You might consider sharing current code for those who are here to help understand the code better by seeing the code.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Thursday, December 12, 2019 2:33 PM
  • Hi

    Here is a stand alone example which may provide some help. This example will filter and update the image in the PictureBox dynamically as User types into TextBox1. I have included a DataGridView for ease of seeing what is happening (choosing a row in DGV also acts on the PictureBox). The PictureBox, ListBox and the DGV update dynamically as User changes the text in TextBox1.

    Probably not what you are wanting, but might offer some pointers.

    ' Stand alone example
    ' Form1 with DataGridView1,
    ' TextBox1, ListBox1, PictureBox1
    ' no Exception handling included
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim Products As New DataTable("Freddy")
    	Dim bs As New BindingSource
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With Products
    			.Columns.Add("ID", GetType(Integer))
    			.Columns.Add("SKU", GetType(String))
    			.Columns.Add("Description", GetType(String))
    			.Columns.Add("Picture", GetType(String))
    			' adding some test data
    			.Rows.Add(1, "SKU 1", "A new boat is on order for you", "C:\Users\lesha\Desktop\Plans\Dice 2\00.png")
    			.Rows.Add(2, "SKU 2", "Any boat floats unless it is a submarine!", "C:\Users\lesha\Desktop\Plans\Dice 2\14.png")
    			.Rows.Add(3, "SKU 3", "A new car is on order as well (for me)", "C:\Users\lesha\Desktop\Plans\Dice 2\24.png")
    			.Rows.Add(4, "SKU 4", "When a suitcase just won't do!", "C:\Users\lesha\Desktop\Plans\Dice 2\34.png")
    			.Rows.Add(5, "SKU 5", "Boats tend to sink when full of holes.", "C:\Users\lesha\Desktop\Plans\Dice 2\44.png")
    		End With
    		bs.DataSource = Products
    		DataGridView1.DataSource = bs
    		With ListBox1
    			.DataSource = bs
    			.DisplayMember = "Description"
    		End With
    		PictureBox1.Image = Nothing
    	End Sub
    	Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    		If TextBox1.Text = Nothing Then
    			Exit Sub
    		End If
    		bs.Filter = "Description LIKE '*" & TextBox1.Text & "*'"
    	End Sub
    	Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    		With PictureBox1
    			.SizeMode = PictureBoxSizeMode.Zoom
    			.Image = Image.FromFile(Products(bs.Position).Item("Picture").ToString)
    		End With
    	End Sub
    End Class

    Regards Les, Livingston, Scotland

    Thursday, December 12, 2019 5:41 PM
  • Hi Keith Paschal,

    Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao 

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, December 24, 2019 7:59 AM