【发布时间】:2016-02-17 10:32:01
【问题描述】:
我正在尝试折叠面板。
为此,我创建了一个表格布局并放置了 3 个可折叠面板,我使用了以下代码
private void ButtonClick(System.Object sender, System.EventArgs e)
{
Label lbl = (Label)sender;
Panel pnl = lbl.Parent;
foreach (Panel p in TableLayoutPanel1.Controls) {
Label l = (Label)p.Controls(0);
if (p.Equals(pnl)) {
//expand or collapse the panel
if (p.Height == 150) {
p.Height = 25;
//Change the imaqge name to YOUR image
l.Image = My.Resources.Expander_Collapsed16;
} else {
p.Height = 150;
//Change the imaqge name to YOUR image
l.Image = My.Resources.Expander_Expanded16;
}
} else {
p.Height = 25;
//Change the imaqge name to YOUR image
l.Image = My.Resources.Expander_Collapsed16;
}
}
}
在该代码中,我在以下三行中出错
panel pnl=lbl.Parent
Label l = (Panel)p.Controls;
我做错了什么?
【问题讨论】:
-
有什么异常?
-
For First Line 获取或设置控件的父容器
-
那么对于发送行不能将'System.Windows.Forms.Controls.ControlCollection'类型转换为'System.Windows.Forms.Panel'
-
您确定 TableLayoutPanel1 中的所有控件都是面板吗?您确定每个面板中的所有控件都是标签吗?
-
是的,首先我放了一个表格布局面板,其中放了 2 个单元格。每个单元格都有一个带有面板标签列表框的面板