如何使用一个循环,循环每一个控件,我是要做多语言版本,要循环所有的控件,逐个更改控件text。
                
                foreach (Control item in this.Controls)
                {
                    if (Values[item.Name] != null)
                        item.Text = Values[item.Name].ToString();
                }
//values是读取到配置文件ini的Hashtable   key是控件name,value是控件text值这样写,访问不到控件
另外我又这样写,还是获取不到控件。
                foreach (DictionaryEntry item in Values)
                {
                    if (this.Controls[item.Key.ToString()] != null)
                        this.Controls[item.Key.ToString()].Text = item.Value.ToString();
                }另外请问一下  桌面应用程序做多语言版本还有其他好的方式吗?

解决方案 »

  1.   

    访问不到什么意思,Controls中的组件访问不到?不会吧
      

  2.   

    请参考
    http://www.cnblogs.com/sharemeteor/articles/215069.html
      

  3.   


    foreach (Control item in this.Controls)
    {
        try
        {
              TextBox t = (TextBox)item;
              //是TextBox控件,做你后面的
         }
        catch
        {
              //不是TextBox控件 自己看着办
         }
    }
      

  4.   

    Controls里面不是所有的控件,只有五六个,我界面上有100多个控件的。包括下拉菜单啊,lable啊,很多的,但Controls里没有一个是我要的控件。
      

  5.   


    不是的啦,连控件都访问不到啦,如果能够访问到控件,我直接更改text就好了嘛。关键是Controls循环,好像就几个子控件,并不是界面上全部的控件的。
      

  6.   

    是不是设计器里面的代码Designer.cs出了问题,我之前也遇到过类似情况,小窗体。。直接重新建了个就好了- -!
      

  7.   

    foreach (Controls ct in this.Controls)
    {
        if(ct is TextBox )
        {
           ((TextBox)ct).text=Values[((TextBox)ct).Name].ToString();
        }
    }
      

  8.   

    建议使用本地化
    http://www.microsoft.com/china/MSDN/library/netFramework/netframework/intronetfx30.mspx?mfr=true
      

  9.   

    如果在tab控件里面的,用直接遍历确实访问不了
      

  10.   

    foreach (Control item in this.Controls)
                {
                    if(item is TabControl)
                    {
                        foreach (Control item_sub in item.Controls)
                        {
                            item_sub.Text = "tab动态修改";
                            foreach (Control item_sub2 in item_sub.Controls)
                                item_sub2.Text = "tab子控件动态修改";                    }
                    }
                    else
                        item.Text = "动态修改";
                }
      

  11.   

    我用的resx文件实现的多语言,你的控件是不是放到什么panel里面了,所以controls看不到?
      

  12.   

    是差不多,我的意思是把界面所有文字全部写到xml,生成界面的时候由xml读取出来显示,这样就是支持多语言了吗?多少种语言就用多少个xml行了
      

  13.   

    举个例子:将该界面所有的textbox的text值都设为123 private void findcontrol(ControlCollection con)
        {
            for (int i = 0; i < con.Count; i++)
            {
                if (con[i] is TextBox)
                    ((TextBox)con[i]).Text = "123";
                if (con[i].HasControls())
                    findcontrol(con[i].Controls);
            }
        }
      

  14.   

    图片看不了啊
    感觉用resx文件实现的多语言比较简单项目的Properties下创建几个resx文件,对应多语言,控件的name作为key,value就是要显示的内容就可以了。也不需要读取文件的操作,比较简单。        public void InitFormLanguse(List<Control> iControls, string lag_flg)
            {
                if (iControls.Count > 0)
                {
                    foreach (Control ct in iControls)
                    {                    if (Constant.LAG_CN.Equals(lag_flg))
                        {
                            ct.Text = Properties.CN_Resources.ResourceManager.GetString(ct.Name.ToString());
                        }
                        else if (Constant.LAG_EN.Equals(lag_flg))
                        {
                            ct.Text = Properties.EN_Resources.ResourceManager.GetString(ct.Name.ToString());
                        }
                        else
                        {
                            ct.Text = Properties.JP_Resources.ResourceManager.GetString(ct.Name.ToString());
                        }
                    }
                }
            }
      

  15.   

    图片地址http://hiphotos.baidu.com/%C4%BE%C4%F1%C8%ED%BC%FE/pic/item/fcb8533df8dcd10028515297728b4710b8122f33.jpg  
      

  16.   

    递归啊        public static void SetControlValue(Control parCtl)
            {
                foreach (Control c in parCtl.Controls)
                {
                    if (c is TextBox)
                        c.Text = "aa";
                    SetControlValue(c);
                }
            }