C#窗体多语言切换(简繁)

多窗体最好继承一个父窗体,在父窗体Load事件中执行此方法

添加引用 using Microsoft.VisualBasic;

 

  1         #region 语言切换
  2         /// <summary>
  3         /// 语言切换
  4         /// </summary>
  5         public static void ChangeLanguage(Form f)
  6         {
  7             if (!StaticInfo.ChangeLanguage) return;
  8 
  9             //设置窗体标题
 10             f.Text = GetLanguage(f.Text);
 11 
 12             //遍历Form上的所有控件
 13             foreach (Control control in f.Controls)
 14             {
 15                 if (control is Panel || control is GroupBox)
 16                 {
 17                     foreach (Control con in control.Controls)
 18                     {
 19                         SetLanguage(con);
 20                     }
 21                 }
 22                 else
 23                 {
 24                     SetLanguage(control);
 25                 }
 26             }
 27         }
 28 
 29         private static void SetLanguage(Control con)
 30         {
 31             //设置按钮
 32             Button btn = con as Button;
 33             if (btn != null)
 34             {
 35                 btn.Text = GetLanguage(btn.Text);
 36             }
 37             
 38             //设置文本标签
 39             Label lb = con as Label;
 40             if (lb != null)
 41             {
 42                 lb.Text = GetLanguage(lb.Text);
 43             }
 44             
 45             //设置复选框
 46             CheckBox cb = con as CheckBox;
 47             if (cb != null)
 48             {
 49                 cb.Text = GetLanguage(cb.Text);
 50             }
 51             
 52             //设置菜单栏
 53             MenuStrip ms = con as MenuStrip;
 54             if (ms != null)
 55             {
 56                 foreach (ToolStripMenuItem item in ms.Items)
 57                 {
 58                     if (StaticInfo.Language.ToUpper() == "CN")
 59                     {
 60                         item.Text = Strings.StrConv(item.Text, VbStrConv.TraditionalChinese, 1);
 61                         for (int i = 0; i < item.DropDownItems.Count; i++)
 62                         {
 63                             item.DropDownItems[i].Text = GetLanguage(item.DropDownItems[i].Text);
 64                         }
 65                     }
 66                     else
 67                     {
 68                         item.Text = Strings.StrConv(item.Text, VbStrConv.SimplifiedChinese, 1);
 69                         for (int i = 0; i < item.DropDownItems.Count; i++)
 70                         {
 71                             item.DropDownItems[i].Text = GetLanguage(item.DropDownItems[i].Text);
 72                         }
 73                     }
 74                 }
 75             }
 76 
 77             //设置工具栏
 78             ToolStrip ts = con as ToolStrip;
 79             if (ts != null)
 80             {
 81                 for (int i = 0; i < ts.Items.Count; i++)
 82                 {
 83                     ts.Items[i].Text = GetLanguage(ts.Items[i].Text);
 84                 }
 85             }
 86 
 87             //设置数据表格
 88             DataGridView dgv = con as DataGridView;
 89             if (dgv != null)
 90             {
 91                 for (int i = 0; i < dgv.Columns.Count; i++)
 92                 {
 93                     dgv.Columns[i].HeaderText = GetLanguage(dgv.Columns[i].HeaderText);
 94                 }
 95             }
 96 
 97             //设置选项卡
 98             TabControl tc = con as TabControl;
 99             if (tc != null)
100             {
101                 tc.Text = GetLanguage(tc.Text);
102 
103                 for (int i = 0; i < tc.TabPages.Count; i++)
104                 {
105                     tc.TabPages[i].Text = GetLanguage(tc.TabPages[i].Text);
106 
107                     foreach (Control c in tc.TabPages[i].Controls)
108                     {
109                         SetLanguage(c);
110                     }
111                 }
112             }
113 
114             //设置ListView
115             ListView lv = con as ListView;
116             if (lv != null)
117             {
118                 for (int i = 0; i < lv.Columns.Count; i++)
119                 {
120                     lv.Columns[i].Text = GetLanguage(lv.Columns[i].Text);
121                 }
122             }
123 
124             //设置分组框
125             GroupBox gb = con as GroupBox;
126             if (gb != null)
127             {
128                 foreach (Control control in gb.Controls)
129                 {
130                     SetLanguage(control);
131                 }
132             }
133         }
134 
135         //获取当前语言下的文本
136         private static string GetLanguage(string text)
137         {
138          return   Strings.StrConv(text,
139                            StaticInfo.Language.ToUpper() == "CN"
140                                ? VbStrConv.SimplifiedChinese
141                                : VbStrConv.TraditionalChinese, 1);
142         }
143 
144         #endregion
多语言切换代码

 

转载于:https://www.cnblogs.com/codeyou/p/4981243.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值