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:
- 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.
- 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 |