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
Building C# GUI without using Visual Studio GUI designer (Toolbox)
提问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
,需要执行以下步骤:
- Go to
C:\Windows\Microsoft.NET\Framework\v4.0.30319
location on your File System and copy the path. - Now right click
Computer
go to Properties. - Under
System Properties
, selectAdvanced
Tab, and click onEnvironment Variables
. - On
Environment Variables
underUser Variables
, selectNew
. - For
Variable Name
writeCSHARP_HOME
or something else, though I am using the same for further needs to explain this out. ForVariable Value
simplyPaste
what you copied in Step 1. Click OK. - Again perform Step 4, if
path
variable does not exist, else you can simply selectpath
and then clickEdit
to perform this next thingy (after putting ;(semi-colon)at the end of theVariable Value
and write%CSHARP_HOME%\
(or use what you used in Step 5) ). This time forVariable Name
writepath
, and forVariable Value
use%CSHARP_HOME%\
and click OK. - Open
cmd
and typecsc
and press ENTER, you might be able to see something like this as an output - 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 insidecommand
folder. sourceand build. - Now from any
Text Editor
create a small sample program (I am using Notepad++), as below, save it asWinFormExample.cs
undersource
folder :
- 转到
C:\Windows\Microsoft.NET\Framework\v4.0.30319
文件系统上的位置并复制路径。 - 现在右键单击
Computer
转到属性。 - 在 下
System Properties
,选择Advanced
Tab,然后单击Environment Variables
。 - 在
Environment Variables
下User Variables
,选择New
。 - 对于
Variable Name
写CSHARP_HOME
或其他东西,尽管我使用相同的内容来进一步解释这一点。对于Variable Value
只是Paste
你在复制的第1步。单击确定。 - 再次执行第 4 步,如果
path
变量不存在,否则您可以简单地选择path
然后单击Edit
以执行下一个操作(将;(分号)放在最后Variable Value
并写入%CSHARP_HOME%\
(或使用您在第 5 步中使用的内容) )。这次是Variable Name
写path
,Variable Value
使用%CSHARP_HOME%\
,然后点击确定。 - 打开
cmd
并输入csc
并按下ENTER,您可能会看到类似这样的输出 - 现在考虑我在这个位置为我的 CSharp 项目创建一个目录结构(在文件系统上)
C:\Mine\csharp\command
。在这里,我在文件command
夹中创建了两个文件夹。源和构建。 - 现在从任何
Text Editor
创建一个小示例程序(我正在使用 Notepad++),如下所示,将其保存WinFormExample.cs
在source
文件夹下:
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());
}
}
}
- 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 : - Now simply run it using
.\build\WinExample
, as shown below : - Now your simple
GUI Application
is up and running :-)
- 现在输入
csc /out:build\WinFormExample.exe source\WinFormExample.cs
(更多信息在最后给出,用于编译器选项)并按下ENTER编译,如下所示: - 现在只需使用 运行它
.\build\WinExample
,如下所示: - 现在您的简单
GUI Application
已启动并运行:-)
Do let me know, I can explain the same thingy regarding Java
as well, if need be :-)
请让我知道Java
,如果需要,我也可以解释同样的事情:-)
More info regarding Compiler Options
can 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: