Does a windows form have anything to uniquely identify it?
I have a bunch of forms that I need to put in a list and know if the form is already in there.
For reasons not relevant to this question, I cannot actually have a reference to the form in开发者_运维技巧 the list.
Is there a ID or Guid that is on a winform that I could use? Or should I just add a Guid to all my forms?
Note that this is Compact Framework. So the "ID" needs to be available in the .NETCF 3.5.
The window handle (hWnd
) should do the trick. Look for the Handle
property of your forms.
I'm not aware of any ID present in a winform. Thus I believe your approach of having a GUID for each form instance could be a viable solution. Your form is just a class, an object with properties, I don't see a particular reason why MS would of added an ID to identify a particular form, since this is really a specific requirement, just my opinion.
I don't know much about the Compact Framework. I would look for the Form.Tag
property. If there is one, use it. This property is meant to be used for programmer's will.
Otherwise, you could just generalize a FormBase class from which your other forms would inherit. Put in it the Guid property so that you will access it to verify a form's existance in the list.
Besides, the Contains
method takes an object as an argument, so passing an instance of your expected form would tell you whether your form is in the list. Although this might be a bit overkill, since you have to instantiate your form to verify its existance into the list.
Another way around is the use of a Dictionary<Tkey, TValue>
, where you can add the name of a form for the TKey part of the dictionary, and your form as for the TValue.
Form.Name (inherited from Control) can also do the trick.
精彩评论