简体   繁体   English

上下文菜单删除所选按钮

[英]context menu to delete selected button

I create a custom control inside a tab control which contains a flowLayoutPanel on every tab by dragging files on the selected tab. 我在选项卡控件中创建了一个自定义控件,该控件通过拖动所选选项卡上的文件在每个选项卡上包含一个flowLayoutPanel。 I have a context menu to rename and delete tab pages, but i want also to be able to delete a button created when I right click on it and select "remove"... I cannot find a way to delete only the selected button.. 我有一个上下文菜单可以重命名和删除标签页,但是我还希望能够删除在右键单击并选择“删除”时创建的按钮。我找不到只删除所选按钮的方法。 。

This is what I have to create the buttons: 这是我必须创建的按钮:

  public void Form1_DragDrop(object sender, DragEventArgs e)
    {
        string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];

        foreach (string s in fileList)
        {
            var button = new Button();
            path_app = String.Format("{0}", s);
            string filename = path_app;
            file_name = Path.GetFileName(path_app);

          Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filename);
          Bitmap bmp = icon.ToBitmap();
          CustomControl custom_btn = new CustomControl(button, new Label { Text = file_name });
          button.Tag = path_app;
          button.BackgroundImage = bmp;
          button.BackgroundImageLayout = ImageLayout.Stretch;
          FlowLayoutPanel selectedFLP = (FlowLayoutPanel)tabControl1.SelectedTab.Controls[0];
          selectedFLP.Controls.Add(custom_btn);
          button.Click += new EventHandler(button_Click);

            ContextMenu cm2 = new ContextMenu();
            cm2.MenuItems.Add("Remove", new EventHandler(rmv_btn_click));
            custom_btn.ContextMenu = cm2;
        }
    }

    private void rmv_btn_click(object sender, System.EventArgs e)
    {
        foreach (Control X in fl_panel.Controls)
        {
            fl_panel.Controls.Remove(X);
        }
    }

How do I get the button which I right click on it as sender in the rmv_btn_click event to know which one to delete? 如何获得在rmv_btn_click事件中右键单击作为发送者的按钮,以知道要删除哪个按钮?

If I understand what you mean, You need to use something like this. 如果我理解您的意思,则需要使用类似的内容。

private void rmv_btn_click(object sender, System.EventArgs e)
{
  fl_panel.Controls.Remove(sender as Button);
}

private void rmv_btn_click(object sender, System.EventArgs e) { 私人void rmv_btn_click(object sender,System.EventArgs e){

        Button btn = new Button();
        Label lbl = new Label();
        CustomControl cst_btn = new CustomControl(btn, lbl);
        cst_btn = sender as CustomControl;
        DialogResult dialogResult = MessageBox.Show("Are you sure that you want to remove this object?", "Remove object", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            cst_btn.Dispose();
        }
        else if (dialogResult == DialogResult.No)
        {
            //do nothing
        }


    }

    public EventHandler handlerGetter(CustomControl button)
    {
        return (object sender, EventArgs e) =>
        {
            rmv_btn_click(button, e);
        };
    }

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

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