Codedom: Adding a Button (or any control for that matter) to a Win Form.

  • Thread starter Thread starter Microsoft Newsgroups
  • Start date Start date
M

Microsoft Newsgroups

Hi All,

I'm looking for some simple examples of using the CodeDom. While the ones
that I have found have been very helpful, I can't seem to find the
following:

How to add a control to a form.
How to add an event handle to that control just was just added to the form.

If anyone has any examples that they could share, it would be much
appreciated.

Thanks in advance for your time,
Gary Stewart
 
Adding a control to a form using System.CodeDom? I think you'll have to be
more specific about what you think you're trying to do.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
Hi Kevin...

Using the CodeDom I create a NameSpace, a Form, a constructor for that Form
and the InitializeComponent method for that form.

Here's a snip of the code that does that:
_Form = new CodeTypeDeclaration(this._Name);

_Form.BaseTypes.Add("System.Windows.Forms.Form");

_NameSpace.Types.Add(_Form);

// Create a constructor for this form.

CodeConstructor oConstructor = new CodeConstructor();

oConstructor.Attributes = MemberAttributes.Public;

oConstructor.Statements.Add(new CodeMethodInvokeExpression(new
CodeThisReferenceExpression(), "InitializeComponent", new
CodeExpression()));


_Form.Members.Add(oConstructor);

CodeMemberMethod oInitControls = new CodeMemberMethod();

oInitControls.Name = "InitializeComponent";

oInitControls.Attributes = MemberAttributes.Private;

oInitControls.CustomAttributes.Add(new
CodeAttributeDeclaration("System.Diagnostics.DebuggerStepThrough"));

/////////////////////////// End Code

That gives me a nice blank Form with a constructor and InitializeComponent
method. How do I add a button to that Form using the CodeDom and its
associated namespaces?


Thanks,

Gary
 
Back
Top