create a application window in csharp

To create a new application window in C#, we can make use of Windows Forms. Windows Forms is a graphical user interface (GUI) class library included in the .NET Framework. It provides developers with the tools to create desktop applications that have a rich user interface.

Here's an example code snippet that creates a simple application window using Windows Forms:

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

namespace MyApplication
{
    public class MainForm : Form
    {
        public MainForm()
        {
            Text = "My Application"; // Set the title of the window
            Width = 300; // Set the width of the window
            Height = 200; // Set the height of the window
        }
        
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new MainForm()); // Launch the window
        }
    }
}
539 chars
23 lines

In this example, we create a new class called MainForm which inherits from Form. We set the window's title, width, and height in the constructor. We also provide a Main method that launches the window when the application is run.

To run the application, we call Application.Run(new MainForm()) which creates a new instance of our MainForm class and displays it on the screen.

Note that you will also need to include the System.Windows.Forms namespace at the top of your file to access the Form class.

gistlibby LogSnag