简体   繁体   English

C#Winform关闭面板

[英]C# Winform Close a panel

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: 如果您确实想做您描述的事情,则应该知道面板控件没有close方法,您可以:

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 或将其添加到TableLayoutPanelFlowLayoutPanel

在此处输入图片说明

Another option could be using a TabControl and remove unwanted tabs. 另一种选择是使用TabControl并删除不需要的选项卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM