如何删除运行时添加的按钮?

How to delete a runtime-added button?

首先,我是一个真正的 c# 初学者,所以请不要用专家级的东西来回答,保持简单,即使那是一个比它必须的更长的变体,谢谢!

我编写了一个小程序,能够在运行时向表单添加所谓的 "Notes"。

注释是一个 class,它包含两个按钮(删除、保存)和一个文本框。 另一个 class,MyNotes,有一个方法,可以将新的注释添加到表单中,最多可达 max。 4.

到目前为止它运行良好。

现在的问题: 我想删除特定的注释,用户为此按下删除按钮(因此应从表单中删除两个按钮和文本框,但(如果有)其他注释应保持不变。

我已经编写了一个删除方法,但问题是,"remove_Click" 中的 "object sender" 确实提供了一个删除按钮,但没有关于 Note 的信息,他是其中的一部分,或他的注释的其他元素(保存按钮、文本框)。我明白为什么,但我不知道如何解决这个问题。

我如何告诉他他属于哪个 Note?

编辑:save_Click 和 button1_Click 不是其中的一部分,它们还有其他功能,与此无关。

EDIT2:那是程序的图片。上部的 4 个文本框、保存按钮和 x 按钮由 "ADD" 添加。问题是在 4 种情况之一中通过按 "X" 删除一个 "Note-set"。 :-/

Click to see an image of my program

表格:

using System;
using System.Windows;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TextboxTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            richTextBox1.BorderStyle = BorderStyle.None; 
        }

        private void load_Click(object sender, EventArgs e)
        {
            Note note = MyNotes.AddNote();
            if (note !=  null)
            {
                this.Controls.Add(note.GetBox());
                this.Controls.Add(note.GetRemoveButton());
                this.Controls.Add(note.GetSaveButton());

                note.GetRemoveButton().Click += new EventHandler(remove_Click);

            }
            richTextBox1.Text = MyNotes.SetNote();
        }

        private void remove_Click(object sender, EventArgs e)
        {
            Note note = sender as Note;

            Controls.Remove(note.GetBox());
            Controls.Remove(note.GetSaveButton());
            Controls.Remove(note.GetRemoveButton());
        }


        private void save_Click(object sender, EventArgs e)
        {
            String savePath = Properties.Settings.Default.datapath;
            System.IO.File.WriteAllText(savePath, richTextBox1.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 settings = new Form2();
            settings.Show();
            MessageBox.Show("Selected Item Text: " + Properties.Settings.Default.textboxFont + "\n");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.Text = MyNotes.SetNote();
        }

    }
}

Class 我的笔记:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TextboxTest
{
    class MyNotes
    {
        public static List<Note> noteList = new List<Note>();
        public static String path = Properties.Settings.Default.datapath;
        private static int y = 50;



        // Adds a new textbox, save- and remove-Button to the form (and the textBoxList) 
        // up to a maximum of 4 each.

        public static Note AddNote()
        {
            if (noteList.Count < 4)
            {
                Note note = new Note();
                note.GetBox().Location = new Point(100, y);
                note.GetRemoveButton().Location = new Point(15, y-2);
                note.GetSaveButton().Location = new Point(40, y - 2);
                y += 22;
                noteList.Add(note);
                return note;
            }
            else
            {
                MessageBox.Show("It's only possible to set a maximum of 4 notes. Please delete or rewrite another one.");
                return null;
            }
        }

        public static String SetNote()
        {
            return System.IO.File.ReadAllText(path);
        }


    }
}

Class 备注:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TextboxTest
{
    class Note
    {
        private TextBox box;
        private Button remove;
        private Button save;

        public Note()
        {
            boxSettings();
            removeSettings();
            saveSettings();
        }

        private void boxSettings()
        {
            box = new TextBox();
            box.Width = 250;
            box.BackColor = System.Drawing.Color.DarkGray;

        }

        private void removeSettings()
        {
            remove = new Button();
            remove.Text = "X";
            remove.TextAlign = ContentAlignment.MiddleCenter;
            remove.Width = 20;

        }

        private void saveSettings()
        {
            save = new Button();
            save.Text = "SAVE";
            save.TextAlign = ContentAlignment.MiddleCenter;
            save.Width = 50;

        }

        public TextBox GetBox()
        {
            return this.box;
        }

        public Button GetRemoveButton()
        {
            return this.remove;
        }

        public Button GetSaveButton()
        {
            return this.save;
        }


    }
}

在这种情况下,您可以使用 Tag Property。将标记 属性(在每个控件中找到)视为可以容纳对象的占位符。

在您的情况下,在用户单击 "X" 按钮后您想要删除该注释,这里的问题是在单击事件中您获得的是按钮本身而不是注释。在这里,您可以将 Note 对象存储在您的按钮中,以便您可以在 Click 事件中使用它们。

代码:

// Class Note
private void removeSettings()
{
    remove = new Button();
    remove.Text = "X";
    remove.TextAlign = ContentAlignment.MiddleCenter;
    remove.Width = 20;
    remove.Tag = this; // Store the current Note in its remove button
}

// Class Form1
private void remove_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    Note note = btn.Tag as Note;

    Controls.Remove(note.GetBox());
    Controls.Remove(note.GetSaveButton());
    Controls.Remove(note.GetRemoveButton());
}

希望这足够简单!

谢谢,效果很好! :-)

最后一个小时我自己也找到了解决办法。但这要复杂得多(但至少它有效 ^^ - 但我更喜欢你的 xD )。

那是 "gold nugget":

private void remove_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            String s = button.Name;
            s = s.Substring(6);
            int index;

            index = Convert.ToInt32(s);
            Note note = MyNotes.noteList.ElementAt(index);

            this.Controls.Remove(note.GetBox());
            this.Controls.Remove(note.GetSaveButton());
            this.Controls.Remove(note.GetRemoveButton());

            MyNotes.noteList.Remove(note);

            int x = MyNotes.noteList.Count;
            MessageBox.Show(x.ToString());

        }