English 中文(简体)
如何正确处置所有动态控制?
原标题:How can i dispose all dynamic controls correctly?
  • 时间:2023-12-08 19:11:08
  •  标签:
  • c#
  • winforms

页: 1 表格1载荷显示Form2:

Form frm = new Form2();

public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    frm.Show();
}

<代码>Form1 有一个纽芬兰文在<>Form2上动态创建10纽芬兰文。

private void btnCreateButtons_Click(object sender, EventArgs e)
{
    for (int t=0;t<10;t++)
    {
        Button button = new Button();
        button.Text = "Button " + t.ToString();
        button.Location = new Point(10, (10+(button.Height*t)));
        frm.Controls.Add(button);
    }
}

在一次点击[Create Buttons]之后,有两种形式。

create dynamic buttons


Then the goal is that when the [Dispose] button is clicked on Form1, it will properly remove and dispose all of the buttons on Form2 that were created in the first step. But I have a problem about destroying the dynamic objects created.

private void btnDisposeAll_Click(object sender, EventArgs e)
{
    foreach(Control control in frm.Controls)
    {
        if (control != null)
        {
            this.Controls.Remove(control);
            control.Dispose();
        }
    }
}

当我想要摧毁这一充满活力的县时,处置方法不能正确操作。 在<代码>Form2中,只有一些有活力的吨位消失。

destroy buttons

what s wrong in my code? thanks

最佳回答

我能够照搬你的法典。 我认为,问题在于重新编辑“<>ControlCollection,同时,你又重新修改了这一汇编。 为了避免出现这一问题,它会形成一个阵列,使你摆脱变化。

public partial class Form1 : Form
{
    Form frm = new Form2();
    public Form1() => InitializeComponent();
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        buttonCreateButtons.Click += CreateButtons_Click;
        buttonDisposeButtons.Click += DisposeButtons_Click;
        frm.Show(this);
        frm.Location = new Point(Right + 10, Top);
    }
    private void CreateButtons_Click(object? sender, EventArgs e)
    {
        for (int t = 0; t < 10; t++)
        {
            Button button = new Button
            {
                Text = $"Button {t}",
                AutoSize = true,
                Margin = new Padding(0,10,0,0),
            };
            button.Location = new Point(10, button.Margin.Top + ((button.Margin.Vertical + button.Height) * t));
            frm.Controls.Add(button);
        }
    }
    private void DisposeButtons_Click(object? sender, EventArgs e)
    {
        foreach (var control in frm.Controls.OfType<Button>().ToArray())
        {
            control?.Dispose();
        }
    }
}

screenshots

问题回答

仅保留表格1中Buttons的,并从那里直接处置。

Form frm = new Form2();
List<Button> buttons = new List<Button>(); // <-- List of Buttons

private void btnCreateButtons_Click(object sender, EventArgs e)
{
    for (int t = 0; t < 10; t++)
    {
        Button button = new Button();
        button.Text = "Button " + t.ToString();
        button.Location = new Point(10, (10 + (button.Height * t)));
        frm.Controls.Add(button);
        buttons.Add(button); // <-- add it to your OWN list!
    }            
}

private void btnDisposeAll_Click(object sender, EventArgs e)
{
    // walk list backwards...
    for(int i=buttons.Count-1; i >= 0; i--)
    {
        buttons[i].Dispose();
    }
    buttons.Clear();
}




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签