sodcalculator

10 Tech How-To

Build a Windows Desktop App with C and .NET

Guide 10-28187 · Updated · Step by step

This guide walks you through creating a native Windows desktop application using C# and .NET (specifically .NET 8 or later with Windows Forms). You’ll build a simple but functional app—a temperature converter—that compiles into an .exe you can run on any Windows machine. No prior GUI experience is needed, but basic C# syntax helps.

What You’ll Need

  • A Windows 10 or Windows 11 PC (64-bit recommended).
  • .NET SDK 8.0 or later installed. Download from the official Microsoft .NET website. Verify with dotnet --version in a terminal.
  • A code editor: Visual Studio 2022 Community (free), Visual Studio Code with the C# extension, or even Notepad (though not recommended).
  • Basic familiarity with the command line (PowerShell or Command Prompt).

Step-by-Step

  1. Open a terminal (PowerShell or CMD). Create a new folder for your project and navigate into it: mkdir MyWinApp then cd MyWinApp.
  2. Create a new Windows Forms project by running: dotnet new winforms -n MyWinApp. This generates a project file (MyWinApp.csproj) and a default Form1.cs with a blank window.
  3. Run the default app to verify everything works: dotnet run. A blank window should appear. Close it and return to the terminal.
  4. Open the project in your editor. If using VS Code, type code . in the terminal. In Visual Studio, open the .csproj file.
  5. Design the UI. Open Form1.cs (or Form1.Designer.cs if you’re using the visual designer). For simplicity, we’ll add controls programmatically. Replace the contents of Form1.cs with the following code:
using System;
using System.Windows.Forms;

namespace MyWinApp
{
    public partial class Form1 : Form
    {
        private TextBox inputBox;
        private ComboBox fromUnit;
        private ComboBox toUnit;
        private Button convertButton;
        private Label resultLabel;

        public Form1()
        {
            InitializeComponent();
            BuildUI();
        }

        private void BuildUI()
        {
            this.Text = "Temperature Converter";
            this.Size = new System.Drawing.Size(400, 250);

            inputBox = new TextBox { Left = 20, Top = 20, Width = 100 };
            fromUnit = new ComboBox { Left = 130, Top = 20, Width = 80 };
            fromUnit.Items.AddRange(new object[] { "Celsius", "Fahrenheit", "Kelvin" });
            fromUnit.SelectedIndex = 0;

            toUnit = new ComboBox { Left = 220, Top = 20, Width = 80 };
            toUnit.Items.AddRange(new object[] { "Celsius", "Fahrenheit", "Kelvin" });
            toUnit.SelectedIndex = 1;

            convertButton = new Button { Text = "Convert", Left = 20, Top = 60, Width = 100 };
            convertButton.Click += ConvertButton_Click;

            resultLabel = new Label { Left = 20, Top = 100, Width = 300, Font = new System.Drawing.Font("Arial", 12) };

            this.Controls.Add(inputBox);
            this.Controls.Add(fromUnit);
            this.Controls.Add(toUnit);
            this.Controls.Add(convertButton);
            this.Controls.Add(resultLabel);
        }

        private void ConvertButton_Click(object sender, EventArgs e)
        {
            if (!double.TryParse(inputBox.Text, out double value))
            {
                resultLabel.Text = "Please enter a valid number.";
                return;
            }

            double celsius = fromUnit.SelectedIndex switch
            {
                0 => value,
                1 => (value - 32) * 5 / 9,
                2 => value - 273.15,
                _ => 0
            };

            double result = toUnit.SelectedIndex switch
            {
                0 => celsius,
                1 => celsius * 9 / 5 + 32,
                2 => celsius + 273.15,
                _ => 0
            };

            resultLabel.Text = $"{value} {fromUnit.SelectedItem} = {result:F2} {toUnit.SelectedItem}";
        }
    }
}
  1. Run the app again with dotnet run. Enter a number, pick units, and click Convert. The result should appear instantly.
  2. Publish a standalone executable (so it runs on machines without .NET installed): dotnet publish -c Release -r win-x64 --self-contained true -p:PublishSingleFile=true. The .exe will be in bin\Release\net8.0-windows\win-x64\publish\.
  3. Test the published exe by double-clicking it. If it opens, you’re done.

Common Mistakes

  • Forgetting to add controls to the form. If you create controls but don’t call this.Controls.Add(), they won’t appear.
  • Using the wrong .NET version. Windows Forms only works on Windows. If you target a cross-platform framework like .NET MAUI, the steps differ.
  • Not handling invalid input. If the user types letters, the app will crash unless you use double.TryParse.
  • Mixing up Celsius/Kelvin conversions. Kelvin has no negative values, and the offset is 273.15, not 273.
  • Running dotnet run from the wrong directory. Make sure you’re in the folder containing the .csproj file.

Tips

  • Use the Visual Studio designer for complex UIs—it’s much faster than manual layout.
  • Keep conversion logic in a separate class for easier testing.
  • Set AcceptButton on the form to the Convert button so pressing Enter triggers conversion.
  • Use Anchor or Dock properties to make controls resize nicely.
  • For production, consider adding error logging and input validation.

Troubleshooting

  • “dotnet” is not recognized: Install the .NET SDK and restart your terminal.
  • Project fails to build with designer errors: If you edited Form1.Designer.cs manually, ensure the InitializeComponent() method exists and matches the controls. Try deleting the Designer file and recreating the project.
  • App runs but shows a blank window: Check that you added controls in the constructor after InitializeComponent(), and that you called this.Controls.Add() for each.
  • Published exe doesn’t run on another PC: Make sure you published with --self-contained true and the correct -r win-x64 (or win-arm64 for ARM machines).
  • Button click does nothing: Verify you subscribed to the Click event with += and that the method signature matches (object sender, EventArgs e).

Frequently Asked Questions

Can I use C instead of C#?

Yes, but .NET is designed for C#. For pure C, you’d use Win32 API or a library like GTK, which is more complex.

Do I need Visual Studio?

No. The .NET CLI and any text editor work. Visual Studio just adds a visual designer and debugging tools.

Can I make a cross-platform app with this?

Windows Forms is Windows-only. For cross-platform, use .NET MAUI or Avalonia.

How do I add an icon to my exe?

Add an .ico file to the project and set app.ico in the .csproj file.

Why is my app flagged by antivirus?

Self-contained single-file exes sometimes trigger false positives. Sign the executable or use a framework-dependent publish to reduce this.

Can I make a database app?

Yes. Use Entity Framework Core with SQLite or SQL Server, and add DataGridView to display tables.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →