Smallest form app?

ddanbe 1 Tallied Votes 166 Views Share

This has by far no practical use. But as I was used to VS, handling most of the code housekeeping "behind the back", I wanted to try to make a Forms program as minimalistic as possible.
Start an Empty project, include a reference to System.Windows.Forms and fill in the code.
Can it be made smaller?

using System.Windows.Forms;

class Class1
{
    static void Main()
    {
        Label label = new Label();
        label.Text = "Hello world!";
        Form form = new Form();
        form.Controls.Add(label);
        Application.Run(form);
    }
}
Ketsuekiame 860 Master Poster Featured Poster

The way that VS currently handles it is pretty neat and tidy, with all the control setup happening in the .Designer partial class.

I wouldn't recommend changing it to be honest as you'll only end up reproducing the same functionality as you increase the amount of controls on the form (to create any practical application).

Whilst VS tells you not to modify the designer file, if you don't intend to use the UI designer then have at it. It's nice to be able to tweak various settings that aren't in the UI designer.

ddanbe 2,724 Professional Procrastinator Featured Poster

Thanks for the comment.:)
I would not even think of changing the way VS handles things, it does indeed a neat job. I was more thinking of the way how you can handle a small test code, like you can do in other language environments like python, C++ etc.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Your snippet works well using ...
SnippetCompiler.exe
from:
http://www.sliver.com/dotnet/SnippetCompiler/

I had to run this in IronPython, here is the code ...

''' ip_small_form101.py
using IronPython from
http://www.codeplex.com/ironpython
'''

import clr
clr.AddReference('System.Windows.Forms')

import System.Windows.Forms as swf

class Class1(swf.Form):
    def __init__(self):
        label = swf.Label()
        label.Text = "Hello world!"
        self.Controls.Add(label)

form = Class1()
swf.Application.Run(form)
ddanbe commented: Great! +15
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The smallest I can think of that doesn't enter the realm of IOCCC and actually opens a form is:

using System.Windows.Forms;

static class Program
{
    static void Main()
    {
        Application.Run(new Form());
    }
}

The bare minimum for a more realistic production quality application in my opinion would be:

using System;
using System.Windows.Forms;

static class Program
{
    [STAThread]
    static void Main()
    {
        AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
        {
            MessageBox.Show("Load Error: " + e.ExceptionObject.ToString());
            MessageBox.Show("An error was detected at load time", "Load Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            if (Application.MessageLoop)
            {
                Application.Exit();
            }
            else
            {
                Environment.Exit(-1);
            }
        };

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form());
    }
}

While you typically don't want to do anything that results in an unhandled exception (throwing from a form constructor or event handler, for instance), it does happen occasionally. So adding provisions in there to properly notify and exit even if the message loop hasn't started is an excellent idea.

ddanbe commented: awesome +15
pitic 15 Newbie Poster

Or just MessageBox.Show("Hello World!"); :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.