Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

winforms

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

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

However, the Add function (public virtual void Add(Control value)) doesn't take a key string, just the System.Windows.Forms.Control you're adding, and all my calls to ContainsKey are returning false.

Figuring something on the Control has to be overridden to be the key (since only the Control is passed), I tried overriding ToString() to return the database field name (which I'm wanting to use for the lookup), but ContainsKey still returns false when I know a control for the field specified is present.

The documentation for this[string key] { get; } says the key parameter is "The name of the control to retrieve from the control collection." The Control does not have a Name property I can override, its only Name property contains the class name, which will be the same for every control I'm adding. The documentation for ContainsKey(string key) says the key parameter is "The key to locate", which is even less helpful.

Found the answer, but I already wrote all this up so I might as well publish it and then self-answer in case someone else might find it useful...

like image 619
Chuck Wilbur Avatar asked Jan 03 '12 16:01

Chuck Wilbur


People also ask

What are controls in WinForms?

Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client-side, Windows-based applications. Not only does Windows Forms provide many ready-to-use controls, it also provides the infrastructure for developing your own controls.

What is the use of control class in Windows form creation?

NET Framework. The Control class implements very basic functionality required by classes that display information to the user. It handles user input through the keyboard and pointing devices. It handles message routing and security.

How do you put controls on a form?

Position a control on the design surface of the Windows Forms Designer. In Visual Studio, drag the control to the appropriate location with the mouse. Select the control and move it with the ARROW keys to position it more precisely. Also, snaplines assist you in placing controls precisely on your form.

What are window forms control in VB?

Visual Basic Form is the container for all the controls that make up the user interface. Every window you see in a running visual basic application is a form, thus the terms form and window describe the same entity. Visual Studio creates a default form for you when you create a Windows Forms Application.


1 Answers

While I can't override the Name property, it's {get;set;}, not pure {get;} as I stupidly assumed. So if I set the Name of my control to the database field name before I add it to the Controls collection, I can look it up as expected.

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

like image 111
Chuck Wilbur Avatar answered Sep 28 '22 05:09

Chuck Wilbur