954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

drop down list

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.

tenshi
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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
bilal_fazlani
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 10
Solved Threads: 3
 

you can use Case statement in comboboxselectedindexchanged event

Pgmer
Master Poster
714 posts since Apr 2008
Reputation Points: 54
Solved Threads: 121
 

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.

codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 


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
Unhnd_Exception
Posting Pro
571 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

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?

tenshi
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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?

tenshi
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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?

tenshi
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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.

tenshi
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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! ^_^

tenshi
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: