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

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.

VB.NET
For Each Item As CultureInfo In CultureInfo.GetCultures(CultureTypes.AllCultures)
 If Item.LCID = 127 Then Continue For
  Me.Items.Add(Item)
Next
C#
foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
 {
  if (culture.LCID == 127) { continue; }
  this.Items.Add(culture);
 }

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.

    VB.NET
    Public Class LanguageSelectorComboBox
        Inherits ComboBox
        
    End Class
    C#
    class LanguageSelectorComboBox:ComboBox
        { }
  • Create an Enumeration with DisplayValues name for LanguageSelectorComboBox class:

    VB.NET
    Enum DisplayValues
        DisplayName = 0
        Name = 1
        NativeName = 2
        EnglishName = 3
    End Enum
    C#
    public enum DisplayValues
     {
        DisplayName = 0,
        Name = 1,
        NativeName = 2,
        EnglishName = 3
     }
  • Create DisplayValue property for the class:

    VB.NET
    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
    
    C#
    private DisplayValues DisplayValueValue;
    [DefaultValue(0)]
    public DisplayValues DisplayValue
    {
        get { return DisplayValueValue; }
        set { DisplayValueValue = value; }
    }
    
  • Create SelectedItem property with CultureInfo type as Shadow property.
    This property uses SelectedItem property of Base Class to set and return Value:

    VB.NET
    Public Shadows Property SelectedItem() As CultureInfo
        Get
            Return MyBase.SelectedItem
        End Get
        Set(ByVal value As CultureInfo)
            MyBase.SelectedItem = value
        End Set
    End Property
    
    C#
    public new CultureInfo SelectedItem
     {
        get { return (CultureInfo)base.SelectedItem; } 
        set {  base.SelectedItem = value; }
     } 
  • 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:

    VB.NET
    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
    C#
    public new int SelectedValue
      {
       get
       {
        if (this.SelectedItem == null)
          {return CultureInfo.CurrentCulture.LCID;}
          else
          {return this.SelectedItem.LCID;}
       }   
       set 
       {
        this.SelectedItem = CultureInfo.GetCultureInfo(value)
       }
      }
  • Use InitLayout overridable method to set Default Values of Properties as below:

    VB.NET
    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
    C#
    protected override void InitLayout()
    {
     base.InitLayout();
     if (this.DesignMode == true)
      {
        return;               
      }
      this.DropDownStyle = ComboBoxStyle.DropDownList;
      this.DrawMode = DrawMode.OwnerDrawFixed;
      this.Sorted = true;
      this.Items.Clear();
      foreach (CultureInfo culture in 
               CultureInfo.GetCultures(CultureTypes.AllCultures))
       {
        if (culture.LCID == 127) { continue; }
         this.Items.Add(culture);
        }
       if (base.Items.Count > 0)
         {
          this.SelectedItem = CultureInfo.CurrentCulture;
         }
        }
  • Using OnDrawItem overridable method to Draw the DisplayValue:

    VB.NET
    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
    C#
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        base.OnDrawItem(e);
        if (e.Index==-1) { return; }
        string Expr = string.Empty;
        CultureInfo Culture = (CultureInfo)this.Items[e.Index];
        switch (DisplayValue)
            {
            case DisplayValues.DisplayName:
                Expr = Culture.DisplayName; 
                break;
            case DisplayValues.EnglishName:
                Expr = Culture.DisplayName; 
                break;
            case DisplayValues.Name:
                Expr = Culture.Name;
                break;
            case DisplayValues.NativeName:
                Expr = Culture.NativeName;
                break;
            }
        Graphics g = e.Graphics;
        e.DrawBackground();
        g.DrawString(Expr, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
    }

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:

VB.NET
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
C#
private Properties.Settings Settings = Properties.Settings.Default;
private void button1_Click(object sender, EventArgs e)
 {
    Settings.SelectedLanguage = languageSelectorComboBox1.SelectedItem;
    Settings.SelectedValue = languageSelectorComboBox1.SelectedValue;
    Settings.Save();
 }

To Reload SelectedItem:

VB.NET
Private Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
    Me.LanguageSelectorComboBox1.SelectedItem = My.Settings.SelectedLanguage
End Sub
C#
private void button2_Click(object sender, EventArgs e)
{
    languageSelectorComboBox1.SelectedItem = Settings.SelectedLanguage;
}

And to Reload SelectedValue:

VB.NET
Private Sub button3_Click(sender As Object, e As EventArgs) Handles button3.Click
    Me.LanguageSelectorComboBox1.SelectedValue = My.Settings.SelectedValue
End Sub
C#
private void button3_Click(object sender, EventArgs e)
{
    languageSelectorComboBox1.SelectedValue = Settings.SelectedValue;
}

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)