none
Search in imagelist and show in picturebox RRS feed

  • Question

  • I have a Imagelist4 with 70 country flags.

    I want to use this to show in Picturebox20.

     If ComboBox37.Text = ("Nederland") Then
                PictureBox20.Image = ImageList4.Images(47)
            Else
                PictureBox20.Image = Nothing
            End If
    This works , but is there a beter way to do this . Because else i have to do this 70 times.

    Maybe to search in this list with the country's and then find the image index ? 

    How do i combine this , and where do i put this list 

     
            "Antiqua and Barbuda",1, ImageIndex = 0})
            "Albanië",2, ImageIndex = 1})
            "Andorra",3, ImageIndex = 2})
            "Australia",4, .ImageIndex = 3})
            "Bahamas",5, .ImageIndex = 4})
            "Bahrain",6, .ImageIndex = 5})
            "Barbados",7, .ImageIndex = 6})
            "Belgium",8, .ImageIndex = 7})
            "Bermuda",9, .ImageIndex = 8})
            "Bosnia and Herzegovinia",10, .ImageIndex = 9})
            "Bulgaria",11, .ImageIndex = 10})
            "Canada",12, .ImageIndex = 11})
            "Cayman Islands",13, .ImageIndex = 12})
            "China",14, .ImageIndex = 13})
            "Cypres",15, .ImageIndex = 14})
            "Denmark",16, .ImageIndex = 15})
            "Estonia",17, .ImageIndex = 16})
            "Faroe Islands",18, .ImageIndex = 17})
            "Finland",19, .ImageIndex = 18})
            "France",20, .ImageIndex = 19})
            "Germany",21, .ImageIndex = 20})
            "Georgia",22, .ImageIndex = 21})
            "Gibraltar",23, .ImageIndex = 22})
            "Greece",24, .ImageIndex = 23})
            "Hong Kong",25, .ImageIndex = 24})
            "Hungary",26, .ImageIndex = 25})
            "Iceland",27, .ImageIndex = 26})
            "India",28, .ImageIndex = 27})
            "Indonesia",29, .ImageIndex = 28})
            "Iran",30, .ImageIndex = 29})
            "Ireland",31, .ImageIndex = 30})
            "Isle of Man",32, .ImageIndex = 31})
            "Italy",33, .ImageIndex = 32})
            "Japan",34, .ImageIndex = 33})
            "Korea",35, .ImageIndex = 34})
            "Kroatië",36, .ImageIndex = 35})
            "Kuwait",37, .ImageIndex = 36})
            "Liberia",38, .ImageIndex = 37})
            "Letland",39, .ImageIndex = 38})
            "Liberia",40, .ImageIndex = 39})
            "Liechtenstein",41, .ImageIndex = 40})
            "Lithuania",42, .ImageIndex = 41})
            "Luxembourg",43, .ImageIndex = 42})
            "Madeira",44, .ImageIndex = 43})
            "Malaysia",45, .ImageIndex = 44})
            "Malta",46, .ImageIndex = 45})
            "Marshall Islands",47, .ImageIndex = 46})
            "Netherlands",48, .ImageIndex = 47})
            "Monaco",49, .ImageIndex = 48})
            "Norway",50, .ImageIndex = 49})
            "Panama",51, .ImageIndex = 50})
            "Philippines",52, .ImageIndex = 51})
            "Poland",53, .ImageIndex = 52})
            "Portugal",54, .ImageIndex = 53})
            "Romania",55, .ImageIndex = 54})
            "Russia",56, .ImageIndex = 55})
            "San Marino",57, .ImageIndex = 56})
            "Slovenia",58, .ImageIndex = 57})
            "Slovakia",59, .ImageIndex = 58})
            "Singapore",60, .ImageIndex = 59})
            "St Vincent & Grenadines",61, .ImageIndex = 60})
            "Spain",62, .ImageIndex = 61})
            "Sweden",63, .ImageIndex = 62})
            "Switzerland",64, .ImageIndex = 63})
            "Thailand",65, .ImageIndex = 64})
            "Tjechië",66, .ImageIndex = 65})
            "Turkey",67, .ImageIndex = 66})
            "Ukraine",68, .ImageIndex = 67})
            "United Kingdom",69, .ImageIndex = 68})
            "United States",70, .ImageIndex = 69})
            "Vietnam",71, .ImageIndex = 70})
     

    Wednesday, August 15, 2018 8:06 PM

Answers

  • Hi,

    Is this the code you need?I hope it can help you.

    Public Class Form1
        Dim dict As New Dictionary(Of String, Integer)()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            getlist()
    
        End Sub
        Public Sub getlist()
            dict.Add("Antiqua and Barbuda", 0)
            dict.Add("Albanië", 1)
            dict.Add("Andorra", 2)
            dict.Add("Australia", 3)
            dict.Add("Bahamas", 4)
            dict.Add("Bahrain", 5)
            dict.Add("Barbados", 6)
            dict.Add("Belgium", 7)
            dict.Add("Bermuda", 8)
            dict.Add("Bosnia and Herzegovinia", 9)
            dict.Add("Bulgaria", 10)
            dict.Add("Canada", 11)
            dict.Add("Cayman Islands", 12)
            dict.Add("China", 13)
            dict.Add("Cypres", 14)
            dict.Add("Denmark", 15)
            dict.Add("Estonia", 16)
            dict.Add("Faroe Islands", 17)
            dict.Add("Finland", 18)
            dict.Add("France", 19)
            dict.Add("Germany", 20)
            dict.Add("Georgia", 21)
            dict.Add("Gibraltar", 22)
            dict.Add("Greece", 23)
            dict.Add("Hong Kong", 24)
            dict.Add("Hungary", 25)
            dict.Add("Iceland", 26)
            dict.Add("India", 27)
            dict.Add("Indonesia", 28)
            dict.Add("Iran", 29)
            dict.Add("Ireland", 30)
            dict.Add("Isle of Man", 31)
            dict.Add("Italy", 32)
            dict.Add("Japan", 33)
            dict.Add("Joegoslavenia", 34)
            dict.Add("Korea", 35)
            dict.Add("Kroatië", 36)
            dict.Add("Kuwait", 37)
            dict.Add("Letland", 38)
            dict.Add("Liberia", 39)
            dict.Add("Liechtenstein", 40)
            dict.Add("Lithuania", 41)
            dict.Add("Luxembourg", 42)
            dict.Add("Madeira", 43)
            dict.Add("Malaysia", 44)
            dict.Add("Malta", 45)
            dict.Add("Marshall Islands", 46)
            dict.Add("Netherlands", 47)
            dict.Add("Monaco", 48)
            dict.Add("Norway", 49)
            dict.Add("Panama", 50)
            dict.Add("Philippines", 51)
            dict.Add("Poland", 52)
            dict.Add("Portugal", 53)
            dict.Add("Romania", 54)
            dict.Add("Russia", 55)
            dict.Add("San Marino", 56)
            dict.Add("Slovenia", 57)
            dict.Add("Slovakia", 58)
            dict.Add("Singapore", 59)
            dict.Add("St Vincent & Grenadines", 60)
            dict.Add("Spain", 61)
            dict.Add("Sweden", 62)
            dict.Add("Switzerland", 63)
            dict.Add("Thailand", 64)
            dict.Add("Tjechië", 65)
            dict.Add("Turkey", 66)
            dict.Add("Ukraine", 67)
            dict.Add("United Kingdom", 68)
            dict.Add("United States", 69)
            dict.Add("Vietnam", 70)
            ComboBox1.DataSource = New BindingSource(dict, Nothing)
            ComboBox1.ValueMember = "Key"
        End Sub
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim num As Integer = dict.Item(ComboBox1.Text)
            If dict.ContainsKey(ComboBox1.Text) Then
                PictureBox1.Image = ImageList1.Images(num)
            End If
        End Sub
    End Class
    

    Best Regards,

    Alex


    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 MSDNFSF@microsoft.com.

    • Marked as answer by KeesBlunder Thursday, August 16, 2018 2:39 PM
    Thursday, August 16, 2018 9:53 AM

All replies

  • Something like this

        Private Sub ImageExmaple_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each Itm As String In ImageList1.Images.Keys
                ComboBox1.Items.Add(Itm)
            Next
        End Sub
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            PictureBox1.Image = ImageList1.Images(ComboBox1.Text)
        End Sub


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi


    • Edited by Gtripodi Wednesday, August 15, 2018 8:21 PM
    Wednesday, August 15, 2018 8:20 PM
  • Thanks for the reply Gtripodi,

    It is showing the name of the flags and when i select one show them in the picturebox.

    But in the combobox it is showing the name's of the flags. 

    For example

    vlagsaintvincentengrenadines.gif
    flag_san_marino.png

    It has to be 

    saintvincentengrenadines
    san marino
    Can this be changed ?

    Wednesday, August 15, 2018 8:43 PM
  • Seeing from prior post you are using a database why not store them there, read them into a list. Set the list to the data source of the ComboBox and get the image and assign the image to the PictureBox?

    Demo class

    Public Class CountryItem
        Public Property Index As Integer
        Public Property Name As String
        Public Property Image As Image
    End Class
    
    Read from database to a list. Then set the list to the ComboBox. Change the image as needed e.g.
    If ComboBox1.SelectedIndex > 0 Then
        Dim item As CountryItem = CType(ComboBox1.SelectedItem, CountryItem)
        ' set picture box image to item.Image
    End If


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Alex Li-MSFT Thursday, August 16, 2018 6:22 AM
    Wednesday, August 15, 2018 9:25 PM
    Moderator
  • This is working , don't know if it is the right way but it works.

    Is there a way to put the list in a Sub or Function ? so i make the list 1 time and only change the combobox or textbox number and picturebox

      Dim dict As New Dictionary(Of String, Integer)()
            dict.Add("Antiqua and Barbuda", 0)
            dict.Add("Albanië", 1)
            dict.Add("Andorra", 2)
            dict.Add("Australia", 3)
            dict.Add("Bahamas", 4)
            dict.Add("Bahrain", 5)
            dict.Add("Barbados", 6)
            dict.Add("Belgium", 7)
            dict.Add("Bermuda", 8)
            dict.Add("Bosnia and Herzegovinia", 9)
            dict.Add("Bulgaria", 10)
            dict.Add("Canada", 11)
            dict.Add("Cayman Islands", 12)
            dict.Add("China", 13)
            dict.Add("Cypres", 14)
            dict.Add("Denmark", 15)
            dict.Add("Estonia", 16)
            dict.Add("Faroe Islands", 17)
            dict.Add("Finland", 18)
            dict.Add("France", 19)
            dict.Add("Germany", 20)
            dict.Add("Georgia", 21)
            dict.Add("Gibraltar", 22)
            dict.Add("Greece", 23)
            dict.Add("Hong Kong", 24)
            dict.Add("Hungary", 25)
            dict.Add("Iceland", 26)
            dict.Add("India", 27)
            dict.Add("Indonesia", 28)
            dict.Add("Iran", 29)
            dict.Add("Ireland", 30)
            dict.Add("Isle of Man", 31)
            dict.Add("Italy", 32)
            dict.Add("Japan", 33)
            dict.Add("Joegoslavenia", 34)
            dict.Add("Korea", 35)
            dict.Add("Kroatië", 36)
            dict.Add("Kuwait", 37)
            dict.Add("Letland", 38)
            dict.Add("Liberia", 39)
            dict.Add("Liechtenstein", 40)
            dict.Add("Lithuania", 41)
            dict.Add("Luxembourg", 42)
            dict.Add("Madeira", 43)
            dict.Add("Malaysia", 44)
            dict.Add("Malta", 45)
            dict.Add("Marshall Islands", 46)
            dict.Add("Netherlands", 47)
            dict.Add("Monaco", 48)
            dict.Add("Norway", 49)
            dict.Add("Panama", 50)
            dict.Add("Philippines", 51)
            dict.Add("Poland", 52)
            dict.Add("Portugal", 53)
            dict.Add("Romania", 54)
            dict.Add("Russia", 55)
            dict.Add("San Marino", 56)
            dict.Add("Slovenia", 57)
            dict.Add("Slovakia", 58)
            dict.Add("Singapore", 59)
            dict.Add("St Vincent & Grenadines", 60)
            dict.Add("Spain", 61)
            dict.Add("Sweden", 62)
            dict.Add("Switzerland", 63)
            dict.Add("Thailand", 64)
            dict.Add("Tjechië", 65)
            dict.Add("Turkey", 66)
            dict.Add("Ukraine", 67)
            dict.Add("United Kingdom", 68)
            dict.Add("United States", 69)
            dict.Add("Vietnam", 70)
    
            ' See if this key exists.
            If dict.ContainsKey(ComboBox1.Text) Then
    
                ' Write value of the key.
                Dim num As Integer = dict.Item(ComboBox1.Text)
                PictureBox1.Image = ImageList4.Images(num)
    
            End If
    
    

    Thursday, August 16, 2018 9:30 AM
  • Hi,

    Is this the code you need?I hope it can help you.

    Public Class Form1
        Dim dict As New Dictionary(Of String, Integer)()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            getlist()
    
        End Sub
        Public Sub getlist()
            dict.Add("Antiqua and Barbuda", 0)
            dict.Add("Albanië", 1)
            dict.Add("Andorra", 2)
            dict.Add("Australia", 3)
            dict.Add("Bahamas", 4)
            dict.Add("Bahrain", 5)
            dict.Add("Barbados", 6)
            dict.Add("Belgium", 7)
            dict.Add("Bermuda", 8)
            dict.Add("Bosnia and Herzegovinia", 9)
            dict.Add("Bulgaria", 10)
            dict.Add("Canada", 11)
            dict.Add("Cayman Islands", 12)
            dict.Add("China", 13)
            dict.Add("Cypres", 14)
            dict.Add("Denmark", 15)
            dict.Add("Estonia", 16)
            dict.Add("Faroe Islands", 17)
            dict.Add("Finland", 18)
            dict.Add("France", 19)
            dict.Add("Germany", 20)
            dict.Add("Georgia", 21)
            dict.Add("Gibraltar", 22)
            dict.Add("Greece", 23)
            dict.Add("Hong Kong", 24)
            dict.Add("Hungary", 25)
            dict.Add("Iceland", 26)
            dict.Add("India", 27)
            dict.Add("Indonesia", 28)
            dict.Add("Iran", 29)
            dict.Add("Ireland", 30)
            dict.Add("Isle of Man", 31)
            dict.Add("Italy", 32)
            dict.Add("Japan", 33)
            dict.Add("Joegoslavenia", 34)
            dict.Add("Korea", 35)
            dict.Add("Kroatië", 36)
            dict.Add("Kuwait", 37)
            dict.Add("Letland", 38)
            dict.Add("Liberia", 39)
            dict.Add("Liechtenstein", 40)
            dict.Add("Lithuania", 41)
            dict.Add("Luxembourg", 42)
            dict.Add("Madeira", 43)
            dict.Add("Malaysia", 44)
            dict.Add("Malta", 45)
            dict.Add("Marshall Islands", 46)
            dict.Add("Netherlands", 47)
            dict.Add("Monaco", 48)
            dict.Add("Norway", 49)
            dict.Add("Panama", 50)
            dict.Add("Philippines", 51)
            dict.Add("Poland", 52)
            dict.Add("Portugal", 53)
            dict.Add("Romania", 54)
            dict.Add("Russia", 55)
            dict.Add("San Marino", 56)
            dict.Add("Slovenia", 57)
            dict.Add("Slovakia", 58)
            dict.Add("Singapore", 59)
            dict.Add("St Vincent & Grenadines", 60)
            dict.Add("Spain", 61)
            dict.Add("Sweden", 62)
            dict.Add("Switzerland", 63)
            dict.Add("Thailand", 64)
            dict.Add("Tjechië", 65)
            dict.Add("Turkey", 66)
            dict.Add("Ukraine", 67)
            dict.Add("United Kingdom", 68)
            dict.Add("United States", 69)
            dict.Add("Vietnam", 70)
            ComboBox1.DataSource = New BindingSource(dict, Nothing)
            ComboBox1.ValueMember = "Key"
        End Sub
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim num As Integer = dict.Item(ComboBox1.Text)
            If dict.ContainsKey(ComboBox1.Text) Then
                PictureBox1.Image = ImageList1.Images(num)
            End If
        End Sub
    End Class
    

    Best Regards,

    Alex


    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 MSDNFSF@microsoft.com.

    • Marked as answer by KeesBlunder Thursday, August 16, 2018 2:39 PM
    Thursday, August 16, 2018 9:53 AM
  • Hello Alex,

    I tryed the code but it is giving a error on this line

     Dim num As Integer = dict.Item(ComboBox1.Text)

    Key not found in Dictionary

    An exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll but was not handled in user code


    I am using the 2015 version

    Thursday, August 16, 2018 11:41 AM
  • Thanks Alex,

    Found the error, changed this to.

    Works great .

    Can You change it in the example for the answer. Maybe some one use the code also ?

    Thanks.

      Dim num As Integer = dict.Item(ComboBox1.Text)
            If dict.ContainsKey(ComboBox1.Text) Then
                PictureBox1.Image = ImageList1.Images(num)
            End If

      If dict.ContainsKey(ComboBox1.Text) Then
                Dim num As Integer = dict.Item(ComboBox1.Text)
                PictureBox1.Image = ImageList4.Images(num)
            End If


    • Edited by KeesBlunder Thursday, August 16, 2018 2:41 PM
    Thursday, August 16, 2018 2:38 PM
  • You could of replaced the KeyValues. I asume you type all that dict list out manually?

    Public Class ImageListExample
    
        Private Sub ImageListExample_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
                For i = 0 To ImageList1.Images.Keys.Count - 1
                    If ImageList1.Images.Keys(i) IsNot Nothing Then
                        Dim FLagName As String = ImageList1.Images.Keys(i)
                        If FLagName.Length > 4 Then
                            'Could use substring if .ext is always 4 chars
                            FLagName = FLagName.Substring(0, FLagName.Length - 4)
                            ImageList1.Images.SetKeyName(i, FLagName.Replace("_", " "))
    
                            'Or use an array split on "." (asuming you have no .'s nywhere in filename)
                            'Dim KeyArray() As String = ImageList1.Images.Keys(i).Split("."c)
                            'ImageList1.Images.SetKeyName(i, KeyArray(0).Replace("_", " "))
    
                        End If
                    End If
                Next
    
                For Each ImgKey As String In ImageList1.Images.Keys
                    ComboBox1.Items.Add(ImgKey)
                Next
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            If ComboBox1.SelectedIndex > 0 Then
                PictureBox1.Image = ImageList1.Images(ComboBox1.Text)
            End If
        End Sub
    End Class


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi


    • Edited by Gtripodi Friday, August 17, 2018 3:31 AM
    Friday, August 17, 2018 2:51 AM