-1

Essentially, I want a user to add comments in the form of labels. I have implemented this no problem, but I want the user to be able to delete the newly created labels.

    comment := TLabel.Create(Self);
    comment.Parent := Form1;

I want to add Labels during runtime, and then fill a combobox or a stringgrid or something similar with the name or any sort of a reference to that label, so that the user can then delete that label.

How would I get the name of the newly created label for a reference ?

Thanks for any help.

gweno10
  • 365
  • 1
  • 5
  • 16

2 Answers2

3

Components that you create yourself don't have a Name assigned unless you explicitly assign one yourself. It is the IDE that synthesizes a Name when you drop a component on the form at design-time.

You can just assign whatever Name you feel like, as long as it's a valid component name (unused, no spaces, and so on), and then the component can be found via its Owner.FindComponent() method, if you don't keep track of the component yourself. Otherwise, put the component into your own list/array so you can find it later.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

The name of a component can be read from its Name property:

comment.Name

You can write to that property to give the component a name that is amenable to showing to users. You'll want to do that when you create a new component dynamically because it won't have a name until you name it.

There's no need for you to use the Name property if you don't wish to. You can use naming of your own choice. For instance you may wish to use names that contain spaces. Or not be constrained from using names that are already in use by the static controls. Hold the components in a dictionary with the name as the key and the component as the value. My advice is that you go down this second path.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    Only if you assign the `Name` when you create it. Controls created dynamically at runtime have no name assigned. – Ken White Mar 02 '15 at 18:22