C#批量更改控件显示名称(举例中英文语言切换)

在WinFrom开发中有中英文切换需求时
需要批量更改控件的显示名称
可以将控件名字和显示名称保存在xml文件中
可以将控件保存进文件,也可从文件中加载来更改显示名
牵涉xml文件的读写插入修改基本知识

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApp1
{
  public  class XML
    {

        public XmlDocument doc;
        public XmlElement root;
        public XmlDeclaration declare;

        public string path;
        public void CreatElement(string formName, string name, string txt)
        {
            if (doc == null)
                doc = new XmlDocument();
            if (doc.GetElementsByTagName("version") == null)
            {
                declare = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
                doc.AppendChild(declare);
            }

            if (doc.SelectSingleNode(formName) == null)
            {
                root = doc.CreateElement(formName);
                doc.AppendChild(root);
            }

            var nods = doc.SelectSingleNode(formName);
            root = (XmlElement)nods;


            bool bHave = Checkhave(formName, name, txt);
            if (bHave)
            {
                //已经包含该名称
                RenameControl(formName, name, txt);
                return;
            }
            XmlElement Contollist;
            Contollist = doc.CreateElement("控件");
            XmlElement ctrolName = doc.CreateElement("控件名");
            XmlText prize1 = doc.CreateTextNode(name);
            ctrolName.AppendChild(prize1);
            XmlElement txtName = doc.CreateElement("显示名");
            XmlText num1 = doc.CreateTextNode(txt);
            txtName.AppendChild(num1);
            root.AppendChild(Contollist);
            Contollist.AppendChild(ctrolName);
            Contollist.AppendChild(txtName);

        }
        public void CreatElementByHashTb(string formName,  Hashtable mtb=null)
        {
             foreach(DictionaryEntry de in mtb) 
            {
                CreatElement(formName, de.Key.ToString(), de.Value.ToString());
            }
            SaveXml(path);
        }
        public Hashtable GetHasTbFromXml(string formName,string path)
        {
            if (doc == null)
                doc = new XmlDocument();
            Hashtable mtab = new Hashtable();
            doc.Load(path); //获取xml根节点
            XmlNode xmlRoot = doc.DocumentElement; //根据节点顺序逐步读取 
          //var nameNodeList = xmlRoot.SelectNodes("控件"); //读取节点的id属性
            XmlNode rootNode = doc.SelectSingleNode(formName);


            int i = 0;
            while(i< rootNode.ChildNodes.Count)
            {
                var curnod = rootNode.ChildNodes[i];
                if (curnod.Name == "控件")
                {
                    if (curnod.ChildNodes[0].Name == "控件名" && curnod.ChildNodes[1].Name == "显示名")
                        mtab.Add(curnod.ChildNodes[0].InnerText, curnod.ChildNodes[1].InnerText);
                   
                }
                i++;
            }
            return mtab;
         
        }

        /// <summary>
        /// 修改指定控件的显示名
        /// </summary>
        /// <param name="name"></param>
        /// <param name="newtxt"></param>
        public void RenameControl(string formName,string name, string newtxt)
        {
            if (doc == null)
            {
                doc = new XmlDocument();
                doc.Load(path); //获取xml根节点
            }
            XmlNode xmlRoot = doc.DocumentElement; //根据节点顺序逐步读取 
                                                  
            XmlNode rootNode = doc.SelectSingleNode(formName);

            int i = 0;
            while (i < rootNode.ChildNodes.Count)
            {
                var curnod = rootNode.ChildNodes[i];
                if (curnod.Name == "控件")
                {
                    if (curnod.ChildNodes[0].InnerText == name)
                    {
                        curnod.ChildNodes[1].InnerText = newtxt;
                    }
                }
                i++;
            }
            doc.Save(path);

        }
        public bool Checkhave(string formName, string name, string newtxt)
        {
            if (doc == null)
            {
                doc = new XmlDocument();
                doc.Load(path); //获取xml根节点
            }
            XmlNode xmlRoot = doc.DocumentElement; //根据节点顺序逐步读取 
                                                   //var nameNodeList = xmlRoot.SelectNodes("控件"); //读取节点的id属性
            XmlNode rootNode = doc.SelectSingleNode(formName);

            int i = 0;
            while (i < rootNode.ChildNodes.Count)
            {
                var curnod = rootNode.ChildNodes[i];
                if (curnod.Name == "控件")
                {
                    if (curnod.ChildNodes[0].InnerText == name)
                    {
                       return true;
                    }
                }
                i++;
            }
            return false; 

        }
        public void SaveXml(string path)
        {
            doc.Save(path);
        }

    }


    public class SetFormText
    {
        XML CurXml = new XML();
        Form mm = new Form();
   
        public void GetControlsFromForm(Form fm)
        {
            Hashtable hashText = new Hashtable();
            GetSubControls(fm.Controls, hashText);
            CurXml.CreatElementByHashTb(fm.Name,   hashText);
        }

        /// <summary>
        /// 通过哈希表数据,设置控件的显示名
        /// </summary>
        /// <param name="controls">父控件</param>
        /// <param name="hashResult">哈希表</param>
        private static void SetSubControls(Control.ControlCollection controls, Hashtable hashText)
        {
            try
            {
                foreach (Control control in controls)
                {
                    if (control.GetType() == typeof(Panel))     //Panel
                    {
                        SetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(GroupBox))     //GroupBox
                    {
                        SetSubControls(control.Controls, hashText);
                    }

                    else if (control.GetType() == typeof(TabControl))       //TabControl
                    {
                        SetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(TabPage))      //TabPage
                    {
                        SetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(DataGridView))
                    {
                        SetHeaderCell((DataGridView)control, hashText);
                    }


                    else if (control.GetType() == typeof(RadioButton))     //RadioButton
                    {
                        SetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(FlowLayoutPanel))     //FlowLayoutPanel
                    {
                        SetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(TableLayoutPanel))     //TableLayoutPanel
                    {
                        SetSubControls(control.Controls, hashText);
                    }
                    else if (hashText.Contains(control.Name))
                    {
                        control.Text = (string)hashText[control.Name];
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        /// <summary>
        /// 将控件列表中获取名字和显示名提取到哈希表
        /// </summary>
        /// <param name="controls"></param>
        /// <param name="hashText"></param>
        private static void GetSubControls(Control.ControlCollection controls, Hashtable hashText)
        {
            try
            {
                foreach (Control control in controls)
                {
                    if (control.GetType() == typeof(Panel))     //Panel
                    {
                        GetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(GroupBox))     //GroupBox
                    {
                        GetSubControls(control.Controls, hashText);
                    }

                    else if (control.GetType() == typeof(TabControl))       //TabControl
                    {
                        GetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(TabPage))      //TabPage
                    {
                        GetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(DataGridView))
                    {
                        GetHeaderCell((DataGridView)control, hashText);
                    }
                    else if (control.GetType() == typeof(RadioButton))     //RadioButton
                    {
                        GetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(FlowLayoutPanel))     //FlowLayoutPanel
                    {
                        GetSubControls(control.Controls, hashText);
                    }
                    else if (control.GetType() == typeof(TableLayoutPanel))     //TableLayoutPanel
                    {
                        GetSubControls(control.Controls, hashText);
                    }
                    else if (!hashText.Contains(control.Name))
                    {
                        hashText.Add(control.Name, control.Text);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 设置DataGridView的列头
        /// </summary>
        /// <param name="dataGridView">DataGridView名</param>
        /// <param name="hashResult">哈希表</param>
        private static void SetHeaderCell(DataGridView dataGridView, Hashtable hashHeaderText)
        {
            foreach (DataGridViewColumn column in dataGridView.Columns)
            {
                if (hashHeaderText.Contains(column.Name.ToLower()))
                {
                    column.HeaderText = (string)hashHeaderText[column.Name.ToLower()];
                }
            }
        }
        /// <summary>
        /// 获取表头名称和显示保存到哈希表内
        /// </summary>
        /// <param name="dataGridView"></param>
        /// <param name="mtb"></param>
        private static void GetHeaderCell(DataGridView dataGridView, Hashtable mtb)
        {
            foreach (DataGridViewColumn column in dataGridView.Columns)
            {
                mtb.Add(column.Name.ToLower(), column.HeaderText);

            }
        }


        public void UpdateControlFromFile( Form  fm)
        {
            Hashtable mtb = new Hashtable();
            OpenFileDialog openfile = new OpenFileDialog();
            if (openfile.ShowDialog() == DialogResult.OK)
            {
                CurXml.path = openfile.FileName;
               mtb= CurXml.GetHasTbFromXml(fm.Name,CurXml.path);
            }
            SetSubControls(fm.Controls, mtb);
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值