label inside usercontrol not visible

1.2k views Asked by At

.NET 4.6.1. I have created a windows forms usercontrol. I have changed it to inherit System.Windows.Forms.TreeView instead of Control. In it, i have added (in the designer) a label, which i decide inside the control when will the label be shown. The control is placed in a form and at runtime it is filled with a tree structure (which is done properly). The problem is that the label is not shown when its Visible property is set to True at runtime.

Public Class Treeview111
Inherits System.Windows.Forms.TreeView
...
Public Sub ShowLabel
Label1.Visible=True
End Sub

Even if the Visible property is set to be True in the designer, the Label is not shown. I have tried dynamically adding the Label at runtime but no luck. I have also tried changing it from Friend to Public but nothing. BringToFront did not help either. Checked its Location and it seems to be inside logical values (45,72).

Can't think of anything else. Am i missing something? Is what i'm trying to achieve even possible?

1

There are 1 answers

0
FaultyOverflow On BEST ANSWER

Thanks to Hans Passant's reply, i was pointed to the right direction. I had added through Visual Studio's UI a Label to the UserControl, expecting that it would add all the necessary code to the source files, as done almost always by VS. It appears that VS2013, when you add a control in a usercontrol which inherits from another control, not all elements are automatically added in the source code. In this case the following statement was not automatically added by VS in the Designer.vb file as i expected:

Me.Controls.Add(Label1)

I manually added it and problem solved.