如何从里边的控件获取外边的控件

其实我是真性情 2010-08-02 01:27:50
首先我定义了一个Form,在里边加了一个System.Windows.Forms.Panel,然后又在Panel里加入了一个Button,我现在想让点击button的时候隐藏Panel怎么做,或者说我如何在Button的click事件里获取到Panel的引用呢?

第2个问题是,我的Form里有多个Panel,我想点一下就选中一个Panel,要怎么做?
刚接触C#不久希望大家指点
...全文
65 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hyblusea 的回复:]
在Click事件中
Panel pnl = (Panel)((Control)sender).Parent;
pnl.Visible =true;
[/Quote]
3Q
我是这么写的
Panel pnl = (Panel)((Button)sender).Parent;
pnl.Hide();
ljjwf 2010-08-02
  • 打赏
  • 举报
回复
this.Button1.Parent.Visible = false;
jarod0407 2010-08-02
  • 打赏
  • 举报
回复
第一个问题
Panel panel = btn.Container as Panel;

第二个

foreach (Control control in this.Controls)
{
if (control.GetType() == typeof(Panel))
{
Panel panel = control as Panel;
panel.Select();
}
}
hyblusea 2010-08-02
  • 打赏
  • 举报
回复
在Click事件中
Panel pnl = (Panel)((Control)sender).Parent;
pnl.Visible =true;
hyblusea 2010-08-02
  • 打赏
  • 举报
回复
btn.parent

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧