-1

.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?

FaultyOverflow
  • 153
  • 1
  • 9
  • 1
    The most important code is missing, it is not obvious what happened to the constructor (Sub New). The InitializeComponent() call is crucial. While it is meaningless for a class that does not derive from Form or UserControl (you can no longer modify it with the designer), the Me.Controls.Add(Label1) call is important to ensure it can be seen. – Hans Passant Sep 17 '17 at 15:31
  • >>The most important code is missing, it is not obvious what happened to the constructor (Sub New). This seemed pretty obvious to me, because omitting the call to InitializeComponent() produces a warning, so there is no chance of it missing. Furthermore, in VB.NET, the "Me.Controls.Add(Label1)" is automatically added by VS itself in the Form.Designer.vb file. I don't see the need for a downvote, for those two reasons. I came here asking specifically that, where to look because i didn't understand what could cause the problem. Anyway, your reply pointed me to the right direction (see reply). – FaultyOverflow Sep 17 '17 at 19:45
  • Well, show us good repro code. What is there now is not nearly good enough to give us any guess why it doesn't work. There is no fundamental reason why it shouldn't work. – Hans Passant Sep 17 '17 at 19:48

1 Answers1

0

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.

FaultyOverflow
  • 153
  • 1
  • 9