laatzen/Common/Utils/UiLanguageControlShared/ControlExtentions.cs
2021-10-01 11:09:20 +02:00

53 lines
1.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace Xylem.Common.Utils.UiLanguageControl
{
/// <summary>
/// Change language at runtime
/// </summary>
/// <remarks date="2021-Feb-23" author="Thomas Wiedebusch">
/// - Initial based on code example from stackoverflow
/// </remarks>
[Serializable]
public static class ControlExtensions
{
/// <summary>
/// Change language at runtime
/// </summary>
/// <remarks date="2021-Feb-23" author="Thomas Wiedebusch">
/// - Initial based on code example:
/// https://stackoverflow.com/questions/52178064/winforms-localization-how-to-change-the-language-of-a-menu.
/// </remarks>
public static IEnumerable<Control> AllControls(this Control control)
{
foreach (Control c in control.Controls)
{
yield return c;
foreach (Control child in c.Controls)
yield return child;
}
}
/// <summary>
/// Update all visual elements after language change
/// </summary>
/// <remarks date="2020-Nov-27" author="Thomas Wiedebusch">
/// - Initial based on code example:
/// https://stackoverflow.com/questions/7556367/how-do-i-change-the-culture-of-a-winforms-application-at-runtime
/// </remarks>
public static void ChangeControlText(ComponentResourceManager resources, IEnumerable controls)
{
foreach (Control ctl in controls)
{
resources.ApplyResources(ctl, ctl.Name);
ChangeControlText(resources, ctl.Controls);
}
}
}
}