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

12,976

Solution 1

Regarding C# :

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

  1. Go to C:\Windows\Microsoft.NET\Framework\v4.0.30319 location on your File System and copy the path.
  2. Now right click Computer go to Properties.
  3. Under System Properties, select Advanced Tab, and click on Environment Variables.
  4. On Environment Variables under User Variables, select New.
  5. For Variable Name write CSHARP_HOME or something else, though I am using the same for further needs to explain this out. For Variable Value simply Paste what you copied in Step 1. Click OK.
  6. Again perform Step 4, if path variable does not exist, else you can simply select path and then click Edit to perform this next thingy (after putting ;(semi-colon) at the end of the Variable Value and write %CSHARP_HOME%\(or use what you used in Step 5) ). This time for Variable Name write path, and for Variable Value use %CSHARP_HOME%\ and click OK.
  7. Open cmd and type csc and 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 command folder. source and build.
  9. Now from any Text Editor create a small sample program (I am using Notepad++), as below, save it as WinFormExample.cs under source 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());
        }
    }
}

  1. 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 : CMD OUTPUT 2
  2. Now simply run it using .\build\WinExample, as shown below : CMD OUTPUT 3
  3. 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:

Share:
12,976

Related videos on Youtube

Mahshid Zeinaly
Author by

Mahshid Zeinaly

my Company my Linkedin

Updated on June 04, 2022

Comments

  • Mahshid Zeinaly
    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
    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 call displayGUI() from within InitializeComponent() method

Related