eclipse 不使用 Visual Studio GUI 设计器(工具箱)构建 C# GUI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18294608/
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-09-19 21:05:31  来源:igfitidea点击:

Building C# GUI without using Visual Studio GUI designer (Toolbox)

c#eclipseswingvisual-studionetbeans

提问by Mahshid Zeinaly

In Java Swing, we can create GUI with only coding Java (for example in Eclipse). Using NetBeans's toolbox to drag and drop components to the UI is optional.

在 Java Swing 中,我们只需编写 Java 代码即可创建 GUI(例如在 Eclipse 中)。使用 NetBeans 的工具箱将组件拖放到 UI 是可选的。

I am wondering if there is the same concept in C#. Can I put my components into my GUI and add them behaviors with only coding? That way I would feel that I have more control over my application.

我想知道 C# 中是否有相同的概念。我可以将我的组件放入我的 GUI 并仅通过编码添加它们的行为吗?这样我就会觉得我对我的申请有更多的控制权。

Example: I do not want to g to toolbox to add "mousehover" to my button! Instead, I want to write the code myself. I know where I can find the code, but is it the only place that I should write that line of code?

示例:我不想 g 到工具箱将“鼠标悬停”添加到我的按钮!相反,我想自己编写代码。我知道在哪里可以找到代码,但它是我应该编写那行代码的唯一地方吗?

Please compare Java Swing with C# in this.

请在此比较 Java Swing 和 C#。

回答by nIcE cOw

Regarding C# :

关于 C#:

In order for you to run a C#application from a cmd, following steps are needed :

为了让您从 运行C#应用程序cmd,需要执行以下步骤:

  1. Go to C:\Windows\Microsoft.NET\Framework\v4.0.30319location on your File System and copy the path.
  2. Now right click Computergo to Properties.
  3. Under System Properties, select AdvancedTab, and click on Environment Variables.
  4. On Environment Variablesunder User Variables, select New.
  5. For Variable Namewrite CSHARP_HOMEor something else, though I am using the same for further needs to explain this out. For Variable Valuesimply Pastewhat you copied in Step 1. Click OK.
  6. Again perform Step 4, if pathvariable does not exist, else you can simply select pathand then click Editto perform this next thingy (after putting ;(semi-colon)at the end of the Variable Valueand write %CSHARP_HOME%\(or use what you used in Step 5) ). This time for Variable Namewrite path, and for Variable Valueuse %CSHARP_HOME%\and click OK.
  7. Open cmdand type cscand press ENTER, you might be able to see something like this as an output CMD OUTPUT 1
  8. Now consider I am creating a directory structure for my CSharp Project like this (on File System) at this location C:\Mine\csharp\command. Here I created two folders inside commandfolder. sourceand build.
  9. Now from any Text Editorcreate a small sample program (I am using Notepad++), as below, save it as WinFormExample.csunder sourcefolder :
  1. 转到C:\Windows\Microsoft.NET\Framework\v4.0.30319文件系统上的位置并复制路径。
  2. 现在右键单击Computer转到属性。
  3. 在 下System Properties,选择AdvancedTab,然后单击Environment Variables
  4. Environment VariablesUser Variables,选择New
  5. 对于Variable NameCSHARP_HOME或其他东西,尽管我使用相同的内容来进一步解释这一点。对于Variable Value只是Paste你在复制的第1步。单击确定。
  6. 再次执行第 4 步,如果path变量不存在,否则您可以简单地选择path然后单击Edit以执行下一个操作(将;(分号)放在最后Variable Value并写入%CSHARP_HOME%\(或使用您在第 5 步中使用的内容) )。这次是Variable NamepathVariable Value使用%CSHARP_HOME%\,然后点击确定。
  7. 打开cmd并输入csc并按下ENTER,您可能会看到类似这样的输出 CMD 输出 1
  8. 现在考虑我在这个位置为我的 CSharp 项目创建一个目录结构(在文件系统上)C:\Mine\csharp\command。在这里,我在文件command夹中创建了两个文件夹。构建
  9. 现在从任何Text Editor创建一个小示例程序(我正在使用 Notepad++),如下所示,将其保存WinFormExample.cssource文件夹下:


using System;
using System.Drawing;
using System.Windows.Forms;

namespace CSharpGUI {
    public class WinFormExample : Form {

        private Button button;

        public WinFormExample() {
            DisplayGUI();
        }

        private void DisplayGUI() {
            this.Name = "WinForm Example";
            this.Text = "WinForm Example";
            this.Size = new Size(150, 150);
            this.StartPosition = FormStartPosition.CenterScreen;

            button = new Button();
            button.Name = "button";
            button.Text = "Click Me!";
            button.Size = new Size(this.Width - 50, this.Height - 100);
            button.Location = new Point(
                (this.Width - button.Width) / 3 ,
                (this.Height - button.Height) / 3);
            button.Click += new System.EventHandler(this.MyButtonClick);

            this.Controls.Add(button);
        }

        private void MyButtonClick(object source, EventArgs e) {
            MessageBox.Show("My First WinForm Application");
        }

        public static void Main(String[] args) {
            Application.Run(new WinFormExample());
        }
    }
}


  1. Now type csc /out:build\WinFormExample.exe source\WinFormExample.cs(more info is given at the end, for compiler options)and press ENTERto compile as shown below : CMD OUTPUT 2
  2. Now simply run it using .\build\WinExample, as shown below : CMD OUTPUT 3
  3. Now your simple GUI Applicationis up and running :-)
  1. 现在输入csc /out:build\WinFormExample.exe source\WinFormExample.cs(更多信息在最后给出,用于编译器选项)并按下ENTER编译,如下所示: CMD 输出 2
  2. 现在只需使用 运行它.\build\WinExample,如下所示: CMD 输出 3
  3. 现在您的简单GUI Application已启动并运行:-)

Do let me know, I can explain the same thingy regarding Javaas well, if need be :-)

请让我知道Java,如果需要,我也可以解释同样的事情:-)

More info regarding Compiler Optionscan be found on C# Compiler Options Listed Alphabetically

有关更多信息,请Compiler Options参阅按字母顺序列出的 C# 编译器选项

回答by What Would Be Cool

There is nothing magic in WinForms drag & drop. You can reference the same classes from System.Windows.Forms. If you are going to roll your own, I would suggest looking at the Model View Presenter pattern.

WinForms 拖放没有什么神奇之处。您可以从 System.Windows.Forms 引用相同的类。如果您打算推出自己的产品,我建议您查看 Model View Presenter pattern

Here is a nice articlecomparing Java Swing and WinForms:

这是一篇比较 Java Swing 和 WinForms的好文章