VB .NET - Exibindo as bandeiras dos países a partir do nome |
Neste artigo vou mostrar como podemos usar o arquivo de recursos para exibir a bandeira de um país selecionado usando a linguagem VB .NET. |
Ao utilizar imagens e arquivos texto em sua aplicação .NET pode ser mais conveniente embutir estas imagens e arquivos como resources(recursos) no seu projeto, ao invés de deixar as imagens e arquivos texto como arquivos separados e depois tentar localizar e carregar as imagens do disco quando a aplicação estiver em execução.
Neste artigo vamos armazenar imagens dos países em um arquivo de recurso e definir uma classe que herda de Dictionary para recuperar a imagem da bandeira dado o nome do país.
Recursos usados :
Criando o projeto no VS Community
Abra o VS Community 2015 e clique em New Project;
A seguir selecione Visual Basic -> Windows -> Windows Forms Application;
Informe o nome Paises_Bandeiras e clique no botão OK;
A partir da ToolBox inclua os seguintes controles no formulário form1.vb:
1 Combobox - cboPaises
2 Label - Label2 e lblPais
1 PictureBox - picBandeira
Disponha os controles conforme o leiaute da figura abaixo:
No menu PROJECT clique em Add Class e informe o nome Bandeiras;
A seguir vamos definir esta classe como herdando da classe Dictionary.
A classe Dictionary representa uma coleção de chaves e valores. É uma coleção do tipo chave/valor e implementa a interface IDictionary que possui duas coleções no seu interior uma para guardar a chave e outra para guardar o valor.
Para no nosso caso o dicionário irá possuir uma coleção de strings e imagens. Então no construtor da classe Bandeiras defina o código abaixo:
Public Class Bandeiras
Inherits Dictionary(Of String, Image)
Public Sub New()
Me.Add("Afghanistan", My.Resources.Afghanistan)
Me.Add("Albania", My.Resources.Albania)
Me.Add("Algeria", My.Resources.Algeria)
Me.Add("American Samoa", My.Resources.American_Samoa)
Me.Add("Andorra", My.Resources.Andorra)
Me.Add("Angola", My.Resources.Angola)
Me.Add("Anguilla", My.Resources.Anguilla)
Me.Add("Antigua and Barbuda", My.Resources.Antigua_and_Barbuda)
Me.Add("Argentina", My.Resources.Argentina)
Me.Add("Armenia", My.Resources.Armenia)
Me.Add("Aruba", My.Resources.Aruba)
Me.Add("Australia", My.Resources.Australia)
Me.Add("Austria", My.Resources.Austria)
Me.Add("Azerbaijan", My.Resources.Azerbaijan)
Me.Add("Bahamas", My.Resources.Bahamas)
Me.Add("Bahrain", My.Resources.Bahrain)
Me.Add("Bangladesh", My.Resources.Bangladesh)
Me.Add("Barbados", My.Resources.Barbados)
Me.Add("Belarus", My.Resources.Belarus)
Me.Add("Belgium", My.Resources.Belgium)
Me.Add("Belize", My.Resources.Belize)
Me.Add("Benin", My.Resources.Benin)
Me.Add("Bermuda", My.Resources.Bermuda)
Me.Add("Bhutan", My.Resources.Bhutan)
Me.Add("Bolivia", My.Resources.Bolivia)
Me.Add("Bosnia", My.Resources.Bosnia)
Me.Add("Botswana", My.Resources.Botswana)
Me.Add("Brazil", My.Resources.Brazil)
Me.Add("British Virgin Islands", My.Resources.British_Virgin_Islands)
Me.Add("Brunei", My.Resources.Brunei)
Me.Add("Bulgaria", My.Resources.Bulgaria)
Me.Add("Burkina Faso", My.Resources.Burkina_Faso)
Me.Add("Burundi", My.Resources.Burundi)
Me.Add("Cambodia", My.Resources.Cambodia)
Me.Add("Cameroon", My.Resources.Cameroon)
Me.Add("Canada", My.Resources.Canada)
Me.Add("Cape Verde", My.Resources.Cape_Verde)
Me.Add("Cayman Islands", My.Resources.Cayman_Islands)
Me.Add("Central African Republic", My.Resources.Central_African_Republic)
Me.Add("Chad", My.Resources.Chad)
Me.Add("Chile", My.Resources.Chile)
Me.Add("China", My.Resources.China)
Me.Add("Christmas Islands", My.Resources.Christmas_Island)
Me.Add("Colombia", My.Resources.Colombia)
Me.Add("Comoros", My.Resources.Comoros)
Me.Add("Cook Islands", My.Resources.Cook_Islands)
Me.Add("Costa Rica", My.Resources.Costa_Rica)
Me.Add("Croatia", My.Resources.Croatia)
Me.Add("Cuba", My.Resources.Cuba)
Me.Add("Cyprus", My.Resources.Cyprus)
Me.Add("Czech Republic", My.Resources.Czech_Republic)
Me.Add("Democratic Republic of the Congo", My.Resources.Democratic_Republic_of_the_Congo)
Me.Add("East Timor", My.Resources.East_Timor)
Me.Add("Ecuador", My.Resources.Ecuador)
Me.Add("Egypt", My.Resources.Egypt)
Me.Add("El Salvador", My.Resources.El_Salvador)
Me.Add("Equatorial Guinea", My.Resources.Equatorial_Guinea)
Me.Add("Eritrea", My.Resources.Eritrea)
Me.Add("Estonia", My.Resources.Estonia)
Me.Add("Ethiopia", My.Resources.Ethiopia)
Me.Add("Falkland Islands", My.Resources.Falkland_Islands)
Me.Add("Faroe Islands", My.Resources.Faroe_Islands)
Me.Add("Fiji", My.Resources.Fiji)
Me.Add("Finland", My.Resources.Finland)
Me.Add("France", My.Resources.France)
Me.Add("French Polynesia", My.Resources.French_Polynesia)
Me.Add("Gabon", My.Resources.Gabon)
Me.Add("Gambia", My.Resources.Gambia)
Me.Add("Georgia", My.Resources.Georgia)
Me.Add("Germany", My.Resources.Germany)
Me.Add("Ghana", My.Resources.Ghana)
Me.Add("Gibraltar", My.Resources.Gibraltar)
Me.Add("Greece", My.Resources.Greece)
Me.Add("Greenland", My.Resources.Greenland)
Me.Add("Grenada", My.Resources.Grenada)
Me.Add("Guam", My.Resources.Guam)
Me.Add("Guatemala", My.Resources.Guatemala)
Me.Add("Guinea", My.Resources.Guinea)
Me.Add("Guinea Bissau", My.Resources.Guinea_Bissau)
Me.Add("Guyana", My.Resources.Guyana)
Me.Add("Haiti", My.Resources.Haiti)
Me.Add("Honduras", My.Resources.Honduras)
Me.Add("Hong Kong", My.Resources.Hong_Kong)
Me.Add("Hungary", My.Resources.Hungary)
Me.Add("Iceland", My.Resources.Iceland)
Me.Add("India", My.Resources.India)
Me.Add("Indonesia", My.Resources.Indonesia)
Me.Add("Iran", My.Resources.Iran)
Me.Add("Iraq", My.Resources.Iraq)
Me.Add("Ireland", My.Resources.Ireland)
Me.Add("Israel", My.Resources.Israel)
Me.Add("Italy", My.Resources.Italy)
Me.Add("Ivory Coast", My.Resources.Ivory_Coast)
Me.Add("Jamaica", My.Resources.Jamaica)
Me.Add("Japan", My.Resources.Japan)
Me.Add("Jordan", My.Resources.Jordan)
Me.Add("Kazakhstan", My.Resources.Kazakhstan)
Me.Add("Kenya", My.Resources.Kenya)
Me.Add("Kiribati", My.Resources.Kiribati)
Me.Add("Kuwait", My.Resources.Kuwait)
Me.Add("Kyrgyzstan", My.Resources.Kyrgyzstan)
Me.Add("Laos", My.Resources.Laos)
Me.Add("Latvia", My.Resources.Latvia)
Me.Add("Lebanon", My.Resources.Lebanon)
Me.Add("Lesotho", My.Resources.Lesotho)
Me.Add("Liberia", My.Resources.Liberia)
Me.Add("Libya", My.Resources.Libya)
Me.Add("Liechtenstein", My.Resources.Liechtenstein)
Me.Add("Lithuania", My.Resources.Lithuania)
Me.Add("Luxembourg", My.Resources.Luxembourg)
Me.Add("Macao", My.Resources.Macao)
Me.Add("Macedonia", My.Resources.Macedonia)
Me.Add("Madagascar", My.Resources.Madagascar)
Me.Add("Malawi", My.Resources.Malawi)
Me.Add("Malaysia", My.Resources.Malaysia)
Me.Add("Maldives", My.Resources.Maldives)
Me.Add("Mali", My.Resources.Mali)
Me.Add("Malta", My.Resources.Malta)
Me.Add("Marshall Islands", My.Resources.Marshall_Islands)
Me.Add("Martinique", My.Resources.Martinique)
Me.Add("Mauritania", My.Resources.Mauritania)
Me.Add("Mexico", My.Resources.Mexico)
Me.Add("Micronesia", My.Resources.Micronesia)
Me.Add("Moldova", My.Resources.Moldova)
Me.Add("Monaco", My.Resources.Monaco)
Me.Add("Mongolia", My.Resources.Mongolia)
Me.Add("Montserrat", My.Resources.Montserrat)
Me.Add("Morocoo", My.Resources.Morocco)
Me.Add("Mozambique", My.Resources.Mozambique)
Me.Add("Myanmar", My.Resources.Myanmar)
Me.Add("Namibia", My.Resources.Namibia)
Me.Add("Nauru", My.Resources.Nauru)
Me.Add("Nepal", My.Resources.Nepal)
Me.Add("Netherlands", My.Resources.Netherlands)
Me.Add("Netherlands Antilles", My.Resources.Netherlands_Antilles)
Me.Add("New Zealand", My.Resources.New_Zealand)
Me.Add("Nicaragua", My.Resources.Nicaragua)
Me.Add("Niger", My.Resources.Niger)
Me.Add("Nigeria", My.Resources.Nigeria)
Me.Add("Niue", My.Resources.Niue)
Me.Add("Norfolk Island", My.Resources.Norfolk_Island)
Me.Add("North Korea", My.Resources.North_Korea)
Me.Add("Norway", My.Resources.Norway)
Me.Add("Oman", My.Resources.Oman)
Me.Add("Pakistan", My.Resources.Pakistan)
Me.Add("Palau", My.Resources.Palau)
Me.Add("Panama", My.Resources.Panama)
Me.Add("Papua New Guinea", My.Resources.Papua_New_Guinea)
Me.Add("Paraguay", My.Resources.Paraguay)
Me.Add("Peru", My.Resources.Peru)
Me.Add("Philippines", My.Resources.Philippines)
Me.Add("Pitcairn Islands", My.Resources.Pitcairn_Islands)
Me.Add("Poland", My.Resources.Poland)
Me.Add("Portugal", My.Resources.Portugal)
Me.Add("Puerto Rico", My.Resources.Puerto_Rico)
Me.Add("Qatar", My.Resources.Qatar)
Me.Add("Republic of the Congo", My.Resources.Republic_of_the_Congo)
Me.Add("Romania", My.Resources.Romania)
Me.Add("Russian Federation", My.Resources.Russian_Federation)
Me.Add("Rwanda", My.Resources.Rwanda)
Me.Add("Saint Kitts and Nevis", My.Resources.Saint_Kitts_and_Nevis)
Me.Add("Saint Lucia", My.Resources.Saint_Lucia)
Me.Add("Saint Pierre", My.Resources.Saint_Pierre)
Me.Add("Saint Vicent and the Grenadines", My.Resources.Saint_Vicent_and_the_Grenadines)
Me.Add("Samoa", My.Resources.Samoa)
Me.Add("Sao Tome and Principe", My.Resources.Sao_Tomé_and_Príncipe)
Me.Add("Saudi Arabia", My.Resources.Saudi_Arabia)
Me.Add("Senegal", My.Resources.Senegal)
Me.Add("Serbia and Montenegro", My.Resources.Serbia_and_Montenegro)
Me.Add("Seychelles", My.Resources.Seychelles)
Me.Add("Sierra Leone", My.Resources.Sierra_Leone)
Me.Add("Singapore", My.Resources.Singapore)
Me.Add("Slovakia", My.Resources.Slovakia)
Me.Add("Slovenia", My.Resources.Slovenia)
Me.Add("Soloman Islands", My.Resources.Soloman_Islands)
Me.Add("Somalia", My.Resources.Somalia)
Me.Add("South Africa", My.Resources.South_Africa)
Me.Add("South Georgia", My.Resources.South_Georgia)
Me.Add("South Korea", My.Resources.South_Korea)
Me.Add("Spain", My.Resources.Spain)
Me.Add("Sri Lanka", My.Resources.Sri_Lanka)
Me.Add("Sudan", My.Resources.Sudan)
Me.Add("Suriname", My.Resources.Suriname)
Me.Add("Swaziland", My.Resources.Swaziland)
Me.Add("Syria", My.Resources.Syria)
Me.Add("Taiwan", My.Resources.Taiwan)
Me.Add("Tajikistan", My.Resources.Tajikistan)
Me.Add("Tanzania", My.Resources.Tanzania)
Me.Add("Thailand", My.Resources.Thailand)
Me.Add("Tibet", My.Resources.Tibet)
Me.Add("Togo", My.Resources.Togo)
Me.Add("Tonga", My.Resources.Tonga)
Me.Add("Trinidad and Tobago", My.Resources.Trinidad_and_Tobago)
Me.Add("Tunisia", My.Resources.Tunisia)
Me.Add("Turkey", My.Resources.Turkey)
Me.Add("Turkmenistan", My.Resources.Turkmenistan)
Me.Add("Turks and Caicos Islands", My.Resources.Turks_and_Caicos_Islands)
Me.Add("Tuvalu", My.Resources.Tuvalu)
Me.Add("UAE", My.Resources.UAE)
Me.Add("Uganda", My.Resources.Uganda)
Me.Add("Ukraine", My.Resources.Ukraine)
Me.Add("United Kingdom", My.Resources.United_Kingdom)
Me.Add("United States of America", My.Resources.United_States_of_America)
Me.Add("Uruguay", My.Resources.Uruguay)
Me.Add("US Virgin Islands", My.Resources.US_Virgin_Islands)
Me.Add("Uzbekistan", My.Resources.Uzbekistan)
Me.Add("Vanuatu", My.Resources.Vanuatu)
Me.Add("Vatican City", My.Resources.Vatican_City)
Me.Add("Venezuela", My.Resources.Venezuela)
Me.Add("Vietnam", My.Resources.Vietnam)
Me.Add("Wallis and Futuna", My.Resources.Wallis_and_Futuna)
Me.Add("Yemen", My.Resources.Yemen)
Me.Add("Zambia", My.Resources.Zambia)
Me.Add("Zimbabwe", My.Resources.Zimbabwe)
End Sub
End Class
|
Observe que no código estamos usando My.Recources e o nome do país para indicar a bandeira do país armazenada em um arquivo de recursos.
Nota : Para obter as imagens das bandeiras dos países baixe o arquivo deste link (Bandeiras.zip) e descompacte o arquivo em uma pasta chamada c:\Bandeiras.
A seguir no evento Load do formulário vamos carregar o controle Combobox - cboPaises - com a informação do nome do país a partir da classe Bandeiras:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each key As String In bandeiras.Keys
cboPaises.Items.Add(key)
Next
End Sub
|
Finalmente vamos usar o evento SelectedIndexChanged do Combobox e incluir o código a seguir
Private Sub cboPaises_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPaises.SelectedIndexChanged
Dim pais As String = cboPaises.Text
If bandeiras.ContainsKey(pais) Then
picBandeira.Image = bandeiras(pais)
lblPais.Text = pais
End If
End Sub
|
Neste código, havendo qualquer alteração no índice do Combobox estamos obtendo o nome do país (que é a chave do dicionário) e a partir dele obtendo a imagem da classe Bandeiras e exibindo no controle PictureBox - picBandeiras:
Executando o projeto e selecionando um país da combobox teremos o resultado abaixo:
Pegue o projeto completo aqui : Paises_Bandeiras.zip (sem os recursos)
Disse-lhe Jesus: Estou há tanto tempo
convosco, e não me tendes conhecido, Filipe? Quem me vê a mim vê o Pai; e como
dizes tu: Mostra-nos o Pai?
João 14:9
Veja os
Destaques e novidades do SUPER DVD Visual Basic
(sempre atualizado) : clique e confira !
Quer migrar para o VB .NET ?
Quer aprender C# ??
Quer aprender os conceitos da Programação Orientada a objetos ? Quer aprender o gerar relatórios com o ReportViewer no VS 2013 ? |
Referências:
Super DVD Vídeo Aulas - Vídeo Aula sobre VB .NET, ASP .NET e C#
Super DVD C# - Recursos de aprendizagens e vídeo aulas para C#
Curso Fundamentos da Programação Orientada a Objetos com VB .NET
https://msdn.microsoft.com/pt-br/library/system.text.asciiencoding%28v=vs.110%29.aspx