Click here to Skip to main content
Click here to Skip to main content

WPF: CountryFlag Control

By , 4 May 2011
 
Prize winner in Competition "Best VB.NET article of May 2011"
Screenshot_1.png

Image: Several CountryFlag controls in action

Introduction

The CountryFlag control is a UserControl that allows you to display the flag of a particular country, in your WPF application, from a choice of 223 countries. This article describes how to use the control and how I went about creating it. The control will hopefully save you from having to scour the net for flag images and from writing numerous lines of code to implement the functionality it provides.

CountryFlag

In order to use the control, add a reference to CountryFlag.dll in your project. In Expression Blend, make the Assets panel active and start typing the word 'CountryFlag' in the Search box. The CountryFlag control should be displayed as you continue typing.

Screenshot_2.png

Double-click on the control to add it to your application or select it and draw out the control in your application window.

Screenshot_3.png

With the control you've just added still selected, look for the Flag property in the Miscellaneous section of the Properties panel.

Screenshot_4.png

Expand the combobox and select the country whose flag you want to display.

Screenshot_5.png

Once you select a country, its flag should be displayed in the control.

Screenshot_6.png

Specifying a Flag in Code

You can also specify which flag you want to display using code.

VB.NET

Imports CountryFlag

Class MainWindow
    Private Function ShowFlag() As String
        CountryFlagCtrl.Flag = Country.Kenya

        Dim countryFlg As String = CountryFlagCtrl.Flag.ToString
        Return countryFlg
    End Function
End Class

C#

private string ShowFlag()
{
    CountryFlagCtrl.Flag = Country.Kenya;

    string countryFlg = CountryFlagCtrl.Flag.ToString();
    return countryFlg;
}

Once you type the equal sign IntelliSense should help you along:

Screenshot_7.png

Design and Layout

The control contains a single Layout control and an Image control named FlagImage:

Screenshot_9.png

The Code

The CountryFlag UserControl library contains an enumeration, Country, that contains the names of 223 countries. Here's parts of the enum:

Public Enum Country
    Afghanistan
    Albania
    Algeria
    American_Samoa
    Andorra
    Angola
    Anguilla
    Antigua_and_Barbuda
    .
    .
    .
    Venezuela
    Vietnam
    Wallis_and_Futuna
    Yemen
    Zambia
    Zimbabwe
End Enum

The UserControl class contains code for creating a DependencyProperty of type Country:

Class CountryFlag
    Public Property Flag() As Country
        Get
            Return CType(GetValue(FlagProperty), Country)
        End Get
        Set(ByVal value As Country)
            SetValue(FlagProperty, value)
        End Set
    End Property

    Public Shared FlagProperty As DependencyProperty = _
        DependencyProperty.Register("Flag", _
                                    GetType(Country), _
                                    GetType(CountryFlag), _
                                    New FrameworkPropertyMetadata( _
                                    New PropertyChangedCallback(AddressOf ChangeFlag)))

    Private Shared Sub ChangeFlag(ByVal source As DependencyObject, _
                                  ByVal e As DependencyPropertyChangedEventArgs)

        Dim countryName As String = CType(e.NewValue, Country).ToString
        Dim path As String = "flags/" & countryname & ".png"

        CType(source, CountryFlag).FlagImage.Source = _
            New BitmapImage(New Uri(path, UriKind.Relative))
    End Sub
End Class

Notice that in the callback method, ChangeFlag, we specify the image for the control's Image control. The reason the .dll does not need to come bundled with a separate folder containing image files is because the images are embedded in the assembly. If you look in the Solution Explorer of Visual Studio, you will notice a folder named Flags that contains the image files.

Screenshot_8.png

Conclusion

CountryFlag will hopefully prove to be useful and I hope you gained more than that from reading the article. Cheers!

History

  • 3rd May, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Meshack Musundi
Software Developer
Kenya Kenya
Member
Meshack is an avid programmer with a bias towards WPF and VB.NET. He currently resides in a small town in Kiambu county, Kenya.
 
Awards;
  • CodeProject MVP 2013
  • CodeProject MVP 2012
  • Best VB.NET article of February 2013
  • Best VB.NET article of October 2012
  • Best VB.NET article of July 2012
  • Best VB.NET article of February 2012
  • Best VB.NET article of January 2012
  • Best VB.NET article of November 2011
  • Best VB.NET article of June 2011
  • Best VB.NET article of May 2011
  • Best VB.NET article of March 2011
  • Best VB.NET article of February 2011
  • Best VB.NET article of January 2011
  • Best VB.NET article of December 2010
  • Best VB.NET article of November 2010

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberfredatcodeproject15 May '13 - 1:24 
very good thanks for sharing
GeneralRe: My vote of 5mvpMeshack Musundi16 May '13 - 2:57 
Thanks. "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"
GeneralMy vote of 5member GeekBond 31 Oct '12 - 4:47 
Nice control, very handy.
GeneralRe: My vote of 5mvpMeshack Musundi31 Oct '12 - 6:52 
Thanks. I'm glad you find it useful. "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"
GeneralMy vote of 5memberBeeWayDev9 Dec '11 - 5:34 
excellent !
GeneralRe: My vote of 5mvpMeshack Musundi12 Jan '12 - 4:54 
Thanks.
GeneralMy vote of 5memberIFFI11 Jul '11 - 21:36 
My vote of 5. Quite a handy control. )
GeneralRe: My vote of 5memberMeshack Musundi11 Jul '11 - 22:30 
Thanks.
GeneralRe: My vote of 5memberBeeWayDev9 Dec '11 - 5:37 
Hi,   I love your WPF articles, i have no experience in WPF and would like to learn how to make attractive UIs, any tips where to start ? if you know any online content about WPF UIs can you please reply to this Msg, thanks a lot, and keep it up
GeneralRe: My vote of 5memberMeshack Musundi9 Dec '11 - 20:17 
I mostly design my UIs using Expression Design, then export the design as a WPF canvas. One of the first tutorials I read when I got started was the Expression Design Quick Start Guide, which you can find in the 'Using Expression Design 1' section here.
 
You can also look around for some Expression Blend tutorials. I started off reading the ones on Kirupa's website which you can access here.
 
After that I just played around with both Expression Design and Blend. Experimentation is a good way to get a good grasp of things. Just use your imagination.
GeneralRe: My vote of 5memberBeeWayDev15 Dec '11 - 4:04 
Thanks a lot
GeneralMy vote of 4membergerm1311 Jul '11 - 20:30 
Very handy.
GeneralRe: My vote of 4memberMeshack Musundi11 Jul '11 - 22:29 
Thanks again.
GeneralMy vote of 5mvpMd. Marufuzzaman15 Jun '11 - 20:01 
Excellent
GeneralRe: My vote of 5memberMeshack Musundi16 Jun '11 - 5:04 
Thanks.
GeneralRe: My vote of 5mvpMd. Marufuzzaman16 Jun '11 - 6:14 
Welcome & Keep it up Thanks Md. Marufuzzaman I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
GeneralNeat, but ...memberMr. Mox10 May '11 - 3:04 
Using Enum this way is too much. It screams for a database - a simple xml file would do - and then you could and should use the ISO 3166 country code as id for the flag and filename - which, further, would allow to use country names of any language, not just English.   /gustav
GeneralRe: Neat, but ...memberMeshack Musundi10 May '11 - 5:39 
Thanks Gustav.   Mr. Mox wrote:It screams for a database - a simple xml file would do I initially had this in mind, especially the use of a xml file, but the need to have an assembly that wasn't dependent on a separate file overrode this idea. The Enum was also beneficial when it came...
GeneralRe: Neat, but ...memberJaime Olivares11 Jul '11 - 10:54 
My vote for the use of ISO codes. You can keep a dictionary<isokey, flagdata> into an embedded xml file in your assembly. You don't need a separated file. Best regards, Jaime.
GeneralRe: Neat, but ...memberMeshack Musundi11 Jul '11 - 22:28 
I agree on the use of ISO codes. Just that I haven't had the time to get back to this. But anyone is at liberty to implement their preferred changes.   Cheers!
SuggestionRe: Neat, but ...membergerm1311 Jul '11 - 20:29 
Thanks...I agree with the gentlemen here. Good work though.
GeneralRe: Neat, but ...memberMeshack Musundi11 Jul '11 - 22:28 
Thanks.
Generalnice articlememberCIDev5 May '11 - 5:17 
A nice little article with an interesting topic. Something a bit different from the usual coding stuff Just because the code works, it doesn't mean that it is good code.
GeneralRe: nice articlememberMeshack Musundi5 May '11 - 9:14 
Thanks. I'm glad you like it.
Generalyeah flag itmemberfreakyit3 May '11 - 6:30 
have a 5 for the local stuff

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 5 May 2011
Article Copyright 2011 by Meshack Musundi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid