473,472 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Form.Controls.Add using reflection

Hi All,

I'm learning how to write C# using reflection, but don't know how to
code using reflection
this.Controls.Add(this.label1);

Could anyone help, Thank in advance.

xxx=============================================== =======================

using System;
using System.Reflection;
using System.Reflection.Emit; // To emit MSIL
using System.Threading; // To get Current AppDomain
using System.IO;

public class HelloWorld
{

static void Main()
{
// public class Form1:System.Windows.Forms.Form{Form1(){}}
// Form1 s= new Form1();

Assembly WinFormasm = Assembly.Load("System.Windows.Forms,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

Type WinForm = WinFormasm.GetType("System.Windows.Forms.Form");

// Create a simple name for the assembly.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "Form1Assembly";

// Create the dynamic assembly.
AssemblyBuilder assembly =
Thread.GetDomain().DefineDynamicAssembly(assemblyN ame,
AssemblyBuilderAccess.Run);

// Create a dynamic module.
ModuleBuilder module = assembly.DefineDynamicModule("Form1Module");

// Define a public class named "Form1Class" in the assembly.
TypeBuilder Form1Class = module.DefineType("Form1",
TypeAttributes.Public, WinForm);

// Create the constructor.
Type[] constructorArgs = null;
ConstructorBuilder constructor =
Form1Class.DefineConstructor(MethodAttributes.Publ ic,
CallingConventions.Standard, constructorArgs);

// Generate IL for the method. The constructor calls its superclass
// constructor. The constructor stores its argument in the private field.
ILGenerator constructorIL = constructor.GetILGenerator();

constructorIL.Emit(OpCodes.Ldarg_0);
ConstructorInfo superConstructor = WinForm.GetConstructor(new Type[0]);
constructorIL.Emit(OpCodes.Call, superConstructor);

constructorIL.Emit(OpCodes.Ret);

// the class CodeGenClass.
Type Form1 = Form1Class.CreateType();
object s= Activator.CreateInstance (Form1);

object label1= Activator.CreateInstance
(WinFormasm.GetType("System.Windows.Forms.Label")) ;
object textBox1= Activator.CreateInstance
(WinFormasm.GetType("System.Windows.Forms.TextBox" ));
object button1= Activator.CreateInstance
(WinFormasm.GetType("System.Windows.Forms.Button") );

/* please show how to implement these 3 lines using reflection.
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
*/

Form1.GetProperty("Text", typeof(string)).SetValue(s, "Hello
World",null);
Console.WriteLine("value {0}",Form1.GetProperty("Text",
typeof(string)).GetValue(s, null));

// Application.Run(New Form1);

Type WinApplication =
WinFormasm.GetType("System.Windows.Forms.Applicati on");
MethodInfo method = WinApplication.GetMethod("Run", new
Type[]{WinForm});
object result = method.Invoke(null, new object[]{s});
}
}

Nov 15 '05 #1
0 8411

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: MP | last post by:
Hello I want to have a public property (and Brows able) in one control, I use this code: public System.Windows.Forms.Form recordForm get { return _recordForm;} set {_recordForm = value;}
3
by: MAY | last post by:
Hi, I have a problem about serialize the form controls. I wrote a test program to test serialize a from but fail (->An unhandled exception of type...
1
by: Jon Turner | last post by:
My intention is to be able to open an assembly and for each form, list the form's children's controls. Can someone please tell me how this can be accomplished ? I am familiar with how to get...
21
by: Simon Verona | last post by:
Hope somebody can help! I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form. Specifically, I'm trying...
4
by: Nikolay Petrov | last post by:
Hi everyone! I am developing app, which should be used by users, which are members of different groups. These users and groups are stored in my SQL database and are custom for my application,...
4
by: Toze | last post by:
I'm using a assembly to load my apllication (ex: Mobi.exe), and now I need to list all forms in my apllication and list all controls (ex: txtname;btnname) inside of each form.
9
by: timnels | last post by:
I have an issue where I have a user control that is launched into a floating form. At some point later, I allow the user to "unfloat" the user control by reparenting it on a split container in...
6
by: Ron | last post by:
I am building a utility that needs to extract all Windows forms control names from a compiled assembly for documentation purposes. I am currently using reflection, but not all of the controls are...
6
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can a Form be set to Serializable? My App keeps crashing ever time I attempt to serialize the form. I was under the impression that Serialization could be used to easily save all of my data....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.