Adding Control By TypeName

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
 
A

Alex Meleta

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
 
G

Guest

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
 
A

Ahmed Hashish

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
 
A

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>
 
A

Ahmed Hashish

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top