如何只使用一个简单的类在C#中创建一个Form [英] How to Use only a simple class to create a Form in C#
问题描述
你好再次代码项目
这次问题真是令人困惑,
我希望大家都知道如何使用VS创建表单。
但我的问题略有不同,我不想通过不使用默认模板来创建Win Forms应用程序,但我只使用类文件来创建它。
我为什么需要这个?
我知道很多人会问这个问题,实际上我正在创建一个自定义C#编译器,使用C#代码提供程序和编译器(有经验的用户会知道这个)它从文本框中读取代码然后将其编译为程序集,但问题是,它只运行控制台应用程序。或者它只是运行类。这就是我想这样做的原因。
PS:我想要一个代码,用户也可以在这个代码中添加控件。它支持纯C#所以我认为它是可能的....
一个窗口表单只是一个派生自System.Windows.Forms.Form [ ^ ]类。所以,的确,这就是你所要做的一切。如果你曾经看过一个Windows Forms项目,你可以自己发现这一切。
你甚至可以拥有一个简单的应用程序这只是打开一个System.Windows.Forms.Form,如下面的代码所示。并不是说这个形式会做任何明智的事情,但是你可以编写一个程序,然后修改表单就像添加控件并挂钩事件处理程序等。
< pre lang =cs> 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Windows.Forms;
命名空间 JustAForm
{
static class 程序
{
/// < 摘要 >
/// 应用程序的主要入口点。
/// < / summary >
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new Form()); // 这只是为了说明可以打开一个空表格。
}
}
}
问候,
- Manfred
如果我了解你,你想创建一个没有Visual Studio Designer的Windows窗体。你可以:
public class Form1 :表格
{
按钮按钮1; // 如果需要,可添加更多控件
void InitializeComponent()
{
button1 = new Button();
this .SuspendLayout();
button1.Name = button1;
button1.Text = 按钮1;
button1.Size = new 大小( 250 , 150 跨度>);
button.Location = new 点( 100 , 100 跨度>);
button1.Click + = button1_Click;
// 如果需要,可添加更多初始化代码
< span class =code-comment> // 初始化您的其他控件
此跨度> .Controls.Add(按钮1);
// 将您的其他控件添加到表单
< span class =code-keyword> this .Name = Form1;
this .Text = Form1< /跨度>;
this .ResumeLayout( false );
this .PerformLayout();
}
public Form1()
{
InitializeComponent();
}
void button1_Click( object sender,EventArgs e)
{
// 您的按钮点击代码
}
}
Quote:其实我正在创建一个自定义C#编译器
重要的附加信息:如果要编译Windows窗体应用程序,请不要忘记添加对System.Drawing.dll和System.Windows.Forms.dll的引用
希望这会有所帮助。
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....
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.
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
Hi,
If I understand you, you want to create a Windows Form without the Visual Studio Designer. You can:
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.
这篇关于如何只使用一个简单的类在C#中创建一个Form的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!