Stécy
Stécy

Reputation: 12349

How to get a component's name?

From a WinForms Designer, I can get the list of components and if a component is a control I can get its name using the GetName() method on it.

However, if the component is a Component there is no GetName() method. How could I get the name of the component?

By name I refer to the object name given from the property sheet of the winforms designer where the component is located.

Upvotes: 2

Views: 458

Answers (1)

Stécy
Stécy

Reputation: 12349

Never mind, figured out that GetName() was my own extension method on a Control.

I needed to change that to extend a Component instead.

For posterity, here's how you get a control or component name:

public static string GetName(this Component component)
{
    return (string)TypeDescriptor.GetProperties(component)["Name"].GetValue(component);
}

Upvotes: 3

Related Questions