I have a project with about 100+ custom user controls in VB.NET, using the newest .NET Core 6 framework. These controls are added to different Forms when the Form load.
I am trying to create an instance of a specific control by using a string (which stores the control name), and then access its properties. For example, if I have a control named Control1, the string would store "Control1".
I have tried using Reflection (see below, but I am getting returned "Nothing"). What am I doing wrong?
Dim sControlType as String 'example = "Control1", or "Control2", etc
Dim objControl as Object = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(sControlType) 'objControl returns Nothing??
Second, after creating an instance of the object, I wish to convert the "Object" to the actual Type. Is this how it is done?
objControl = Convert.ChangeType(objControl , Type.GetType(sControlType))
Lastly, after converting the "Object" to the actual Type, I then want to access methods and properties from this Type. Is the following how it is done?
Dim objControl_Type As Type = objControl.GetType
If objControl_Type.GetField("Dock") IsNot Nothing Then objControl.Dock = DockStyle.Fill
Thank you very much.
Aucun commentaire:
Enregistrer un commentaire