C# 如何以编程方式创建 Windows 窗体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11854971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:31:18  来源:igfitidea点击:

How do I programmatically create a windows form?

c#.netcsharpcodeprovider

提问by Rafik Bari

I have a unique c# source file named source.csthat i compile using CSharpCodeProvider from a builder to get an executable.

我有一个独特的 c# 源文件source.cs,我使用构建器中的 CSharpCodeProvider 编译该文件以获取可执行文件。

I would put an option on the builder whether to display the About form on application startup or not.

我会在构建器上放置一个选项,是否在应用程序启动时显示“关于”表单。

How can i create a form with title as About Us then add controls within (Labels, RichTextEdit etc..)

我如何创建一个标题为关于我们的表单,然后在(标签、RichTextEdit 等)中添加控件

Something like

就像是

if (display_about_dialog) {
// code to display the form }

Any help would be highly appreciated

任何帮助将不胜感激

采纳答案by Dan

Try something like this:

尝试这样的事情:

using (Form form = new Form())
{
    form.Text = "About Us";

    // form.Controls.Add(...);

    form.ShowDialog();
}

Here's the documentation page for the System.Windows.Forms.Formclass.

这是该System.Windows.Forms.Form课程的文档页面。

回答by Sam Axe

Form aForm = new Form();

aForm.Text = @"About Us";
aForm.Controls.Add(new Label() {Text = "Version 5.0"});
aForm.ShowDialog();  // Or just use Show(); if you don't want it to be modal.

回答by Prasanth

Formis a class which you can instantiate like any other, set it's properties, call it's methods.

Form是一个类,您可以像任何其他类一样实例化它,设置它的属性,调用它的方法。

回答by Hans Z

if you have a class MyForm : System.Windows.Forms.Form(that you create using windows form builder)

如果您有一个类MyForm : System.Windows.Forms.Form(您使用 Windows 窗体构建器创建)

You can do

你可以做

MyForm form = new MyForm();
form.Show();

To launch an instance of MyForm.

启动 MyForm 的实例。



Though if you want to create a simple confirmation or message dialog, check out the many uses of MessageBox

但是,如果您想创建一个简单的确认或消息对话框,请查看 MessageBox

MessageBox.Show("text");
MessageBox.Show("text", "title", MessageBoxButtons.OKCancel);