Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello Again Code project
This time the question is really confusing,
I hope everybody knows how do you create a Form using VS.
But my question is slightly different, I don't want to create a Win Forms application by not using the default template but I will use only a class file to create it.
Why do I need this?
I know many of you would ask this, Actually I am creating a custom C# compiler, using C# code provider, and compiler (experienced users would know this) It reads the code from a text box and then compiles it to an assembly, but the problem is, it only runs console applications. Or it just runs classes. That's the reason I wanna do this.

PS : I want such a code in which the user would also be able to add controls to the form. It supports pure C# so I think it's possible....
Posted
Updated 10-Jul-13 3:23am
v2
Comments
Joezer BH 10-Jul-13 9:24am    
I almost understand what you are asking, can you explain a little bit more?
[no name] 10-Jul-13 9:25am    
"This time the question is really confusing", not really..... you did not ask any kind of a question at all.

A windows form is just a class that is derived from System.Windows.Forms.Form[^] class. So yes indeed that is all you'll have to do. If you've ever had a look at a Windows Forms project you could have "discovered" that all on your own.

You could even have a "simple" application that just opens a System.Windows.Forms.Form like in the code below. Not that this form would do anything sensible, but you could write a program that would then modify the form like adding controls to it and hooking up event handlers etc.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace JustAForm
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form()); // This is just to illustrate that it is possible to open up an empty Form.
        }
    }
}



Regards,
— Manfred
 
Share this answer
 
v2
Comments
sid2x 11-Jul-13 5:22am    
How to do I add controls to this form????
Manfred Rudolf Bihy 11-Jul-13 5:33am    
Every form has the Controls property. First you create a control by calling its constructor, set all the relevant properties and then add it to the forms Controls collection using the Add method of said collection. There are many questions and solutions on how to do this both on CP and also SO. Just do some research by googling: add controls dynamically to windows form..

Cheers!
sid2x 11-Jul-13 6:05am    
Remember that my case is different, I am not programming in C#, but on a custom compiler, So commands like this.SuspendLayout,this.Controls.Add are not working which are required to add controls dynamically to windows form
Manfred Rudolf Bihy 11-Jul-13 7:28am    
Why wouldn't they work? You don't have to use "this", just use a valid reference to a form you instantiated.
BTW if you're ready to build your own compiler I'm not sure why exactly you are having difficulties with doing this.
Maybe you should explain which part is giving you trouble.

Cheers!
sid2x 11-Jul-13 10:49am    
The only thing I need is the name of the component from which I use to add controls and stuff. Is it Form or Form1 or anything else. I mean the name of the form object. Thanks in advance.....
Hi,

If I understand you, you want to create a Windows Form without the Visual Studio Designer. You can:
C#
public class Form1 : Form
{
    Button button1; // add more controls if you want
    
    void InitializeComponent()
    {
         button1 = new Button();

         this.SuspendLayout();
         button1.Name = "button1";
         button1.Text = "Button 1";
         button1.Size = new Size(250, 150);
         button.Location = new Point(100, 100);
         button1.Click += button1_Click; 
         // add more initialization code if you want
        
         // initialize your other controls

         this.Controls.Add(button1);
         // add your other controls to the form

         this.Name = "Form1";
         this.Text = "Form1";
         this.ResumeLayout(false);
         this.PerformLayout();
    }
    public Form1()
    {
         InitializeComponent();
    }
    void button1_Click(object sender, EventArgs e)
    {
          // your button click code
    }
}

Quote:
Actually I am creating a custom C# compiler

Important additional information: if you want to compile a Windows Forms application, don't forget to add a reference to "System.Drawing.dll" and to "System.Windows.Forms.dll"

Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900