Table of Contents

"Hello, World" with Visual Studio

In this tutorial, you will create a cross-platform desktop application using C# and Microsoft Visual Studio. The application will display a message box in response to a button click.

Microsoft Visual Studio is supported on Windows OS only. To develop on macOS or Linux, see "Hello, World" with Command-Line and VS Code.

Prerequisites

  1. Install Microsoft Visual Studio. Visual Studio 2022 is supported.
  2. Ensure the ".NET Desktop Development" or "ASP.NET and web development" workflow is installed.
  3. Download and install AlterNET UI Visual Studio extension.

Create New Project

  1. Open Visual Studio, and in the start window, select Create new project.

  2. On the Create new project page, locate the AlterNET UI Application template. Select it, then click Next. Create a new project in Visual Studio

  3. On the Configure your new project page set the project name to HelloWorld and specify the desired project location. When done, click Create.

  4. The project will be created, and you will be presented with a development environment. New project in Visual Studio after creation

  5. Press Ctrl+F5 to build and run the application. The application will start and display its window:

    Created Application Window

  6. In Visual Studio, open MainWindow.uixml. In the editor, change the Title attribute value from "HelloWorld" to "My First Application":

    <Window xmlns="http://schemas.alternetsoft.com/ui/2021"
            xmlns:x="http://schemas.alternetsoft.com/ui/2021/uixml"
            x:Class="HelloWorld.MainWindow"
            Title="My First Application">
    </Window>
    
  7. Press Ctrl+F5 to build and run the application and see its window title has changed accordingly.

Note

By default, the created project will use .NET 6.0 as a target framework. If .NET 6.0 runtime is not installed on your machine, you will be prompted to do so on the first application run.

Add Button to the Window

  1. In MainWindow.uixml, add the following markup:

    <Window xmlns="http://schemas.alternetsoft.com/ui/2021"
            xmlns:x="http://schemas.alternetsoft.com/ui/2021/uixml"
            x:Class="HelloWorld.MainWindow"
            Title="My First Application">
      <StackPanel>
        <Button Name="helloButton" Text="Say Hello" Margin="20" />
      </StackPanel>
    </Window>
    
  2. Run the application by pressing Ctrl+F5:

    Button Added

Write Code to Respond to the Button Click

  1. In MainWindow.uixml, add the Click attribute to the Button element like the following:

    <Window xmlns="http://schemas.alternetsoft.com/ui/2021"
            xmlns:x="http://schemas.alternetsoft.com/ui/2021/uixml"
            x:Class="HelloWorld.MainWindow"
            Title="My First Application">
      <StackPanel>
        <Button Name="helloButton" Text="Say Hello" Margin="20" Click="HelloButton_Click" />
      </StackPanel>
    </Window>
    

    This will bind the Click event to its handler, ' HelloButton_Click`.

  2. In MainWindow.uixml.cs file, add the following HelloButton_Click method:

    using System;
    using Alternet.UI;
    
    namespace HelloWorld
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void HelloButton_Click(object? sender, EventArgs e)
            {
                MessageBox.Show("Hello, world!");
            }
        }
    }
    
  3. You can use IntelliSense features provided by the AlterNET UI Visual Studio Extension:

    Event IntelliSense

  4. Run the application, then click Say Hello button. The message box appears:

    MessageBox on Click

Note

The application created in this tutorial can be compiled and run without modifications on all the supported platforms: Windows, macOS, and Linux.


Congratulations, you have successfully completed the "Hello, World" tutorial using Microsoft Visual Studio.

For a similar tutorial, but using command line tools and Visual Studio Code, see "Hello, World" with Command-Line and Visual Studio Code.