techmore.in

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

  1. Open Visual Studio.

  2. 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.”
  3. Configure Your Project:

    • Enter a project name (e.g., HelloWorldForms).
    • Choose a location to save your project.
    • Click “Create.”
  4. Design the Form:

    • Visual Studio will open a designer view for Form1 (or whatever name you chose for the form).
  5. Add a Button:

    • From the Toolbox (usually on the left), drag a Button control onto the form.
  6. Set Button Properties:

    • Click on the button to select it.
    • In the Properties window, set the Text property to “Click Me.”
  7. 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:

      csharp
      private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello, World!"); }
  8. 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!”

Using Visual Studio Code with .NET CLI

  1. Open Visual Studio Code.

  2. Open Terminal:

    • Use `Ctrl+`` (backtick) to open the terminal.
  3. Create a New Windows Forms Project:

    bash
    dotnet new winforms -o HelloWorldForms cd HelloWorldForms
  4. Edit the Form Code:

    • Open Form1.cs and replace its contents with:

      csharp
      using 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!"); } }
  5. Run Your Application:

    bash
    dotnet 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!