如何在C#注册表单中利用ArrayList将数据分配至对应Label控件?
Hey there! Let's walk through exactly how to build this registration form with data storage and navigation. I'll use C# WinForms since your question references controls like TextBox, DateTimePicker, Label, and buttons.
Step 1: Create a User Class to Encapsulate Data
First, it's best to create a simple class to hold each user's information—this makes storing and retrieving data from the ArrayList way cleaner than storing random strings or objects.
public class User { // Adjust these properties to match your 3 TextBoxes (e.g., Username, Phone, Address) public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public DateTime RegistrationDate { get; set; } }
Step 2: Declare Global Variables in Your Form
Add these variables to your form class to keep track of the user list and the currently viewed user:
// Stores all registered users private ArrayList userList = new ArrayList(); // Tracks the index of the user currently displayed (starts at -1 for no users) private int currentUserIndex = -1;
Step 3: Implement the Add Button
This button will collect input from your controls, validate it, and add a new User to the ArrayList.
private void btnAdd_Click(object sender, EventArgs e) { // Replace these control names with your actual TextBox/DateTimePicker IDs User newUser = new User { FirstName = txtFirstName.Text.Trim(), LastName = txtLastName.Text.Trim(), Email = txtEmail.Text.Trim(), RegistrationDate = dtpRegistrationDate.Value }; // Basic validation to ensure required fields aren't empty if (string.IsNullOrEmpty(newUser.FirstName) || string.IsNullOrEmpty(newUser.Email)) { MessageBox.Show("Please fill in all required fields!"); return; } // Add the new user to the list userList.Add(newUser); // Set current index to the newly added user currentUserIndex = userList.Count - 1; // Clear input fields for the next registration txtFirstName.Clear(); txtLastName.Clear(); txtEmail.Clear(); dtpRegistrationDate.Value = DateTime.Now; // Immediately show the newly added user's info DisplayUserInfo(currentUserIndex); }
Step 4: Create a Reusable Method to Display User Data
Instead of repeating code in multiple buttons, make a helper method to render user info into your Label:
private void DisplayUserInfo(int index) { // Handle invalid index cases if (index < 0 || index >= userList.Count) { lblUserDetails.Text = "No user selected!"; return; } // Cast the ArrayList item to our User class (required because ArrayList is non-generic) User selectedUser = (User)userList[index]; // Format the info for the Label—customize this string to match your needs lblUserDetails.Text = $"User {index + 1} / {userList.Count}\n" + $"First Name: {selectedUser.FirstName}\n" + $"Last Name: {selectedUser.LastName}\n" + $"Email: {selectedUser.Email}\n" + $"Registered On: {selectedUser.RegistrationDate:yyyy-MM-dd HH:mm}"; }
Step 5: Implement the Show Button
This button will display the current user's info (or the first user if none is selected yet):
private void btnShow_Click(object sender, EventArgs e) { if (userList.Count == 0) { MessageBox.Show("No users have been registered yet!"); return; } // If no user is selected, default to the first one if (currentUserIndex == -1) { currentUserIndex = 0; } DisplayUserInfo(currentUserIndex); }
Step 6: Implement Previous and Next Navigation Buttons
These buttons let users switch between stored users, with boundary checks to prevent invalid navigation:
Prev Button
private void btnPrev_Click(object sender, EventArgs e) { if (userList.Count == 0) { MessageBox.Show("There are no users to navigate through!"); return; } if (currentUserIndex > 0) { currentUserIndex--; DisplayUserInfo(currentUserIndex); } else { MessageBox.Show("You're already viewing the first user!"); } }
Next Button
private void btnNext_Click(object sender, EventArgs e) { if (userList.Count == 0) { MessageBox.Show("There are no users to navigate through!"); return; } if (currentUserIndex < userList.Count - 1) { currentUserIndex++; DisplayUserInfo(currentUserIndex); } else { MessageBox.Show("You're already viewing the last user!"); } }
Quick Note: ArrayList vs. Generic List
While ArrayList works here, modern C# development prefers using List<User> (a generic collection) instead. It eliminates the need for casting and provides type safety. To switch, just replace:
private ArrayList userList = new ArrayList();
with:
private List<User> userList = new List<User>();
No other changes are needed—all the rest of the code will work the same!
内容的提问来源于stack exchange,提问作者Cavid Hummatov

