create a window in csharp

To create a window in C#, you can use the Windows Forms framework, which is part of the .NET Framework. Here is a code snippet that creates a simple window with a label:

main.cs
using System;
using System.Windows.Forms;

namespace MyNamespace
{
    class MyForm : Form
    {
        public MyForm()
        {
            // Set the title of the window
            this.Text = "My Window";
            
            // Create a label and add it to the window
            Label label = new Label();
            label.Text = "Hello, world!";
            this.Controls.Add(label);
        }
        
        static void Main()
        {
            Application.Run(new MyForm());
        }
    }
}
515 chars
25 lines

In this example, we define a new form named MyForm that inherits from the Form class. We set the window title using the Text property, and add a Label control to the form using the Controls.Add() method. Finally, we start the application by calling Application.Run() with an instance of MyForm.

gistlibby LogSnag