Search code examples
c#.netwinformsuser-controlswindows-forms-designer

Lable dynamical created in OnDraw event and editable from Visual Studio Designer


In concern to a UserControl, how would one create a dynamic number(number from property editable from Visual Studio) of labels whose text can be edited via the Visual Studio "Design" window?

Edit To clarify, I do not want the user to alter the text of labels at run time, instead only the developer who is using the control in their project is able to alter the text of labels inside the Visual Studio Designer by simply clicking on the label and changing the text. (Not the source code itself).

As it stands with what I have, when I add the labels dynamical within an override of the OnDraw event, the labels can be seen but not edited within the design window.

To provide more clarity on my question, I have provided a sample situation to demonstrate the problem. To be specific, I am creating a certain number of labels inside the Draw event like the code below. The number of labels and their location will depend on the number of labels to be drawn which is provided via a publicly accessible property in the control.

    public void CalledFromDrawMethod (string strElement, UserControl ucParentControl, Point pLoc)
    {
        var lblChild = new Label();
        lblChild.Text = strElement;
        lblChild.Location = pLoc;
        ucParentControl.Controls.Add(lblChild);// Add the label
    }

Below is the user control within a form from the view of the "Design" window in Visual Studio. One picture is the user control when 6 labels are to be drawn and the second picture is when 3 labels are to be drawn. Again, this number of labels is specified by a publicly accessible property in the control

enter image description here enter image description here

In the specific situation depicted from the pictures, I would like to be able to click on one of the "DEFAULT" labels and change the label "Text" property.

Version Of Visual Studio: VS 2013 (I also tried with VS 15 and 17 but got the same results) GUI Framework: Winforms


Solution

  • The answer to your question is its not possible to achieve what you are wanting.

    What you are wanting is simply not possible in Visual Studio. If it was possible then there should be some .NET control that has this behavior but there is not. Though it would be nice to have labels created in the OnDraw event behave just as if the label was placed on the form in the Designer, that extra support would of have to be added into Visual Studio. As stated in a previous answer, the best work around for this is to have one collection that can modify the text for those labels in the Visual Studio Designer.