label1.Text = file2.ReadIni("Items", "Itm" + 1.ToString(), "");
label2.Text = file2.ReadIni("Items", "Itm" + 2.ToString(), "");
label3.Text = file2.ReadIni("Items", "Itm" + 3.ToString(), "");
label4.Text = file2.ReadIni("Items", "Itm" + 4.ToString(), "");
label5.Text = file2.ReadIni("Items", "Itm" + 5.ToString(), "");
我想用一个循环去代替,该如何写?我的写法如下,但调试不通过不知道为什么, for (int i = 1; i <= 5; i++)
{
string s = "label" + i;
Label c = this.Controls[s] as Label;
c.Text = file2.ReadIni("Items", "Itm" + i.ToString(), "");
}调试的时候 提示最后一行“未将对象引用设置到对象的实例。”
希望高手能赐教
谢谢!!!!
解决方案 »
- c#错误,非静态的字段、方法或属性 要求对象引用.
- 2005发布部署、打包时候微软框架包问题,客户机怎么能够自动安装框架包
- 如果C#中使用大量DLLimport 来编程
- listView控件用于显示数据时,怎么处理数据的右对齐!(detail显示)
- 我创建了一个hash表,如何通过值得到它对应的key呢?
- 我在C#中如何返回,在Oracle 中的数据集合!急
- WPF DataGrid 一行的单击事件怎么实现。
- C#实现FTP客户端登录报错
- C#问题请教高手
- 在c#中如何实现随机数。
- c#做的C/S服务器,能负载多少个客户端连接??????我没经验
- fckeditor 添加属性???高手进。。谢谢
Controls从0开始,修改为this.Controls[s-1]
{
string s = "label" + i;
Label c = this.FindControl(s) as Label;
c.Text = file2.ReadIni("Items", "Itm" + i.ToString(), "");
}
2、检查file2是否实例化。
如果在groupBox1里:
Label c = this.Controls[s] as Label; ---------------》》》》
Label c = groupBox1.Controls[s] as Label;
private List<Label> lb = new List<Label>();foreach (Control control in this.Controls)
{
if (control.GetType().ToString() == "System.Windows.Forms.Label")
{
for (int i = 0; i < 5; i++)
{
if (control.Name == "label" + (i + 1).ToString())
{
lb.Add((Label)control);
}
}
}
}for(int i=0;i<5;i++)
{
lb[i].Text= file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");}
Label c = this.Controls(s) as Label;
LZ的label应该不是直接放在form里的,找找,应该是放在别的容器里了
那么你的Label c = this.Controls[s] as Label;
要将this换成你装Label 的容器名
{
string s = "label" + i;
//Label c = this.Controls[s] as Label;
Label c = this.Controls.Find(s,true)[0] as Label;
c.Text = file2.ReadIni("Items", "Itm" + i.ToString(), "");
}换成上面的代码就OK了
这位楼主的方法就可以啊;不行就用数组:
Label[] lab = new Label[]{label1,label2,label3,label4,label5};
for(int i =0;i< 5; i++)
{
lab[0].Text = file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");
lab[1].Text = file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");
lab[2].Text = file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");
lab[3].Text = file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");
lab[4].Text = file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");
}
你的Label也要动态添加才行啊
是我写的一个类的实例 系统好像也有INI文件操作的类吧 你要感兴趣我可以发给你 呵呵!!
刚想调试代码结果客户来找我调试东西去啦一直没时间看帖子!!