C# windows应用程序中的可折叠面板
我正在尝试可折叠面板 为此,我创建了一个表布局,并为可折叠放置了3个面板,我使用了以下代码C# windows应用程序中的可折叠面板,c#,window,C#,Window,我正在尝试可折叠面板 为此,我创建了一个表布局,并为可折叠放置了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 = (
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;
我做错了什么?是的,我以前做过……虽然没有取得很大的成功,但它可以关闭面板
private void pnSearch_MouseHover(object sender, EventArgs e)
{
ReshapePanel(pnSearch);
}
private void ReshapePanel(Panel p)
{
int targetSize;
int threshold;
int changeDelay = 8000;
decimal direction;
if (p.Width == 0)
{ //Expand
targetSize = 200;
threshold = 195;
direction = 1;
}
else
{ //Contract
targetSize = 1;
threshold = 5;
direction = 2;
}
do
{
if (pnSearch.Width > threshold)
{
pnSearch.Width = targetSize;
}
else
{
if (direction == 2)
{
pnSearch.Width = pnSearch.Width + 1;
}
else
{
pnSearch.Width = pnSearch.Width - 1;
}
//pnSearch.Width = pnSearch.Width + parse.int(1 * direction);
Task.Delay(changeDelay);
}
} while (pnSearch.Width != targetSize);
}
例外情况是什么?对于第一行,获取或设置控件的父容器,然后对于发送行,无法将类型“System.Windows.Forms.Controls.ControlCollection”转换为“System.Windows.Forms.Panel”是否确实TableLayoutPanel1中的所有控件都是面板?你确定每个面板中的所有控件都是标签吗?是的,首先我放了一个表格布局面板,其中有两个单元格。每个单元格有一个面板,面板内有标签列表框