Question 5 from instructions I need help display the appropriate symbol in the picture box when that country is selected in the list box and currency is displayed in the label box.

Burke, Brendan J 141 Reputation points
2022-03-12T02:18:42.913+00:00

![182380-wk9-c5p-instructions.jpg][1]182349-main-form.pdf

182405-overlapping-images.jpg182396-nonoverlapping-images.jpg

The first link is the code and the 2nd and third is so you know what the images look like when they are overlapping and not overlapping. I just need the code to display the currency symbol in each box whenever you click that country it belongs to in the list box and the currency name appears in the label box. The first symbol in the picture box the one you see is called in the image called overlapping images is first followed by pound symbol for England, Rupee symbol for India, Rupel symbol for Russia then the Dollar symbol for the United States. Is there anyway you could help me display the currency symbol for that country in the picture box when the country is selected in the list box and the currency is displayed in the label.

[1]: /api/attachments/182380-wk9-c5p-instructions.jpg?platform=QnA "Question number 5 I need help with"

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,386 Reputation points
    2022-03-12T13:29:21.757+00:00

    Since this is some form of homework I will give you code in C#.

    • One listBox named CountryListBox
    • One PictureBox

    Create a folder named images, place your images in there and follow the code below. This way you have direction.

    182397-figure1.png

    using System;  
    using System.Collections.Generic;  
    using System.Drawing;  
    using System.IO;  
    using System.Linq;  
    using System.Windows.Forms;  
      
    namespace CountryImages  
    {  
        /// <summary>  
        /// Create a folder named Images, place each country image in this folder  
        /// Add an blank image named 0.bmp or 0.png, set copy to output directory  
        /// to copy if newer  
        ///  
        /// Operation class in VB.NET would be a code module  
        ///  
        /// Each class should be in a separate file.  
        ///   
        /// </summary>  
        public partial class Form1 : Form  
        {  
            private readonly BindingSource bindingSource = new BindingSource();  
            public Form1()  
            {  
                InitializeComponent();  
                Shown += OnShown;  
            }  
      
            private void OnShown(object sender, EventArgs e)  
            {  
                bindingSource.DataSource = Operations.GetCountries();  
                CountryListBox.DataSource = bindingSource;  
                pictureBox1.DataBindings.Add("Image", bindingSource, "Image");  
            }  
        }  
      
        public class Country  
        {  
            public string Name { get; set; }  
            public Image Image { get; set; }  
            public override string ToString() => Name;  
        }  
      
        public class Operations  
        {  
            public static List<Country> GetCountries()  
            {  
                List<Country> list = new List<Country>();  
                var files = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images"));  
      
                foreach (var file in files)  
                {  
                    Country country = new Country  
                    {  
                        Name = Path.GetFileNameWithoutExtension(file),  
                        Image = Image.FromFile(file)  
                    };  
                    list.Add(country);  
                }  
      
                list.FirstOrDefault().Name = "Select";  
      
                return list;  
            }  
        }  
    }  
      
    

    And note the creating the list above can be done in less code

    public class Operations  
    {  
        public static List<Country> GetCountries()  
        {  
            var files = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images"));  
      
            List<Country> list = files.Select(file => new Country { Name = Path.GetFileNameWithoutExtension(file), Image = Image.FromFile(file) }).ToList();  
      
            list.FirstOrDefault().Name = "Select";  
      
            return list;  
        }  
    }  
    

  2. LesHay 7,126 Reputation points
    2022-03-12T18:39:12.387+00:00

    Hi
    Although you are being very specific with your requests, it is really up to you to take what information is given and adapt for your use as needed. This forum is not a software writing service. If you get information supplied then study it and see if you can use the various methods shown etc.
    Here is another version, a combination from the code posted by Karen, and bits and pieces from other applications I have here. This is a complete example for testing purposes, you can run this as a new Project to try it. There is a PictureBox1, Label1 and a ComboBox named CountryListBox1.

    See if any of this or other code can help you.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim BS As New BindingSource()
        Dim CurrDic As New Dictionary(Of String, String)
    
        ' I have some Images in this folder
        ' change as needed
        Dim imPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Images")
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            With CurrDic
                .Add("Australia", "dollar")
                .Add("Brazil", "real")
                .Add("Canada", "dollar")
                .Add("Chile", "peso")
                .Add("China", "yuan")
                .Add("China2", "renminbi")
                .Add("Czech Republic", "koruna")
                .Add("Denmark", "krone")
                .Add("France", "euro")
                .Add("Germany", "euro")
                .Add("Greece", "euro")
                .Add("HongKong", "dollar")
                .Add("Hungary", "forint")
                .Add("India", "rupee")
                .Add("Indonesia", "rupiah")
                .Add("Israel", "newshekel")
                .Add("Italy", "euro")
                .Add("Japan", "Yen")
                .Add("Malaysia", "ringgit")
                .Add("Mexico", "peso")
                .Add("Netherlands", "euro")
                .Add("NewZealand", "dollar")
                .Add("Norway", "krone")
                .Add("Pakistan", "rupee")
                .Add("Philippines", "peso")
                .Add("Poland", "zloty")
                .Add("Russia", "ruble")
                .Add("Saudi Arabia", "riyal")
                .Add("Singapore", "dollar")
                .Add("SouthAfrica", "rand")
                .Add("SouthKorea", "won")
                .Add("Spain", "euro")
                .Add("Sweden", "krona")
                .Add("Switzerland", "franc")
                .Add("Taiwan", "dollar")
                .Add("Turkey", "lira")
                .Add("United Kingdom", "pound sterling")
                .Add("Great Britain", "pound sterling")
                .Add("GB", "pound sterling")
                .Add("UK", "pound sterling")
                .Add("United States", "dollar")
                .Add("USA", "dollar")
            End With
            BS.DataSource = Countries()
            CountryListBox1.DataSource = BS
            PictureBox1.DataBindings.Add("Image", BS, "Image")
            Label1.DataBindings.Add("Text", BS, "Currency")
            CountryListBox1.SelectedIndex = -1
        End Sub
        Public Function ResizeIm(im As Image) As Image
            ' this will resize image to fit the PictureBox
            Return New Bitmap(im, PictureBox1.Size)
        End Function
        Function GetCurr(country As String) As String
            Return CurrDic(country)
        End Function
        Public Function Countries() As List(Of Country)
            Dim list As New List(Of Country)
            Dim files() As String = IO.Directory.GetFiles(imPath)
            For Each File As String In files
                Dim country As New Country
                With country
                    .Name = IO.Path.GetFileNameWithoutExtension(File)
                    .Image = ResizeIm(Image.FromFile(File))
                    .Currency = GetCurr(.Name)
                End With
                list.Add(country)
            Next
            Return list
        End Function
    End Class
    Public Class Country
        Property Name As String
        Property Image As Image
        Property Currency As String
        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class
    
    0 comments No comments