Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB
Print

VB & C# .NET/WinForms: Language Selector ComboBox

3.12/5 (6 votes)
16 Jan 2023CPOL1 min read 8.9K   278  
Custom DropDownBox with all languages of the world to select a language.Save and reload Selected language using Application Settings
In this tip, you will see an inherited DropDown box from ComboBox class with all languages to select one of them using CultureInfo Try to save selected language in Application Settings and reload it.

Introduction

This custom component can be used, to select a language for user profile by Application user or else.

Image 1

Background

Use GetCultures function of CultureInfo class to Load All Languages by choosing CultureTypes.AllCultures for types argument.

For Each Item As CultureInfo In CultureInfo.GetCultures(CultureTypes.AllCultures)
 If Item.LCID = 127 Then Continue For
  Me.Items.Add(Item)
Next

Using the Code

Namespaces

  1. System.Windows.Forms
  2. System.ComponentModel
  3. System.Globalization
  4. System.Drawing

Steps

  • Create a new class with inheritance of ComboBox class.
    Set the class name to LanguageSelectorComboBox.

    Public Class LanguageSelectorComboBox
        Inherits ComboBox
        
    End Class
  • Create an Enumeration with DisplayValues name for LanguageSelectorComboBox class:

    Enum DisplayValues
        DisplayName = 0
        Name = 1
        NativeName = 2
        EnglishName = 3
    End Enum
  • Create DisplayValue property for the class:

    Private DisplayValueValue As DisplayValues
    <RefreshProperties(RefreshProperties.All)>
    <DefaultValue(0)>
    Public Property DisplayValue() As DisplayValues
        Get
            Return DisplayValueValue
        End Get
        Set(ByVal value As DisplayValues)
            DisplayValueValue = value
        End Set
    End Property
    
  • Create SelectedItem property with CultureInfo type as Shadow property.
    This property uses SelectedItem property of Base Class to set and return Value:

    Public Shadows Property SelectedItem() As CultureInfo
        Get
            Return MyBase.SelectedItem
        End Get
        Set(ByVal value As CultureInfo)
            MyBase.SelectedItem = value
        End Set
    End Property
    
  • Create SelectedValue property with Integer Type as Shadow property:
    This property uses GetCultureInfo function of CultureInfo class to set SelectedItem property of the Class and take LCID of SelectedItem as return Value:

    Public Shadows Property SelectedValue() As Integer
     Get
      If Me.SelectedItem Is Nothing Then Return CultureInfo.CurrentCulture.LCID
      Return Me.SelectedItem.LCID
     End Get
     Set(ByVal value As Integer)
      Me.SelectedItem = CultureInfo.GetCultureInfo(value)
     End Set
    End Property
  • Use InitLayout overridable method to set Default Values of Properties as below:

    Protected Overrides Sub InitLayout()
     MyBase.InitLayout()
      If Me.DesignMode = True Then Exit Sub
       Me.Items.Clear()
       Me.DrawMode = DrawMode.OwnerDrawFixed
       Me.DropDownStyle = ComboBoxStyle.DropDownList
       Me.Sorted = True
       For Each Item As CultureInfo _
           In CultureInfo.GetCultures(CultureTypes.AllCultures)
         If Item.LCID = 127 Then Continue For
          Me.Items.Add(Item)
       Next
       If Me.Items.Count > 0 Then
          Me.SelectedItem = CultureInfo.CurrentCulture
       End If
      End Sub
  • Using OnDrawItem overridable method to Draw the DisplayValue:

    Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
        MyBase.OnDrawItem(e)
        If e.Index = -1 Then Exit Sub
        Dim Expr As String = ""
        e.DrawBackground()
        Dim g As Graphics = e.Graphics
        With DirectCast(Me.Items(e.Index), CultureInfo)
            Select Case Me.DisplayValue
                Case DisplayValues.DisplayName
                    Expr = .DisplayName
                Case DisplayValues.Name
                    Expr = .Name
                Case DisplayValues.NativeName
                    Expr = .NativeName
                Case DisplayValues.EnglishName
                    Expr = .EnglishName
            End Select
        End With
        g.DrawString(Expr, e.Font, New SolidBrush(e.ForeColor), e.Bounds)
    End Sub

After these steps, build the project and go to Toolbox, then choose LanguageSelectorComboBox item and drop that on the project MainForm:

Image 2

Go to Application Properties -> Settings Tab-page and create

  1. SelectedLanguage As System.Globalization.CultureInfo
  2. SelectedValue As Integer

Settings for Application Settings

Image 3

To set Application Settings, use these code lines:

Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
    My.Settings.SelectedLanguage = Me.LanguageSelectorComboBox1.SelectedItem
    My.Settings.SelectedValue = Me.LanguageSelectorComboBox1.SelectedValue
    My.Settings.Save()
End Sub

To Reload SelectedItem:

Private Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
    Me.LanguageSelectorComboBox1.SelectedItem = My.Settings.SelectedLanguage
End Sub

And to Reload SelectedValue:

Private Sub button3_Click(sender As Object, e As EventArgs) Handles button3.Click
    Me.LanguageSelectorComboBox1.SelectedValue = My.Settings.SelectedValue
End Sub

History

  • Friday, 13th January, 2023: Initial version

License

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