zoukankan      html  css  js  c++  java
  • 遍历WinForm窗体 根据语言类型设置其控件Text显示

    示例内容:

    Form1 中 有一个Label1
        中文时显示 " 姓名:"
        英文时显示 " Name: "
        开发时 默认显示为 " Name: "
        有一个弹出的提示信息MessageBox.Show

    中文时语言文件的内容:
    Name:===姓名:
    Home Address: ===家庭地址:


    1、语言文件读取并设置控件Text显示的 处理类

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    using System.IO;
    using System.Windows.Forms;

    namespace LngTxtReader
    {
        
    public class LngTxtReader
        {

            
    public Hashtable SetTextByLng(System.Windows.Forms.Form winform, string strLngTxtPath)
            {
                Hashtable ht 
    = new Hashtable();
                
    if (winform == null)
                {
                    
    return ht;
                }

                
    if (File.Exists(strLngTxtPath))
                {
                    
                    
    string strlineContent;
                    
    int iIndex;

                    System.IO.StreamReader file 
    = new System.IO.StreamReader(strLngTxtPath);
                    
                    
    while ((strlineContent = file.ReadLine()) != null)
                    {
                        iIndex 
    = strlineContent.IndexOf("===");//语言文件的字串 以===区隔 key 和 value
                        ht.Add(strlineContent.Substring(0, iIndex).Trim(), strlineContent.Substring(iIndex + 3, strlineContent.Length - iIndex - 3).Trim());
                    }

                    
    if (ht.Count > 0)
                    {
                        winform.Text 
    = GetMsgByLng( winform.Text, ht);
                        fn_FindControl(winform.Controls, ht);
                    }
                }

                
    return ht;
            }

            
    private void fn_FindControl(Control.ControlCollection ctls,Hashtable ht)
            {
                
    foreach (Control ctl in ctls)
                {
                    
    if (ht.Contains(ctl.Text.Trim()))
                    {
                        ctl.Text 
    = ht[ctl.Text.Trim()].ToString();
                    }
                    
    if (ctl.HasChildren)
                    {
                        fn_FindControl(ctl.Controls,ht);
                    }
                }
            }

            
    public string GetMsgByLng(string strMsg, Hashtable ht)
            {
                
    string strMsgWithLng = strMsg.Trim();
                
    if (ht.Contains(strMsg.Trim()))
                {
                    strMsgWithLng 
    = ht[strMsg.Trim()].ToString();
                }
                
    return strMsgWithLng;
            }
        }
       
    }

    2、调用上述处理

    代码
       public Form1()
       {
            InitializeComponent();
            
    #region 语言显示
            
    string currDirPath = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).Directory.FullName;
            
    string strFilePath = currDirPath + "\\PassChcek_Lng1033.txt";
            LngTxtReader.LngTxtReader lngReader 
    = LngTxtReader.LngTxtReader();
            ht 
    = lngReader.SetTextByLng(this,strFilePath);
            
    #endregion
        }

        
    private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(
    new Xys.Comm.LngTxtReader.LngTxtReader().GetMsgByLng("Hello world",ht));
        }
  • 相关阅读:
    Asp.Net实现FORM认证的一些使用技巧(必看篇)
    python3 关联规则Apriori代码模版
    一家人(模拟)
    一家人(模拟)
    积木城堡(dp)
    积木城堡(dp)
    Tensorflow笔记——神经网络图像识别(四)搭建模块化的神经网络八股(正则化,指数衰减学习率,滑动平均等优化)...
    Tensorflow笔记——神经网络图像识别(四)搭建模块化的神经网络八股(正则化,指数衰减学习率,滑动平均等优化)
    保存训练好的机器学习模型
    保存训练好的机器学习模型
  • 原文地址:https://www.cnblogs.com/freeliver54/p/1885333.html
Copyright © 2011-2022 走看看