Bigbeanpole
Member
- Joined
- Jul 7, 2005
- Messages
- 15
- Programming Experience
- 1-3
Awhile ago, I was looking for how to create dynamic forms based on a string. In that search, I came across this:
[/color][/size]
Now, according to the above, should I not also be able to create an object reference to a combo box, or any other object I so desire, by passing it a string of FormName.ControlName ? I've tried several different methods and can't quite find the right one. Does anybody have any suggestions?
I am wanting to have a dynamic reference to a combobox based on a string that will definately exist (I'm passing it specific values, so I know the control is there...)
In the above, I am able to create forms by calling it thus:
with formName being a valid string value.
Can I not do the same thing changing Form to ControlOfChoice (ComboBox currently) ?
(alot of the above code is for testing purposes...all the messageboxes and what-not)[/size]
VB.NET:
[size=2]
[/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Shared[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] CreateObjectInstance([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] objectName [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object
[/color][/size][size=2][color=#008000]' Creates and returns an instance of any object in the assembly by its type name.
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] obj [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] testing [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "Form1.ComboBox1"
[/size][size=2][color=#0000ff]Try
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] objectName.LastIndexOf(".") = -1 [/size][size=2][color=#0000ff]Then
[/color][/size][size=2][color=#008000]'Appends the root namespace if not specified.
[/color][/size][size=2]objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
[/color][/size][size=2]obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)
MessageBox.Show("[assembly].getentryassembly " & [Assembly].GetEntryAssembly.ToString & ControlChars.NewLine & _
"[assembly].getentryassembly.GetName: " & [Assembly].GetEntryAssembly.GetName.ToString & ControlChars.NewLine & _
"[assembly].getentryassembly.GetName.Name: " & [Assembly].GetEntryAssembly.GetName.Name.ToString & ControlChars.NewLine & _
"[assembly].getentryassembly.GetName.name.cbolookupbyasset " & [Assembly].GetEntryAssembly.GetName.Name & "." & testing.ToString)
[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception
obj = [/size][size=2][color=#0000ff]Nothing
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Try
[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] obj
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Function[/color][/size]
[size=2][color=#0000ff]
Now, according to the above, should I not also be able to create an object reference to a combo box, or any other object I so desire, by passing it a string of FormName.ControlName ? I've tried several different methods and can't quite find the right one. Does anybody have any suggestions?
I am wanting to have a dynamic reference to a combobox based on a string that will definately exist (I'm passing it specific values, so I know the control is there...)
In the above, I am able to create forms by calling it thus:
VB.NET:
[/color][/color][/size]
[size=2][color=#0000ff][size=2][color=#0000ff]DirectCast[/color][/size][size=2][color=#000000](CreateObjectInstance(formName), Form)[/color][/size]
[size=2][color=#000000]
with formName being a valid string value.
Can I not do the same thing changing Form to ControlOfChoice (ComboBox currently) ?
(alot of the above code is for testing purposes...all the messageboxes and what-not)[/size]