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.
Background
Use GetCultures
function of CultureInfo c
lass 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
foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
if (culture.LCID == 127) { continue; }
this.Items.Add(culture);
}
Using the Code
Namespaces
System.Windows.Forms
System.ComponentModel
System.Globalization
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
class LanguageSelectorComboBox:ComboBox
{ }
-
Create an Enumeration with DisplayValues
name for LanguageSelectorComboBox
class:
Enum DisplayValues
DisplayName = 0
Name = 1
NativeName = 2
EnglishName = 3
End Enum
public enum DisplayValues
{
DisplayName = 0,
Name = 1,
NativeName = 2,
EnglishName = 3
}
-
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
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:
Public Shadows Property SelectedItem() As CultureInfo
Get
Return MyBase.SelectedItem
End Get
Set(ByVal value As CultureInfo)
MyBase.SelectedItem = value
End Set
End Property
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:
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
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:
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
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
:
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
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
:
Go to Application Properties -> Settings Tab-page and create
SelectedLanguage
As System.Globalization.CultureInfo
SelectedValue
As Integer
Settings for Application Settings
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
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
:
Private Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
Me.LanguageSelectorComboBox1.SelectedItem = My.Settings.SelectedLanguage
End Sub
private void button2_Click(object sender, EventArgs e)
{
languageSelectorComboBox1.SelectedItem = Settings.SelectedLanguage;
}
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
private void button3_Click(object sender, EventArgs e)
{
languageSelectorComboBox1.SelectedValue = Settings.SelectedValue;
}
History
- Friday, 13th January, 2023: Initial version