Adding Control By TypeName

  • Thread starter Thread starter Ahmed Hashish
  • Start date Start date
A

Ahmed Hashish

Dear Sir
I'm trying to create a new control and add it to a form at runtime.
The function should be like this:

private void AddControl(Form f, string ControlType, string ConrtolName)

and I'll call it like this:
AddControl(this,"Button","Button1")

regards
Ahmed
 
Hi Ahmed,

So, what is a problem to use something like just this:
public void AddControl(Form form, string type, string name)
{
switch(type) { case "Button": Button btn = new Button(); btn.Name = name;
..... }
form.Controls.Add(btn);
... and so on
}

Sure it's more reliable then:

Control dynamicControl = (Control)
(Activator.CreateInstance(Assembly.GetAssembly(typeof(Control)).FullName,
string.Format("System.Windows.Forms.{0}", type)).Unwrap());

Regards, Alex
[TechBlog] http://devkids.blogspot.com



AH> Dear Sir
AH> I'm trying to create a new control and add it to a form at
AH> runtime.
AH> The function should be like this:
AH> private void AddControl(Form f, string ControlType, string
AH> ConrtolName)
AH>
AH> and I'll call it like this:
AH> AddControl(this,"Button","Button1")
AH> regards
AH> Ahme
 
Hello Ahmed,

Instead of taking "string ControlType" define its type as a control.
Some thing like this:

VB CODE:
Private Sub addcontrol(ByVal f As Form, ByVal ControlType As Control,
ByVal cname As String)
c.Name = cname
f.Controls.Add(c)
End Sub

And when calling it you can call it as following
VB CODE:
addcontrol(Me, New Button, "mybtn")

I hope this helps!
-Rajneesh
 
Thanks Alex


Hi Ahmed,

So, what is a problem to use something like just this:
public void AddControl(Form form, string type, string name)
{
switch(type) { case "Button": Button btn = new Button(); btn.Name = name;
.... }
form.Controls.Add(btn);
... and so on
}

Sure it's more reliable then:

Control dynamicControl = (Control)
(Activator.CreateInstance(Assembly.GetAssembly(typeof(Control)).FullName,
string.Format("System.Windows.Forms.{0}", type)).Unwrap());

Regards, Alex
[TechBlog] http://devkids.blogspot.com



AH> Dear Sir
AH> I'm trying to create a new control and add it to a form at
AH> runtime.
AH> The function should be like this:
AH> private void AddControl(Form f, string ControlType, string
AH> ConrtolName)
AH>
AH> and I'll call it like this:
AH> AddControl(this,"Button","Button1")
AH> regards
AH> Ahmed
 
Thanks
I need to ask another question.
I can now create the controls, How I could set the control properties? I have all control properties in a string format I'm trying to do it like this:
Private Sub SetProperty(ByVal C As Control, ByVal PropertyName As String, ByVal PropertyValue As String)

Dim Props As PropertyDescriptorCollection = TypeDescriptor.GetProperties(C)

Dim myProperty As PropertyDescriptor = Props.Find(PropertyName, True)

If Not myProperty Is Nothing Then myProperty.SetValue(C, PropertyValue) 'I got error "Object of type 'System.String' cannot be converted to type 'System.Windows.Forms.AnchorStyles'

End Sub



The PropertyName is the output of the method "myProperty.GetValue(C).ToString"



Regards,

Ahmed Hashish

Herfried K. Wagner said:
I'm trying to create a new control and add it to a form at runtime.
The function should be like this:

private void AddControl(Form f, string ControlType, string ConrtolName)

Take a look at this code snippet:

<URL:http://dotnet.mvps.org/dotnet/code/techniques/#ClassByName>
 
I found it
I used the typeconverter class to convert the property value to string, and used it again to restore the value.
I used the following code to get it:
Dim tc As TypeConverter = TypeDescriptor.GetConverter(myProperty.PropertyType)

If tc.CanConvertFrom(GetType(String)) Then

Try

Dim val As Object = tc.ConvertFrom(PropertyValue)

myProperty.SetValue(C, val)

Catch ex As Exception

Debug.Print("Setting " + PropStr + " failed:" + ex.Message)

End Try



regards,

Ahmed Hashish

Thanks
I need to ask another question.
I can now create the controls, How I could set the control properties? I have all control properties in a string format I'm trying to do it like this:
Private Sub SetProperty(ByVal C As Control, ByVal PropertyName As String, ByVal PropertyValue As String)

Dim Props As PropertyDescriptorCollection = TypeDescriptor.GetProperties(C)

Dim myProperty As PropertyDescriptor = Props.Find(PropertyName, True)

If Not myProperty Is Nothing Then myProperty.SetValue(C, PropertyValue) 'I got error "Object of type 'System.String' cannot be converted to type 'System.Windows.Forms.AnchorStyles'

End Sub



The PropertyName is the output of the method "myProperty.GetValue(C).ToString"



Regards,

Ahmed Hashish

Herfried K. Wagner said:
I'm trying to create a new control and add it to a form at runtime.
The function should be like this:

private void AddControl(Form f, string ControlType, string ConrtolName)

Take a look at this code snippet:

<URL:http://dotnet.mvps.org/dotnet/code/techniques/#ClassByName>
 
Back
Top