代码编写没有问题,但在执行的时候 赋值不成功,原因在哪里?public static int changeLanguageShowEachControl3(Form uiForm)
{
Hashtable dictTable = ReadLanguageResource(uiForm.Name, "AppResource_EN.xml"); if (dictTable == null)
return -2010; //????????????????????????????????? 这里不对么?如何改?
foreach (Control tCtrl in uiForm.Controls)
{
if (tCtrl.GetType() == typeof(System.Windows.Forms.Panel))
changeLanguageShowSubControl(tCtrl.Controls, dictTable); if (tCtrl.GetType() == typeof(System.Windows.Forms.GroupBox))
changeLanguageShowSubControl(tCtrl.Controls, dictTable); string ctrlCaption;
ctrlCaption = (string)dictTable[tCtrl.Name]; if (ctrlCaption != null)
{
if (ctrlCaption.Length > 0)
{
//下面执行的 tCtrl.Text 及 (string)dictTable[tCtrl.Name] 产生的值都正常,但最终运行的结果是 tCtrl.Text 没有被改变,即赋值不成功,原因何在?
tCtrl.Text = (string)dictTable[tCtrl.Name];
}
}
}
return 1;
}
{
Hashtable dictTable = ReadLanguageResource(uiForm.Name, "AppResource_EN.xml"); if (dictTable == null)
return -2010; //????????????????????????????????? 这里不对么?如何改?
foreach (Control tCtrl in uiForm.Controls)
{
if (tCtrl.GetType() == typeof(System.Windows.Forms.Panel))
changeLanguageShowSubControl(tCtrl.Controls, dictTable); if (tCtrl.GetType() == typeof(System.Windows.Forms.GroupBox))
changeLanguageShowSubControl(tCtrl.Controls, dictTable); string ctrlCaption;
ctrlCaption = (string)dictTable[tCtrl.Name]; if (ctrlCaption != null)
{
if (ctrlCaption.Length > 0)
{
//下面执行的 tCtrl.Text 及 (string)dictTable[tCtrl.Name] 产生的值都正常,但最终运行的结果是 tCtrl.Text 没有被改变,即赋值不成功,原因何在?
tCtrl.Text = (string)dictTable[tCtrl.Name];
}
}
}
return 1;
}
不带命名空间,试一下看行不行
private void button1_Click(object sender, EventArgs e)
{
ConfigItemEdit aa = new ConfigItemEdit();
LoadSqlConfig.changeLanguageShowEachControl3(aa);}
private void button1_Click(object sender, EventArgs e)
{
ConfigItemEdit aa = new ConfigItemEdit();
LoadSqlConfig.changeLanguageShowEachControl3(aa);}
{
if (tCtrl is Panel)
changeLanguageShowSubControl(tCtrl.Controls, dictTable); if (tCtrl is GroupBox))
changeLanguageShowSubControl(tCtrl.Controls, dictTable); string ctrlCaption;
ctrlCaption = (string)dictTable[tCtrl.Name]; if (ctrlCaption != null)
{
if (ctrlCaption.Length > 0)
{
if(tCtrl is TextBox)
tCtrl.Text = (string)dictTable[tCtrl.Name];
}
}
}你的tCtrl是遍历了窗口的所有控件 如果当前的tCtrl控件没有Text属性怎么办,当然就报错了
我晕倒,你传进去的这个aa是new出来的,又不是当前显示的窗体,当然对当前的窗体无效啦,改成这样试试:private void button1_Click(object sender, EventArgs e)
{
LoadSqlConfig.changeLanguageShowEachControl3(this);}
string ctrlCaption;
ctrlCaption = (string)dictTable[tCtrl.Name]; if (!string.IsNullOrEmpty(ctrlCaption))
{
tCtrl.Text = ctrlCaption;
}
foreach中不能添加删除元素,但是可以改变元素的属性的
Text属性是在Control中声明的,所以不存在“没有”的问题,最多控件不处理它,否则不要说运行了,连编译都通不过。
控件不处理它,所以就造成了tCtrl.Text 没有被改变
要在给tCtrl.Text属性赋值的时候,判断下当前控件是否支持Text属性
根据楼主提供的代码,哪些控件的Text属性要更新是由配置文件决定的,也就是说,在开发的时候就已经知道哪些控件要更新Text属性哪些不更新,因此判断控件是否支持Text属性是没有必要的。而且,要判断一个控件是否处理Text属性好象不太现实,因为控件本身没有一个属性或方法告诉你是否支持Text,而你不可能穷尽所有的控件类型。不过对于楼主这种方式我也不太敢荀同,因为他是通过配置文件里的Key绑定到控件,这样在正常使用时是没有问题的,但如果有人恶意修改了配置文件就容易发生不必要的问题,比较好的做法是反过来,由控件决定到绑定到字典中的哪个Key。