trying to create a adressbook where i can add, delete, and search for a user but im stuck help !
Loading
trying to create a adressbook where i can add, delete, and search for a user but im stuck help !
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ajay KumarRank #56 · 29K reputationPosted Sep 21, 2023, 6:12 AM EDT
Creating an address book application in Windows Forms using C# involves several steps. Below, I'll guide you through the process of creating a basic address book application that allows you to add, delete, and search for users.
Step 1: Set Up Your Windows Forms Project
Step 2: Design Your Form
Design your form by adding the necessary controls (e.g., labels, textboxes, buttons, and a DataGridView). Your form might look something like this:
Label: First Name TextBox: [First Name] Label: Last Name TextBox: [Last Name] Button: Add Button: Delete Button: Search DataGridView (to display the address book data)Step 3: Create a User Class
You should create a class to represent a user's information. This class should have properties for First Name, Last Name, and any other relevant information.
public class User { public string FirstName { get; set; } public string LastName { get; set; } // Add other properties like Address, Phone Number, etc., if needed. }Step 4: Create a List to Store Users
In your form's code-behind, create a list to store the user data.
List addressBook = new List();Step 5: Implement Adding Users
When the "Add" button is clicked, you can create a new
Userobject and add it to your list.private void AddButton_Click(object sender, EventArgs e) { string firstName = FirstNameTextBox.Text; string lastName = LastNameTextBox.Text; User newUser = new User { FirstName = firstName, LastName = lastName }; addressBook.Add(newUser); // Refresh the DataGridView to display the updated data. RefreshDataGridView(); }Step 6: Implement Deleting Users
When the "Delete" button is clicked, you can remove a user from the list based on the selected user in the DataGridView.
private void DeleteButton_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { int selectedIndex = dataGridView.SelectedRows[0].Index; addressBook.RemoveAt(selectedIndex); // Refresh the DataGridView to display the updated data. RefreshDataGridView(); } }Step 7: Implement Searching Users
When the "Search" button is clicked, you can search for users based on their first name or last name and display the results in the DataGridView.
private void SearchButton_Click(object sender, EventArgs e) { string searchQuery = SearchTextBox.Text.ToLower(); List searchResults = addressBook .Where(user => user.FirstName.ToLower().Contains(searchQuery) || user.LastName.ToLower().Contains(searchQuery)) .ToList(); dataGridView.DataSource = searchResults; }Step 8: Implement DataGridView Refresh Method
Create a method to refresh the DataGridView with the latest data from your address book list.
private void RefreshDataGridView() { dataGridView.DataSource = null; dataGridView.DataSource = addressBook; }Step 9: Wiring up Events
Don't forget to wire up the event handlers for your buttons (Add, Delete, Search) in the Form Designer or code-behind to make them functional.
Step 10: Test Your Application
Build and run your application to test the functionality.