使用集合来管理多个表单 [英] Using Collections to manage multiple Forms

查看:72
本文介绍了使用集合来管理多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解收藏,不得不承认我真的在为此苦苦挣扎.我看到了很多将变量添加到arraylist的示例.我正在尝试构建一个PostIT note应用程序(会有人说他还在这样做吗!"),而收藏是前进的道路.我模拟了一个非常快速的应用程序,以测试创建新表单并将其添加到Arraylist中.



I''m trying to understand collections and have to admit I''m really struggling with it. I have seen many examples where variables are added to an arraylist. I am trying to build a PostIT note application (there will be people saying "is he still doing that!!!") and collections are the way forward. I mocked up a very quick application to test creating a new form and adding it an Arraylist.



namespace test2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public ArrayList test = new ArrayList();

        private void btnAdd_Click(object sender, EventArgs e)
        {
            test.Add(new Form2());
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            test.RemoveAt(0);
        }
    }
}



Form2在Form2构造函数中仅具有"this.Show();"来显示自身.我不会通过在其中发布代码来节省空间.

只要单击添加"按钮,代码就会创建一个Form2对象.我需要帮助的删除"按钮.仅出于测试目的,每按一次,我将从Arraylist中删除第一个对象.我正在检查存储在测试中的对象的数量,并且确实在减少它的数量.我希望从Arraylist中删除该对象也会删除该对象本身.

我猜想删除arraylist的对象只会删除指向它的指针,所以我还需要关闭窗体.是否引发了我应该使用的事件,还是有更好的方法?请记住,我正在尝试自学,并且掌握的知识有限,因此请越简单越好.



And Form2 just has ''this.Show();'' in the Form2 constructor to display itself. I''ll save space by not posting the code in it.

The code creates a Form2 object whenever I click the ''Add'' button which is good. Its the ''Remove'' button that I need help with. Just for testing, I am removing the first object from the Arraylist with each press. I am checking the number of objects stored in test and it is indeed decreasing as it should. I expected that removing the object from Arraylist would also remove the object itself.

I am guessing that removing the object for arraylist just removes the pointer to it so I need to also close the form. Is an event raised that I should use or is there a better way? Please bear in mind that I am trying to teach myself and have quite a limited knowledge so simpler the better please.

推荐答案

在Remove方法中,您可以很好地获得窗体实例,将其关闭,然后将其从ArrayList中删除.为了安全起见,可以使用List<Form>代替ArrayList.
In the Remove method, you can very well get the form instance, close it and then remove it from the ArrayList. And just to be type safe, you can use a List<Form> instead of an ArrayList.


假设您使用test.RemoveAt(0),它将从Arraylist中删除.

如果要关闭该表单,请使用以下代码:
Suppose you use test.RemoveAt(0) it will just remove from the Arraylist.

If you want to close that form then use following code:
((Form)test[0]).Dispose();


如果要显示该表单,请使用Show()代替Dispose.

试试这个,希望它对您有用.

问候!
Aman


if you want to show that form then use Show() instead of Dispose.

Try this, Hope it will work for you.

Regards!
Aman


解决方案1和2可以正确解决您的紧迫问题.我想对收藏更为笼统.

如您所料,从集合中删除项目不会破坏该项目-.Net集合不会占有"您放入其中的项目的所有权.通常这是一件好事,因为您将在其他地方引用,并且这些引用应该仍然有效(如果您不这样做,则该项目无论如何都会超出范围,并在GC周期中被销毁).但是,使用UI元素,框架会维护对它们的引用,以使它们不会超出范围,并且您的应用程序可能会失去对它们的跟踪.

如果您使用实现INotifyCollectionChanged(即ObservableCollection< Form>)或IBindingList(例如BindingList< T>)的列表类,则更改集合时会发生事件.但是对于List< T> (默认情况下,对于此类可扩展列表,应该使用什么),没有事件.

在这种情况下,我建议您切换逻辑.关闭表单应将其从集合中删除(无论如何您都无法对其进行任何操作),您可以通过挂钩到表单的
Solutions 1 and 2 correctly solve your immediate problem. I want to be more general about collections.

As you''ve surmised, removing an item from a collection does not destroy that item – .Net collections do not ''take ownership'' of items you put into them. This is generally a good thing, as you will have references elsewhere and those should remain valid (and if you don''t, the item will go out of scope anyway and be destroyed in a GC cycle). However, with UI elements, the framework maintains references to them so they don''t go out of scope and your application can lose track of them.

If you use a list class which implements INotifyCollectionChanged (i.e. ObservableCollection<Form>) or IBindingList (e.g. BindingList<T>), there is an event when the collection is changed. But for List<T> (what you should use by default for typed extensible lists like this), there are no events.

In this case I would recommend switching the logic. Closing the form should remove it from the collection (you can''t do anything with it anyway), which you can do by hooking onto the form''s FormClosed[^] event:
List<Form> test = new List<Form>();

void Add(){
 Form2 form2 = new Form2();
 form2.FormClosed += (s,e) => test.Remove((Form)sender);
}



然后,可以通过关闭表单来删除该表单,这将导致FormClosed触发(如果允许进行关闭),因此该表单将从列表中删除:



Then, you can cause a form to be removed by closing it, which will cause FormClosed to fire (if the close is allowed to happen) and thus the form to be removed from the list:

void RemoveFirst(){
 test[0].Close();
}


这篇关于使用集合来管理多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆