0

I have a winforms user control that comprises of a single TableLayoutPanel. The user control has an ObservableCollection<Label> as a public property. Each Label should be shown in a row of the TableLayoutPanel. I want to be able to add or remove Labels either at design-time or programmatically.

I added an instance of the user control to the main form using the designer. The collection shows in the property-window of the designer, and upon clicking on it, a window opens where I can add/remove Labels and change the properties of each single Label. When I add Labels they do show up in the TableLayoutPanel.

However, when I run the project (having added Labels to the collection prior) the form is empty.

I observed that the designer adds each Label to Form1.Designer.cs as private field (eg., private Label label1;). Though the form is empty (as is the collection) after running the project, the Labels themselves are not deleted.

This is my code:

namespace TestTableLayoutPanel
{
    public partial class TLPsLabels : UserControl
    {
        public TLPsLabels()
        {
            InitializeComponent();

            labels = new();
            labels.CollectionChanged += OnLabelsChanged;
        }


        private ObservableCollection<Label> labels;
        public ObservableCollection<Label> Labels { get { return labels; } }


        private void OnLabelsChanged(object? sender, NotifyCollectionChangedEventArgs e)
        {
                tLP1.RowStyles.Clear();
                tLP1.RowCount = labels.Count;
                tLP1.ColumnCount = 1;

                for (int i = 0; i < tLP1.RowCount; i++)
                {
                    tLP1.RowStyles.Add(new RowStyle(SizeType.Percent, 100.0F));
                    tLP1.Controls.Add(labels[i]);
                    labels[i].Anchor = AnchorStyles.None;
                    // Do some other stuff, like resizing tLP1's parent control, etc.
                }
        }
    }
}

tLP1 is the TableLayoutPanel which should contain the Labels.

I know the problem is labels = new(); in the user control's constructor. It "overrides" the collection made at design-time. However, I have no idea how to solve this. Maybe even another approach is needed?

This code is just for testing/learning. Instead of labels, I want to show other, more complex user controls inside the TableLayoutPanel.

Flipps
  • 11
  • 5
  • You *could* decorate your Labels collection with `[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]`, which forces the Serializer to serialize your new objects. Of course also add `labels.CollectionChanged -= OnLabelsChanged;` in the `OnHandleDestroyed()` override -- BUT, in this case your new Labels are serialized in the Form Container and this has *side effects*: the new Controls (not tested, but I'm quite sure about it) can be reached and modified in the Designer. So also moved. This should cause a `NullReferenceException` -- I.e., you need your own Designer – Jimi May 22 '23 at 13:45
  • In your current implementation, there's nothing that serializes the Controls you add to the TLP, hence, at run-time, nothing is added to the TLP (since there's nothing to add) – Jimi May 22 '23 at 13:48
  • BTW, about custom Designers in .NET 6+, see the notes here: [Using Command Links in Winforms .NET Core](https://stackoverflow.com/a/74935646/7444103) – Jimi May 22 '23 at 14:04
  • I found this: https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/creating-a-wf-control-design-time-features?view=netframeworkdesktop-4.8 However I got stuck at "Define a Custom Control and Its Custom Designer" step 4. I cant inherit from DocumentDesigner class, because it is not found. Do you know about another tutorial for custom designers? I made a .NET Framework 4.7.2 project, btw. – Flipps May 25 '23 at 07:49
  • Nevermind, found this one: https://stackoverflow.com/questions/2785376/how-to-enable-design-support-in-a-custom-control I added reference to System.Design.dll – Flipps May 25 '23 at 08:17
  • That's for .NET Framework. In .NET you (still) need to add that supporting Package – Jimi May 25 '23 at 11:51

0 Answers0