Mada za sehemu hiiDesigning form with menus controlsMada 4
- Designing forms with basic controls
- Incorporating menus to forms
- Implementing menu items to execute specific tasks
- Implementing event handling for controls
Designing Forms with Basic Controls
In event-driven programming, graphical user interfaces (GUIs) allow users to interact with applications through visual elements like buttons, text boxes, and menus. This topic teaches you how to design forms using Windows Forms (WinForms) in C#, add basic controls to them, and make these controls respond to user actions through event handlers.
Windows Forms (WinForms) is a GUI framework in C# that provides a drag-and-drop interface for building desktop applications. Instead of writing sequential code, WinForms applications respond to events such as button clicks, text input, and mouse movements. This makes applications interactive and user-friendly.
When you create a WinForms application, you start with a blank Form, which serves as the main window or container. You then add controls onto this form to create your user interface.
The following table lists the most commonly used controls you will work with:
| Control | Description |
|---|---|
| Form | The main window that holds other controls |
| Label | Displays non-editable text for instructions |
| TextBox | Provides an input field for users to type text |
| Button | A clickable element that triggers an action |
| ListView | Displays a list of items in rows and columns |
To start designing forms with basic controls, follow these steps:
- Open Visual Studio
- Click File → New → Project
- Select C#: Windows Forms App (.NET Framework)
- Enter a project name (for example,
MyFirstApp) - Choose a location to save your project
- Click Create
After loading, a blank form named Form1 appears. This is your design canvas where you will add controls.
To add controls to your form:
- Open Form1.cs [Design] view
- If the Toolbox is not visible, go to View → Toolbox
- From the Common Controls section, drag the desired control onto the form
For example, to create a simple login form:
- Drag a Label onto the form, then in the Properties window, change its
Textproperty to "Enter your name:" - Drag a TextBox below the label for user input
- Drag a Button below the text box, and change its
Textproperty to "Submit"
Adjust the size and position of controls to create a neat layout.
Each control has properties that define its appearance and behavior. Access these properties in the Properties window (right-click the control → Properties). Common properties include:
- Text: The displayed text (for labels, buttons, text boxes)
- Name: The variable name used in code (for example,
btnSubmit) - Size: The width and height of the control
- Location: The X and Y coordinates on the form
- Enabled: Whether the control is active or grayed out
- BackColor and ForeColor: Background and text colors
WinForms uses an event-driven model. When a user interacts with a control, an event is triggered, and an event handler (a block of code) executes in response.
Handling Button Click Events
To make a button perform an action when clicked:
- Double-click the button on the form, OR
- Right-click the button → Properties → Events (lightning icon ⚡) → Double-click the Click event
This generates an event handler method in the code. For example, to display a greeting when the user enters their name:
private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text; // Get user input from textbox
MessageBox.Show("Hello, " + name + "!", "Welcome");
}
Handling Text Input Events
Use the TextChanged event to respond when users type in a text box. A common use is enabling or disabling a button based on whether the text box has content:
private void textBox1_TextChanged(object sender, EventArgs e)
{
// Enable button only when textbox is not empty
button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text);
}
Handling Keyboard Events
To trigger an action when the user presses a specific key (like Enter):
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(sender, e); // Simulate button click
}
}
Handling Mouse Movement Events
Use MouseHover to change appearance when the mouse moves over a control, and MouseLeave to restore it when the mouse moves away:
private void button1_MouseHover(object sender, EventArgs e)
{
button1.BackColor = System.Drawing.Color.LightBlue;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.BackColor = System.Drawing.Color.LightGray;
}
The following example combines several controls and events to create a functional login form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string name = textBoxName.Text.Trim();
if (string.IsNullOrWhiteSpace(name))
{
MessageBox.Show("Please enter your name.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
MessageBox.Show("Welcome, " + name + "!", "Greeting",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void textBoxName_TextChanged(object sender, EventArgs e)
{
btnSubmit.Enabled = !string.IsNullOrWhiteSpace(textBoxName.Text);
}
private void textBoxName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnSubmit_Click(sender, e);
}
}
}
This form:
- Has a label, text box, and submit button
- Disables the button until the user types something
- Accepts both button click and Enter key to submit
- Validates input and shows appropriate messages
In Tanzania, designing forms with basic controls is used in small businesses such as shops and pharmacies to create simple inventory management systems. For example, a local duka in Dar es Salaam might use a form where shop attendants enter product names and prices using TextBoxes, click a Button to save records to a database, and view all products in a ListView — replacing handwritten stock books with a faster, more organized digital system.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
The student should be able to:
- Add, configure, and align basic controls on a form.
Tathmini ya matokeo
Forms with basic controls are designed with intuitive, user-friendly layouts tailored to meet specific task requirements.
Tathmini ya maarifa
Methods used: The trainee should explain how to:
- Add and configure controls like text boxes, labels, buttons, and menus on a form.
- Use properties and events to customize control behavior.
Principles: The student should explain the principle of:
- Form design, focusing on usability, accessibility, and user interaction.
- Layout alignment and spacing for clear and organized forms.
Theories: The trainee should explain: The trainee should explain:
- How event-driven programming enables interactive forms.
- The importance of user-centered design in form development.
Circumstantial knowledge:
Detailed knowledge about:
- Common challenges in form design, such as misaligned controls or unhandled events, and their solutions.
- Best practices for designing forms that enhance user experience.
- Real-world applications of form design in data entry, user registration, and search interfaces.
Swali
Which control in Windows Forms (WinForms) is used to display non-editable text for instructions to the user?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado