C# Hello World
To create a "Hello, World!" application using Windows Forms in C#, follow these steps to set up a simple Windows Forms application that displays a message box when a button is clicked:
Using Visual Studio
Open Visual Studio.
Create a New Project:
- Click on “Create a new project.”
- Select “Windows Forms App (.NET Framework)” or “Windows Forms App” (for .NET Core/5/6/7) depending on your setup.
- Click “Next.”
Configure Your Project:
- Enter a project name (e.g.,
HelloWorldForms
). - Choose a location to save your project.
- Click “Create.”
- Enter a project name (e.g.,
Design the Form:
- Visual Studio will open a designer view for
Form1
(or whatever name you chose for the form).
- Visual Studio will open a designer view for
Add a Button:
- From the Toolbox (usually on the left), drag a
Button
control onto the form.
- From the Toolbox (usually on the left), drag a
Set Button Properties:
- Click on the button to select it.
- In the Properties window, set the
Text
property to “Click Me.”
Add Code for the Button Click Event:
Double-click the button to create a click event handler.
Visual Studio will generate an event handler method in
Form1.cs
(or the code-behind file). Replace the code in this method with:csharpprivate void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello, World!"); }
Run Your Application:
- Press
F5
or click the “Start” button to build and run your application. - A window will appear with the button labeled “Click Me.” Clicking this button will display a message box with “Hello, World!”
- Press
Using Visual Studio Code with .NET CLI
Open Visual Studio Code.
Open Terminal:
- Use `Ctrl+`` (backtick) to open the terminal.
Create a New Windows Forms Project:
bashdotnet new winforms -o HelloWorldForms cd HelloWorldForms
Edit the Form Code:
Open
Form1.cs
and replace its contents with:csharpusing System; using System.Windows.Forms; public partial class Form1 : Form { private Button button1; public Form1() { InitializeComponent(); } private void InitializeComponent() { this.button1 = new Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(100, 100); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "Click Me"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.ClientSize = new System.Drawing.Size(284, 261); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Hello World"; this.ResumeLayout(false); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello, World!"); } }
Run Your Application:
bashdotnet run
- The form should appear with a button labeled "Click Me." Clicking this button will show a message box with "Hello, World!"
Explanation
- Windows Forms: A graphical user interface (GUI) toolkit for .NET that enables the creation of desktop applications with a Windows look and feel.
- Button Control: A clickable button on the form which triggers actions when clicked.
- MessageBox: A standard dialog box that can be used to display a message to the user.
Feel free to ask if you have any more questions or need additional assistance!