在FlowLayoutPanel控件中动态添加了多个Panel控件,然后循环分别在panel控件中添加Lable,不知道为什么调试结果只有第一个Panel显示label内容。后面的panel均不显示Lable的内容。期待高手解答,谢谢。
for (int i = 0; i < FlowPanel_Station.Controls.Count; i++) { Panel pan = new Panel(); pan = (Panel)FlowPanel_Station.Controls[i]; string[] str = new string[8]; str[0] = "站点名称:"; str[1] = "连接状态:"; str[2] = "监听端口:"; str[3] = "更新时间:"; str[4] = ""; str[5] = ""; str[6] = ""; str[7] = ""; Label[] label = new Label[8]; Point[] point = new Point[8]; for (int k = 0; k < 8; k++) { if (k < 4) { label[k] = new Label(); label[k].Text = str[k]; label[k].Width = 70; pan.Controls.Add(label[k]); int x = pan.Location.X + 5; int y = pan.Location.Y + label[k].Height * k + 10; point[k] = new Point(x, y); label[k].Location = point[k]; } else { label[k] = new Label(); label[k].Text = str[k]; label[k].Width = 70; pan.Controls.Add(label[k]); int x = pan.Location.X + label[k - 4].Width + 5; int y = pan.Location.Y + label[k - 4].Height * (k - 4) + 10; point[k] = new Point(x, y); label[k].Location = point[k]; } } }
FlowPanel_Station.Controls.Count是多少?
是不确定的,这个也是前面动态添加的。
@wzhwang: 如果这个值为1的话你页面只有一个Panel,你跟下代码看看
同问FlowPanel_Station.Controls.Count是多少
因为你给lable定位的时候超出了panel的可见范围,可以把label[k].Location = point[k];注释掉看看。
楼主,请问:
pan = (Panel)FlowPanel_Station.Controls[i];
为什么不能写成:
FlowPanel_Station.Controls.Add(pan);
FlowPanel_Station.Controls 理论上没有被实例化过吧。
也就是默认Controls没有子集,那让他的子集[i]=pan,貌似不太合理
foreach (Panel item in FlowPanel_Station.Controls) for循环改成这个 foreach (Labelitem in Panel .Controls)
循环都用foreach试下