Share via


Select items in a Listbox and display a picture how ?

Question

Sunday, April 10, 2011 9:35 PM

Okay so I have a Textbox a button a listbox and a picturebox.

This is what I want to happen.

I want to type Balloon into the TextBox, then press the button.
Then I want the listbox to display 2 items. Balloon1 and Balloon2.

When you select Balloon1 in the listbox I want the picturebox to change to a picture of a balloon. When you selected Balloon2 I want the picturebox to change to another picture.

Seems simple,

 

If textbox1.text = Balloon then
Listbox1.items.add ("Balloon1")
Listbox1.items.add ("Balloon2")

Thats pretty much as far as I got.

The pictures I want the picturebox to change to is at this directory.
My.resources.Baloon1 &  My.resources.Baloon1

All replies (2)

Monday, April 11, 2011 12:40 AM ✅Answered

Hi,

Do you realise every time you type Balloon in the TextBox that you will get 2 items added in the ListBox with what you suggest?

Why not just add;

  • Balloon1
  • Balloon2

 

to a ListBox once or better still use a ComboBox.

Then when you select the appropriate item then the relevant picture is shown.

Public Class Form1

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

    PictureBox1.BackgroundImageLayout = ImageLayout.Zoom
    Listbox1.items.add("Balloon1")
    Listbox1.items.add("Balloon2")

  End Sub

  Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    If ListBox1.SelectedItem.ToString = "Balloon1" Then
      PictureBox1.BackgroundImage = Image.FromFile(My.Resources.Balloon1)
    ElseIf ListBox1.SelectedItem.ToString = "Balloon2" Then
      PictureBox1.BackgroundImage = Image.FromFile(My.Resources.Balloon2)
    End If

  End Sub
End Class
Regards, John

Click this link to see how to insert a picture into a forum post.

Installing VB6 on Windows 7


Monday, April 11, 2011 12:48 AM ✅Answered

try this:

 

Public Class Form1

  Private Structure detail
    Dim description As String
    Dim imageName As String
    Public Overrides Function ToString() As String
      Return Me.imageName
    End Function
  End Structure
  Private details As New List(Of detail)

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'i added 8 icons to my.resources
    'the description property is what you check against in your textbox
    '+ the imageName is the name of the icons in my.resources
    details.Add(New detail With {.description = "Arrow", .imageName = "Arrow_Down"})
    details.Add(New detail With {.description = "Arrow", .imageName = "Arrow_Up"})
    details.Add(New detail With {.description = "Arrow", .imageName = "Arrow_Left"})
    details.Add(New detail With {.description = "Arrow", .imageName = "Arrow_Right"})
    details.Add(New detail With {.description = "Symbol", .imageName = "Symbol_Add"})
    details.Add(New detail With {.description = "Symbol", .imageName = "Symbol_Check"})
    details.Add(New detail With {.description = "Symbol", .imageName = "Symbol_Delete"})
    details.Add(New detail With {.description = "Symbol", .imageName = "Symbol_Error"})
  End Sub

  Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    If details.Where(Function(d) d.description = TextBox1.Text).Count > 0 Then
      ListBox1.DataSource = details.Where(Function(d) d.description = TextBox1.Text).ToArray
    Else
      ListBox1.DataSource = Nothing
    End If
  End Sub

  Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    If ListBox1.SelectedIndex > -1 Then
      PictureBox1.Image = DirectCast(My.Resources.ResourceManager.GetObject(ListBox1.Text), Icon).ToBitmap
    Else
      PictureBox1.Image = Nothing
    End If
  End Sub

End Class

thanks for any help