I am trying to create a general function in VB.NET that can dynamically create a form object (label , button etc.) and this object can be pin the form or container . My problem is i am unable to pass the control object type to the function when calling it. Visual studio gives error.
Private Function AddnewObject(newObject As Integer, ContainerObject As Object, ByRef Objectn As Control) As System.Windows.Forms.Control`
ContainerObject.Controls.Add(Objectn)
Objectn.Top = 560 + (newObject * 27)
Objectn.Left = 100
Objectn.Text = Objectn.ToString & newObject.ToString
newObject += 1 ' useless in this context
Return Objectn
End Function
when trying to call it like this :
AddnewObject(concounter, TableLayoutPanel1, Label)
i get error
BC30109 'Label' is a class type and cannot be used as an expression.
I tried to change the type of objectn but can't figure the correct one/way to use
Typeto indicate what control type to instantiate. Then, instead ofLabel, you would useGetType(Label)and pass the instance to receive the new control (if needed) in theByRefargument.