I have created a custom close image label, added it to a larger label, and added that to my panel. I have a bunch of these panels all over. I created a function to make this label collection which I then adds it to the panel. How do I create an event that closes the panel (Parent of Parent?) when a small close-style label is clicked? Here's what I have so far.
public void MakePanel1(string panel_name)
{
Panel MyPanel = new Panel();
Label TitleLabel = AddTitleLabel(panel_name);
MyPanel.Controls.Add(TitleLabel);
this.Controls.Add(MyPanel);
}
public Label AddTitleLabel(string title)
{
Label TitleLabel = new Label();
TitleLabel.Size = new Size(231, 20);
TitleLabel.BorderStyle = BorderStyle.FixedSingle;
TitleLabel.TextAlign = ContentAlignment.MiddleLeft;
TitleLabel.Text = title;
Label CloseLabel = new Label();
CloseLabel.Size = new Size(16, 16);
CloseLabel.Location = new Point(212, 2);
CloseLabel.Image = Image.FromFile(@"..\..\pics\x.png");
CloseLabel.Click += new System.EventHandler(this.DoStuffAndClosePanel);
TitleLabel.Controls.Add(CloseLabel);
return TitleLabel;
}
private void DoStuffAndClosePanel(object sender, EventArgs e)
{
// Do some stuff
// Close the panel -- sender.Close() ?????
}
thanks in advance
If you really want to do what you described you should know the panel control doesn't have a close method and you can:
private void DoStuffAndClosePanel(object sender, EventArgs e)
{
//Do Stuff
//...
//Close Panel
var parent=((Control)sender).Parent;
parent.Visible = false;
parent.Dispose();
parent = null;
}
As another option you can use a Form instead of such panel. You can hide title bar of form and use your close button instead.
For example if you want to have such form:
public class PanelForm:Form
{
protected override void WndProc(ref Message message)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
switch (message.Msg)
{
case WM_SYSCOMMAND:
int command = message.WParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
return;
break;
}
base.WndProc(ref message);
}
}
And the for showing such panel:
var f= new PanelForm();
f.TopLevel=false;
f.FormBorderStyle= System.Windows.Forms.FormBorderStyle.FixedSingle;
f.MinimizeBox=false;
f.MaximizeBox=false;
this.Controls.Add(f);
f.Show();
Or add it to a TableLayoutPanel
, or FlowLayoutPanel
Another option could be using a TabControl
and remove unwanted tabs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.