1

Hi Can I create a Control from String Value Like

from "System.Windows.Forms.TextBox" create control

2
  • Your question is not clear to answer. Commented Apr 26, 2011 at 8:59
  • Makes perfect sense to me Commented Sep 9, 2014 at 17:13

4 Answers 4

5

You could use reflection:

var textBoxType = typeof(Control).Assembly.GetType("System.Windows.Forms.TextBox", true);
var textBox = Activator.CreateInstance(textBoxType);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.Thanks.Thanks.Its true,working wonderfull.i have one question too.I define textbox but how can i change textbox text or size from string like "Text"
@deneme, you could once again use reflection on the newly created instance: textBoxType.GetProperty("Text").SetValue(textBox, "Hello World", null);. I would recommend you reading more about reflection: msdn.microsoft.com/en-us/library/f7ykdhsy.aspx
1

Do like this :

var controlType = typeof(Control);
        var type = controlType
            .Assembly
            .GetTypes()
            .Where(t => controlType.IsAssignableFrom(t) && 
                        t.Namespace == "System.Windows.Forms"
                        t.Name == "ControlName"
            ).FirstOrDefault();

var inst = Activator.CreateInstance(type );

This answer is because of your previous question.

Comments

0
var assembly = Assembly.GetExecutingAssembly();
var type = assembly.GetType("System.Windows.Forms.TextBox");
var inst = Activator.CreateInstance(type);

Can't test it yet. But it should do the trick.

Comments

0
 Type t = Type.GetType( yourTypeStingHere);

 ConstructorInfo info = t.GetConstructor( new Type[] { } );
 object instance = info.Invoke(new object[]{} )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.