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

这个博客介绍了一种在Windows Forms应用程序中实现中英文切换的方法,通过使用XML文件存储控件名称和显示名,实现了批量更改控件的显示名称。主要涉及XML文件的读写操作,以及对窗体控件的遍历和更新。

在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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值