Buttons TextBoxes Controls - potatoscript/csharp GitHub Wiki

πŸ₯” Buttons, TextBoxes, and Controls in C# πŸ₯”


🎯 What are Controls in C#?

Imagine you’re building a potato vending machine πŸ₯”πŸ€–. You need:

  • πŸ“ TextBoxes – So customers can type their names and choose their potatoes.
  • πŸ–±οΈ Buttons – To let them place an order.
  • πŸ“’ Labels – To show messages like β€œThank you for buying potatoes!”

These items that users interact with in a Windows Form are called Controls in C#. 🎁✨

A control is any item on your form that helps users interact with your application. There are many types of controls in C#, but today we’ll focus on:

  • πŸ–±οΈ Buttons
  • πŸ“ TextBoxes
  • πŸ“’ Labels

πŸ₯” Why Use Controls?

Controls help make your application: βœ… User-friendly
βœ… Interactive
βœ… Easy to understand

Without controls, users would need to type everything manually in the console. But with controls, they can just click, type, and view! πŸ₯³


🎨 Overview of Basic Controls

πŸ–₯️ Control πŸ“ Purpose 🎁 Example
πŸ–±οΈ Button Perform an action Submit an order
πŸ“ TextBox Input text Enter name or details
πŸ“’ Label Display text or messages Show order confirmation
🎨 ComboBox Choose from a dropdown list Select potato type
πŸ“š ListBox Display multiple options List available potato types

πŸ–±οΈ Button Control in C#

A Button is like the β€œOrder Now” button on your potato vending machine.
When a customer clicks it, something happens! πŸŽ‰


🎁 Step 1: Add a Button to Your Form

  • Drag a Button from the toolbox onto the form.
  • Change the Text property to Order Now.
  • Set the Name property to btnOrder.

🎁 Step 2: Add Click Event to the Button

When the button is clicked, we want to show a Thank You message.

private void btnOrder_Click(object sender, EventArgs e)
{
    MessageBox.Show("πŸ₯” Thank you for ordering potatoes!");
}

βœ… Explanation:

  • btnOrder_Click – This method is triggered when the button is clicked.
  • MessageBox.Show() – Displays a message.

πŸ§‘β€πŸ’» Complete Button Example:

using System;
using System.Windows.Forms;

namespace PotatoFormApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOrder_Click(object sender, EventArgs e)
        {
            MessageBox.Show("πŸ₯” Thank you for ordering potatoes!");
        }
    }
}

πŸ“ TextBox Control in C#

A TextBox is like a β€œCustomer Name” field where customers type their names before ordering.


🎁 Step 1: Add a TextBox to Your Form

  • Drag a TextBox from the toolbox onto the form.
  • Change the Name property to txtName.
  • Add a Label with the text Enter Your Name: to guide the user.

🎁 Step 2: Get Text from a TextBox

To get the value from a TextBox, use the Text property:

string customerName = txtName.Text;

βœ… Explanation:

  • txtName.Text – Gets the text the user entered in the TextBox.

πŸ§‘β€πŸ’» Complete TextBox Example:

private void btnOrder_Click(object sender, EventArgs e)
{
    string customerName = txtName.Text;

    if (string.IsNullOrEmpty(customerName))
    {
        MessageBox.Show("❌ Please enter your name.");
    }
    else
    {
        MessageBox.Show($"πŸ₯” Hello {customerName}, your potatoes are on the way!");
    }
}

πŸ“’ Label Control in C#

A Label is like a signboard that shows messages to customers at your potato vending machine.
You can use it to display information or guide the user.


🎁 Step 1: Add a Label to Your Form

  • Drag a Label from the toolbox onto the form.
  • Change the Text property to Enter Your Name:.

🎁 Step 2: Set or Update Label Text

To change the label text in code, use the Text property:

lblMessage.Text = "πŸ₯” Order received! Preparing your potatoes...";

βœ… Explanation:

  • lblMessage.Text – Updates the label’s text dynamically.

πŸ§‘β€πŸ’» Complete Label Example:

private void btnOrder_Click(object sender, EventArgs e)
{
    string customerName = txtName.Text;

    if (string.IsNullOrEmpty(customerName))
    {
        lblMessage.Text = "❌ Please enter your name.";
    }
    else
    {
        lblMessage.Text = $"πŸ₯” Hello {customerName}, your potatoes are being prepared!";
    }
}

🎯 Putting It All Together – Create a Potato Order Form


🎁 Form Design:

  • TextBox: txtName – To enter customer’s name.
  • Button: btnOrder – To place the order.
  • Label: lblMessage – To display confirmation.

πŸ§‘β€πŸ’» Complete Code for Potato Order Form:

using System;
using System.Windows.Forms;

namespace PotatoFormApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOrder_Click(object sender, EventArgs e)
        {
            // Get the customer name from the TextBox
            string customerName = txtName.Text;

            // Check if the name is empty
            if (string.IsNullOrEmpty(customerName))
            {
                lblMessage.Text = "❌ Please enter your name.";
            }
            else
            {
                // Update label to confirm order
                lblMessage.Text = $"πŸ₯” Hello {customerName}, your potato order is being processed!";
            }
        }
    }
}

πŸ₯” Adding Multiple Controls

You can add more controls to improve the form:

  • πŸ“ TextBox: For potato type.
  • πŸ”’ NumericUpDown: For quantity.
  • 🎁 ComboBox: For additional options.
  • πŸ“š ListBox: To list available potato types.

πŸŽ‰ Challenge Time!

πŸ“ Task:

  1. Create a form with:
    • πŸ“ A TextBox to enter the customer’s name.
    • πŸ–±οΈ A Button to submit the order.
    • πŸ“’ A Label to display a thank-you message.
  2. Add another button that clears the TextBox and resets the form.

🎁 Bonus: Form Reset Button Code

private void btnReset_Click(object sender, EventArgs e)
{
    txtName.Text = "";         // Clear the TextBox
    lblMessage.Text = "";      // Clear the Label
    txtName.Focus();           // Set focus back to TextBox
}

βœ… Explanation:

  • txtName.Text = "" – Clears the TextBox.
  • lblMessage.Text = "" – Clears the Label.
  • txtName.Focus() – Puts the cursor back in the TextBox.

πŸ₯” Summary of Controls in C#

🎨 Control πŸ“ Purpose
πŸ–±οΈ Button Perform an action when clicked
πŸ“ TextBox Get input from the user
πŸ“’ Label Show messages or instructions
πŸ“š ListBox Display multiple selectable items
🎨 ComboBox Allow user to select an option