hi, i'm new to vb.net.is it possible to link item in drop down list to image? for example if i click "computer" in the drop down list, the image of the computer will appear. sorry if my question sounds silly. i'm really a newbie in vb.thanks for ur help.

Recommended Answers

All 9 Replies

yes it is...

u have to use the "selected index change" event for ur combobox..

then you can disappear and reappear any image for example :

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

if combobox1.selecteditem.tostring = "Computer" then

picturebox1.visible=false

else if

combobox1.selecteditem.tostring = "Keyboard" then

picturebox2.visible=false

end if

End Sub

you can use Case statement in comboboxselectedindexchanged event

Since "new" to vb.net, see if this helps.
1 ListBox, 1 PictureBox
...and a Folder with a few images.

Public Class Form1
    '// set this Folder to your ImagesFolder.
    Private myCoolImgsFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\.TEMP\just.because\"

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        With PictureBox1 '// shorten code:) and set some Properties for the PictureBox.
            .BackColor = Color.Black
            .SizeMode = PictureBoxSizeMode.Zoom
        End With
        '// load Files if the Folder.Exists.
        If IO.Directory.Exists(myCoolImgsFolder) Then
            loadMyCoolFilesToListBox(myCoolImgsFolder, ListBox1)
        Else
            MsgBox("Images Folder Does Not Exist.", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub loadMyCoolFilesToListBox(ByVal selCoolFolder As String, ByVal selListBox As ListBox, Optional ByVal selFileExtension As String = "*.*")
        For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                                    (selCoolFolder, FileIO.SearchOption.SearchTopLevelOnly, selFileExtension)
            selListBox.Items.Add(IO.Path.GetFileName(myCoolFile)) '// add file name with extension.
        Next
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        With ListBox1
            If Not .SelectedIndex = -1 Then '// If item selected.
                PictureBox1.ImageLocation = myCoolImgsFolder & .SelectedItem.ToString '// load image from Folder.
                Me.Text = .SelectedItem.ToString '// just.because :)
            End If
        End With
    End Sub
End Class

If you only need to load a certain file.extension type, you can use something as: loadMyCoolFilesToListBox(myCoolImgsFolder, ListBox1, "*.png") , otherwise it will load all files by default.

Member Avatar for Unhnd_Exception

Just Because

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim FolderPath As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\New Folder"
    If IO.Directory.Exists(FolderPath) Then
        ComboBox1.DisplayMember = "Name" 'Filename with extension
        ComboBox1.ValueMember = "FullName" 'Full File Name
        ComboBox1.FormattingEnabled = True
        'Set data source to all files in the FolderPath
        ComboBox1.DataSource = New IO.DirectoryInfo(FolderPath).GetFiles
    End If
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
   If ComboBox1.SelectedValue IsNot Nothing Then
        'Load the image from the full file path
        PictureBox1.ImageLocation = CStr(ComboBox1.SelectedValue)
   End If
End Sub

Private Sub ComboBox1_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ListControlConvertEventArgs) Handles ComboBox1.Format
    'Remove the file extension from the name
    e.Value = CStr(e.Value).TrimEnd(CType(e.ListItem, IO.FileInfo).Extension.ToCharArray)
End Sub
commented: i'm a keep an eye on this one :D;nicely executed:) +12

yes it is...

u have to use the "selected index change" event for ur combobox..

then you can disappear and reappear any image for example :

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

if combobox1.selecteditem.tostring = "Computer" then

picturebox1.visible=false

else if

combobox1.selecteditem.tostring = "Keyboard" then

picturebox2.visible=false

end if

End Sub

for this, i need to use drop down list, so i change the code a bit. this is example of my code

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

    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        If DropDownList1.SelectedItem.ToString = "Computer" Then


            Image1.Visible = False



        ElseIf DropDownList1.SelectedItem.ToString = "Keyboard" Then


            Image2.Visible = False


        End If

from what I understand, if i choose computer from the list, then image 2 will be visible instead of image 1 and if if i choose keyboard, image 1 will be visible instead of image 2. but why doesn't it works? both images appeared no matter which item i choose.is something wrong with my coding?or i can't use it for drop down list?

Since "new" to vb.net, see if this helps.
1 ListBox, 1 PictureBox
...and a Folder with a few images.

Public Class Form1
    '// set this Folder to your ImagesFolder.
    Private myCoolImgsFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\.TEMP\just.because\"

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        With PictureBox1 '// shorten code:) and set some Properties for the PictureBox.
            .BackColor = Color.Black
            .SizeMode = PictureBoxSizeMode.Zoom
        End With
        '// load Files if the Folder.Exists.
        If IO.Directory.Exists(myCoolImgsFolder) Then
            loadMyCoolFilesToListBox(myCoolImgsFolder, ListBox1)
        Else
            MsgBox("Images Folder Does Not Exist.", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub loadMyCoolFilesToListBox(ByVal selCoolFolder As String, ByVal selListBox As ListBox, Optional ByVal selFileExtension As String = "*.*")
        For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                                    (selCoolFolder, FileIO.SearchOption.SearchTopLevelOnly, selFileExtension)
            selListBox.Items.Add(IO.Path.GetFileName(myCoolFile)) '// add file name with extension.
        Next
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        With ListBox1
            If Not .SelectedIndex = -1 Then '// If item selected.
                PictureBox1.ImageLocation = myCoolImgsFolder & .SelectedItem.ToString '// load image from Folder.
                Me.Text = .SelectedItem.ToString '// just.because :)
            End If
        End With
    End Sub
End Class

If you only need to load a certain file.extension type, you can use something as: loadMyCoolFilesToListBox(myCoolImgsFolder, ListBox1, "*.png") , otherwise it will load all files by default.

I need to use drop down list. i try to change ur code, but as you can see, with my limited knowledge, there's nothing much that i can do.do you have any suggestion on how i can change ur code so i can use drop down list instead of list box?

Just Because

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim FolderPath As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\New Folder"
    If IO.Directory.Exists(FolderPath) Then
        ComboBox1.DisplayMember = "Name" 'Filename with extension
        ComboBox1.ValueMember = "FullName" 'Full File Name
        ComboBox1.FormattingEnabled = True
        'Set data source to all files in the FolderPath
        ComboBox1.DataSource = New IO.DirectoryInfo(FolderPath).GetFiles
    End If
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
   If ComboBox1.SelectedValue IsNot Nothing Then
        'Load the image from the full file path
        PictureBox1.ImageLocation = CStr(ComboBox1.SelectedValue)
   End If
End Sub

Private Sub ComboBox1_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ListControlConvertEventArgs) Handles ComboBox1.Format
    'Remove the file extension from the name
    e.Value = CStr(e.Value).TrimEnd(CType(e.ListItem, IO.FileInfo).Extension.ToCharArray)
End Sub

I try to change ur code but honestly, i dun even know where to start. is there any way i can alter the code so that i can use it in my drop down list code?

hi, I' ve managed to make the images appear as intended after i make AutoPostBack = True.In this coding, basically, it hides the other image while it shows the image that correspond to the item selected in the drop down list. example, if i select "computer", it will show the computer's image while it hides the keyboard's image. Is it possible to make the images appear in the same position? Here's my code

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

        If DropDownList1.SelectedItem.ToString = "Computer" Then
            Image1.Visible = True

        ElseIf DropDownList1.SelectedItem.ToString = "Eraser" Then
            Image2.Visible = True

        
        End If


    End Sub

or is there another way of doing it?thanks for ur help.

hi, i've managed to solve it.this is my code

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



    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        If DropDownList1.SelectedItem.ToString = "Computer" Then

            Image1.ImageUrl = "http://localhost/LoadPicture/Computer.bmp"
            

        ElseIf DropDownList1.SelectedItem.ToString = "Eraser" Then

            Image1.ImageUrl = "http://localhost/LoadPicture/Eraser.bmp"
            

     

        End If




    End Sub

thanks guys! i really appreciate ur help! ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.