Hi, everybody! I'm currently building a C++ Forms application for my supervisor to track employee sick/vacation time. The program reads each employee's information from a file, and I want to be able to display each employees information in a label.
(The binary files have been created successfully and the file streaming is functioning)

However, the amount of employees is subject to change, so I don't want to have to create and hide 800 labels. :P

I've been searching for information on how to dynamically create and delete labels at runtime but all I seem to find is info for C# or VB.

Any sort of link or advice would be greatly appreciated.

Thanks!

Dani AI

Generated

Brief, practical guidance tied to the thread: runtime labels are perfectly possible (as and noted), but for a changing number of employees it’s usually better to avoid hundreds of standalone Label controls. Labels are fine for a few items, but when the list grows (dozens or hundreds) a tabular or virtualized control gives far better performance, selection, sorting, and editing (ListView in report mode, DataGridView / TStringGrid, or an owner-drawn list).

VCL notes (clarification to ): the constructor’s Owner argument controls lifetime; the Parent property controls where a control is shown. If the Form (or a container like TPanel/TFlowPanel) is passed as Owner, the form will free the component when destroyed; otherwise the code that created the label must free it. If a created label is invisible, check Parent, Visible, bounds (Left/Top inside client area) and Z-order (BringToFront). For layout, prefer TFlowPanel or a grid-like container instead of manual absolute positioning when many items exist.

WinForms / C++/CLI notes (clarification to ): adding a control to a container’s Controls collection makes it visible and ties lifetime to that collection; remove it with Controls->Remove and Dispose (delete in C++/CLI triggers Dispose). Never modify controls from a background thread — marshal updates with Invoke/BeginInvoke or similar.

Practical checklist and troubleshooting tips:

  • Prefer a data-bound control for employee lists (DataGridView/ListView virtual mode) to avoid creating hundreds of child controls.
  • If labels are required, create them inside a scrolling container (Panel/FlowLayoutPanel), store handles in a list for easy removal, and reuse controls where possible.
  • When reading files on a background thread, parse into an in-memory list, then marshal a single UI-thread update to populate/refresh controls.
  • If performance or flicker appears, switch to owner-drawn/virtual mode controls.

These patterns keep lifetime and threading correct while giving a maintainable UI for a variable employee count.

Recommended Answers

All 3 Replies

As far as VCL is concerned, a Label is created at runtime as:

TLabel *myLabel = new TLabel(<Owner Handle>);

It's not too bad:

Label^ lbl = gcnew Label();
//fill in the Location property (specify by System::Drawing::Point(x,y))
//fill in the Text property
Controls->Add(lbl);

Awesome, thanks a bunch!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.