C# Удалить панели из панели - PullRequest
0 голосов
/ 17 апреля 2020

Мне нужно сделать так, чтобы мои панели снимались с Panel. Мне нужно иметь возможность удалить указанную c панель с панели:

  1. панель создается после нажатия кнопки «Создать атрибут»
  2. Каждый атрибут имеет «contextmenustrip»
  3. При нажатии кнопки «Удалить» следует удалить указанный атрибут c.

Пробовал другой сценарий ios, но я смог удалить только последнюю созданную панель. Вероятно, я должен как-то использовать списки?

Не смущайте название панели "Кнопка" - фактически это также панель ... Вот метод, используемый для создания панели (атрибута) и вставки ее в Панель (attributes_panel). Этот метод выполняется из второй формы после нажатия кнопки.

******************* Основная форма *********************** *****

 int attributeTabIndex = 0;
    public PanelAttribute attribute;
    public void CreateNewFormAttribute()
    {

        //------- Creating Panel -------
        attribute = new PanelAttribute();
        TextBox txtBox = new TextBox();
        Label attribute_label = new Label();
        attribute.TabIndex = attributeTabIndex;
        attribute.Name = "attribute" + attributeTabIndex.ToString();
        //------- Creating TextBox -------
        txtBox.Parent = attribute;
        txtBox.Size = new Size(attribute.Size.Width - 4, txtBox.Height);
        txtBox.Location = new Point(3, 25);
        //------- Creating Label -------
        attribute_label.Parent = attribute;
        attribute_label.Location = new Point(3, 5);
        attribute_label.Text = attribute.Name;
        //------- Inserting created "attribute" into "attributes_panel"
        mainPanelOfAttributes.Controls.Add(attribute);
        //------- Adding index increment -------
        attributeTabIndex++;
        //-------
    }

******************** 2-я форма ************** ********

  public void create_btn_Click(object sender, EventArgs e)
    {
        if (_parentForm != null)
        {
            _parentForm.CreateNewFormAttribute();
        }

************* Добавление ContextMenuStrip ****************** *

Выполнено в панели (как атрибут) класс

private void CreateNewOption()
    {
        AttributeOptions newAttributeOption = new AttributeOptions();
        this.ContextMenuStrip = newAttributeOption;
    }

Пример

...