Hello All,
I am here with the same Query but with a different look
I want to apply the culture change to my program.
I created a class library for culture change. and applied it .
It is only working with the form name, i did it with two different languages and got the result.
But the controls inside the form remains as it is.
Please help.

Dani AI

Generated

Short diagnosis and reliable fix

Designer-generated resource keys are based on the control’s Name (and "$this" for the form), not the private field name you see with reflection. Reflection can return compiler/backing-field names (often with a leading underscore), so using field names to locate resources will fail. The robust approach is either: set the UI culture before the form is constructed so InitializeComponent loads the right .resx, or apply resources at runtime using a ComponentResourceManager and the control.Name values.

Quick checklist

  • Set the Form's Localizable = True in the designer and edit each Language so Visual Studio creates the .resx files.
  • To have forms load localized text automatically, set Thread.CurrentThread.CurrentUICulture (and CurrentCulture if needed) before creating the form.
  • To switch language at runtime, use a ComponentResourceManager and apply resources recursively to controls, ToolStrip/MenuStrip items and non-Control items (DataGridView columns, etc.).

Example: apply resources at runtime (VB.NET)

' Set culture first (if switching at runtime)
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("fr-FR")
System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture

' Apply resources to an existing form instance
Sub ApplyCultureToForm(frm As Form)
    Dim rm As New System.ComponentModel.ComponentResourceManager(frm.GetType())
    rm.ApplyResources(frm, "$this")
    ApplyResourcesRecursive(frm, rm)
End Sub

Sub ApplyResourcesRecursive(parent As Control, rm As System.ComponentModel.ComponentResourceManager)
    For Each c As Control In parent.Controls
        rm.ApplyResources(c, c.Name)
        If TypeOf c Is ToolStrip Then ApplyToolStripItems(DirectCast(c, ToolStrip).Items, rm)
        If TypeOf c Is DataGridView Then
            For Each col As DataGridViewColumn In DirectCast(c, DataGridView).Columns
                rm.ApplyResources(col, col.Name)
            Next
        End If
        If c.HasChildren Then ApplyResourcesRecursive(c, rm)
    Next
End Sub

Sub ApplyToolStripItems(items As ToolStripItemCollection, rm As System.ComponentModel.ComponentResourceManager)
    For Each it As ToolStripItem In items
        rm.ApplyResources(it, it.Name)
        Dim dd = TryCast(it, ToolStripDropDownItem)
        If dd IsNot Nothing Then ApplyToolStripItems(dd.DropDownItems, rm)
    Next
End Sub

Notes

  • ' advice to iterate controls is on point; the above shows a production-ready variant that also handles ToolStrips and grid columns.
  • ’s underscore observation is consistent with reflection returning different field identifiers — rely on control.Name (the key used in .resx) rather than field names.
  • Custom user controls must expose localizable properties and be present in the form .resx to be picked up by ApplyResources; otherwise recreate the control after setting the culture.

Recommended Answers

All 4 Replies

Add a sub like

Sub ApplyCulture(Byval SelectedCulturename as String)
	Dim Rm As New Resources.ResourceManager("YourProject.YourFormClass", System.Reflection.Assembly.GetExecutingAssembly())
	Dim Ci As System.Globalization.CultureInfo = New System.Globalization.CultureInfo(SelectedCultureName)
	Me.Text = Rm.GetString("$this.Text", Ci)
.
.	' you need to do this for all controls having a distinct text depending on the language
.
	Me.btnWhatItWas.Text = Rm.GetString("btnWhatItWas.Text", Ci)
.
.
.
End Sub

Then call this sub at the load event of the form. Also when you want to change the interface language.

Hope this helps

Thanks for your reply
But scenario is little bit different.
this code is not dynamic.
to make it dynamic I created a Class library
and inside that I did the things you mentioned and used a code to get the controls of form dynamically.
It works with perferctly in demo project I got from web.
but when i tried to make my own program, I did all those things related to culture exactly similar to the demo project.
The thing is My form title is changing.
but the controls text is not changing.

Dim fields As FieldInfo() = form.[GetType]().GetFields(BindingFlags.Instance Or BindingFlags.DeclaredOnly Or BindingFlags.NonPublic)

This code returns exact name of controls in demo project.

but in my program and underscore is prefixed like '_controlName'.

Did you already tryed:

For each C as Control In Form.Controls
'
'    Do here what you need for each control
'
Next

?

Hello lolafuertes,
Thank you for your reply.
it works.
Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.