C# 用于删除所选按钮的关联菜单
我在选项卡控件中创建了一个自定义控件,通过拖动选定选项卡上的文件,该控件在每个选项卡上都包含一个flowLayoutPanel。我有一个上下文菜单重命名和删除标签页,但我也想能够删除一个按钮时,我右键点击它,并选择“删除”。。。我无法找到仅删除所选按钮的方法 这就是我必须创建的按钮:C# 用于删除所选按钮的关联菜单,c#,button,custom-controls,tabcontrol,flowlayoutpanel,C#,Button,Custom Controls,Tabcontrol,Flowlayoutpanel,我在选项卡控件中创建了一个自定义控件,通过拖动选定选项卡上的文件,该控件在每个选项卡上都包含一个flowLayoutPanel。我有一个上下文菜单重命名和删除标签页,但我也想能够删除一个按钮时,我右键点击它,并选择“删除”。。。我无法找到仅删除所选按钮的方法 这就是我必须创建的按钮: public void Form1_DragDrop(object sender, DragEventArgs e) { string[] fileList = e.Data.GetDa
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);
}
}
在rmv_btn_click事件中,我如何获得作为发送者右键单击的按钮,以知道要删除哪个按钮?如果我理解您的意思,您需要使用类似的方法
private void rmv_btn_click(object sender, System.EventArgs e)
{
fl_panel.Controls.Remove(sender as Button);
}
私有void rmv_btn_单击(对象发送者,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);
};
}
我认为,通过这种方式,发送者就是上下文菜单中的“删除”按钮。我需要知道在右键单击该按钮并从上下文菜单中选择“删除”时要删除哪个按钮。您可以尝试使用TabIndex属性,只需将其放入EventArgs并使用RemoveAt()但是TabIndex将如何帮助我知道我右键单击要删除的按钮?你能给我更多关于如何使用它的详细信息吗?