What is used for a key in System.Windows.Forms.Control.ControlCollection?

So, a System.Windows.Forms.Control has a Controls property of type Control.ControlCollection . I have control over a form that contains a bunch of small sub-controls in this collection. These sub-controls have a label and a text identifier, which is the name of the field from the database.

I need to go back to the Controls collection and find the controls by name. ControlCollection has public virtual Control this[string key] { get; } public virtual Control this[string key] { get; } and a public virtual bool ContainsKey(string key) , so it looks like I can find them.

However, the add function ( public virtual void Add(Control value) ) does not accept the key string, just adding System.Windows.Forms.Control , and all my ContainsKey calls return false.

Computing something on Control should be redefined as a key (since only Control is passed), I tried to override ToString() to return the name of the database field (which I want to use for the search), but ContainsKey still returns false when I I know that the control for the specified field is present.

In the documentation for this[string key] { get; } this[string key] { get; } that the key parameter is "The name of the control to retrieve from the control collection." Control does not have a Name property that I can override, its only Name property contains the name of the class, which will be the same for every control that I add. The documentation for ContainsKey(string key) says that the key parameter is “Search Key,” which is even less useful.

Found the answer, but I already wrote all this to publish it, and then answer it myself if someone can find it useful ...

+4
source share
1 answer

While I cannot override the Name property, it is {get; set;}, not pure {get;}, as I stupidly thought. Therefore, if I set the Name my control in the database name field, before adding it to the Controls collection, I can find it as expected.

The answer was in the final documents of the Control[] Find(string key, bool searchAllChildren) method Control[] Find(string key, bool searchAllChildren) , and not in the documents for the functions that I was going to use: "Looks for controls using the System.Windows.Forms.Control.Name property and builds an array of all controls that match. "

+7
source

Source: https://habr.com/ru/post/1389109/


All Articles