To-Do List Application in C# - Base Level
Building a Simple To-Do List Application in C#
Creating a simple to-do list application is a great way to get started with C# and understand the basics of Windows Forms, a GUI toolkit included in the .NET framework. This guide will walk you through the process of building a basic to-do list application using C#.
Prerequisites
- Visual Studio: Install the latest version of Visual Studio with the .NET desktop development workload.
- Basic Knowledge of C#: Familiarity with C# programming is recommended.
Step 1: Create a New Windows Forms Project
- Open Visual Studio: Start Visual Studio and select "Create a new project."
- Select Project Template: Choose "Windows Forms App (.NET Framework)" and click "Next."
- Configure Project: Enter a project name (e.g., "ToDoListApp"), choose a location, and click "Create."
Step 2: Design the User Interface
Form Layout: Open
Form1.csin the designer view.Add Controls:
- TextBox: For entering new to-do items.
- Button: For adding items to the list.
- ListBox: For displaying the list of to-do items.
- Button: For removing selected items from the list.
Arrange Controls: Arrange the controls on the form to your liking.
Example Layout:
- TextBox: Place at the top for new item entry.
- Add Button: Next to the TextBox to add the item.
- ListBox: Below the TextBox and Add Button to display the items.
- Remove Button: Below the ListBox to remove selected items.
Step 3: Write the Code
Initialize the Form: Open
Form1.csin the code view.Add Event Handlers: Double-click the Add and Remove buttons in the designer to generate click event handlers.
Implement Add Button Logic:
csharpprivate void addButton_Click(object sender, EventArgs e) { string newItem = newItemTextBox.Text; if (!string.IsNullOrWhiteSpace(newItem)) { todoListBox.Items.Add(newItem); newItemTextBox.Clear(); } else { MessageBox.Show("Please enter a valid to-do item.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }Implement Remove Button Logic:
csharpprivate void removeButton_Click(object sender, EventArgs e) { if (todoListBox.SelectedItem != null) { todoListBox.Items.Remove(todoListBox.SelectedItem); } else { MessageBox.Show("Please select an item to remove.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }Form Load Event (Optional): You can initialize the form with some sample data or other settings.
csharpprivate void Form1_Load(object sender, EventArgs e) { // Sample data (optional) todoListBox.Items.Add("Sample Task 1"); todoListBox.Items.Add("Sample Task 2"); }
Step 4: Run the Application
- Build and Run: Press
F5to build and run the application. Ensure there are no errors. - Test Functionality: Add, view, and remove to-do items to test the application.
Complete Code Example
Form1.Designer.cs:
csharp
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.TextBox newItemTextBox;
private System.Windows.Forms.Button addButton;
private System.Windows.Forms.ListBox todoListBox;
private System.Windows.Forms.Button removeButton;
private void InitializeComponent()
{
this.newItemTextBox = new System.Windows.Forms.TextBox();
this.addButton = new System.Windows.Forms.Button();
this.todoListBox = new System.Windows.Forms.ListBox();
this.removeButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// newItemTextBox
//
this.newItemTextBox.Location = new System.Drawing.Point(12, 12);
this.newItemTextBox.Name = "newItemTextBox";
this.newItemTextBox.Size = new System.Drawing.Size(260, 20);
this.newItemTextBox.TabIndex = 0;
//
// addButton
//
this.addButton.Location = new System.Drawing.Point(278, 10);
this.addButton.Name = "addButton";
this.addButton.Size = new System.Drawing.Size(75, 23);
this.addButton.TabIndex = 1;
this.addButton.Text = "Add";
this.addButton.UseVisualStyleBackColor = true;
this.addButton.Click += new System.EventHandler(this.addButton_Click);
//
// todoListBox
//
this.todoListBox.FormattingEnabled = true;
this.todoListBox.Location = new System.Drawing.Point(12, 38);
this.todoListBox.Name = "todoListBox";
this.todoListBox.Size = new System.Drawing.Size(341, 199);
this.todoListBox.TabIndex = 2;
//
// removeButton
//
this.removeButton.Location = new System.Drawing.Point(12, 243);
this.removeButton.Name = "removeButton";
this.removeButton.Size = new System.Drawing.Size(341, 23);
this.removeButton.TabIndex = 3;
this.removeButton.Text = "Remove";
this.removeButton.UseVisualStyleBackColor = true;
this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(365, 278);
this.Controls.Add(this.removeButton);
this.Controls.Add(this.todoListBox);
this.Controls.Add(this.addButton);
this.Controls.Add(this.newItemTextBox);
this.Name = "Form1";
this.Text = "To-Do List";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
}
Form1.cs:
csharp
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addButton_Click(object sender, EventArgs e)
{
string newItem = newItemTextBox.Text;
if (!string.IsNullOrWhiteSpace(newItem))
{
todoListBox.Items.Add(newItem);
newItemTextBox.Clear();
}
else
{
MessageBox.Show("Please enter a valid to-do item.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void removeButton_Click(object sender, EventArgs e)
{
if (todoListBox.SelectedItem != null)
{
todoListBox.Items.Remove(todoListBox.SelectedItem);
}
else
{
MessageBox.Show("Please select an item to remove.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void Form1_Load(object sender, EventArgs e)
{
// Sample data (optional)
todoListBox.Items.Add("Sample Task 1");
todoListBox.Items.Add("Sample Task 2");
}
}
Conclusion
By following this guide, you have built a simple to-do list application using C# and Windows Forms. This project helps you understand the basics of C# GUI programming, event handling, and working with controls. You can further enhance this application by adding features like saving tasks to a file, setting task priorities, or implementing a search function. Happy coding!


Comments
Post a Comment