Building C# GUI without using Visual Studio GUI designer (Toolbox)
Solution 1
Regarding C# :
In order for you to run a C#
application from a cmd
, following steps are needed :
- 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. source and build. - Now from any
Text Editor
create a small sample program (I am using Notepad++), as below, save it asWinFormExample.cs
undersource
folder :
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 ENTER to compile as shown below : - Now simply run it using
.\build\WinExample
, as shown below : - Now your simple
GUI Application
is up and running :-)
Do let me know, I can explain the same thingy regarding Java
as well, if need be :-)
More info regarding Compiler Options
can be found on C# Compiler Options Listed Alphabetically
Solution 2
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.
Here is a nice article comparing Java Swing and WinForms:
Related videos on Youtube
Comments
-
Mahshid Zeinaly about 2 years
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.
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.
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?
Please compare Java Swing with C# in this.
-
nIcE cOw almost 11 years@mahshidzeinaly : Though you can use the above code in your Visual Studio projects too, simply go to
Form1.Designer.cs
file and add all using statements on top, the three as presented in the example code by me, simply calldisplayGUI()
from withinInitializeComponent()
method