Windows Forms中如何限制选手号码输入次数不超过指定圈数?
Fixing the Runner Lap Input Issue in Windows Forms
Hey there! Let's sort out this inconsistent lap count problem you're facing. The main flaw in your current code is using a single global count variable to track input times for all runners—this leads to mixed-up counts since it doesn't track each runner individually. A Dictionary<int, int> is perfect here because it lets us map each runner's number to their current lap count, keeping everything organized.
Step 1: Replace Global Variables with a Dictionary
First, remove your existing count and invalid variables, and add this dictionary as a member variable in your form class (outside any method):
// Tracks how many times each runner's number has been entered private Dictionary<int, int> _runnerLapCounts = new Dictionary<int, int>();
Step 2: Rewrite the textBox2_TextChanged Event
Here's the corrected event code that fixes the count issues and properly enforces the lap limit:
private void textBox2_TextChanged(object sender, EventArgs e) { textBox2.Focus(); label3.Text = string.Empty; // Clear previous error messages bool result = int.TryParse(textBox2.Text, out int runnerNumber); // Validate input: must be integer and in the valid runner list if (!result || !ReadRunnerNumbers2.Contains(runnerNumber)) { label3.Text = "Please enter a valid player number from the list"; return; } // Check if this runner has already reached the max lap count if (_runnerLapCounts.TryGetValue(runnerNumber, out int currentCount) && currentCount >= getLaps2) { MessageBox.Show("Max number of laps for this runner has been entered"); textBox2.Clear(); return; } // Update the lap count for this runner if (_runnerLapCounts.ContainsKey(runnerNumber)) { _runnerLapCounts[runnerNumber]++; } else { _runnerLapCounts[runnerNumber] = 1; } // Check if we just hit the max lap count if (_runnerLapCounts[runnerNumber] == getLaps2) { label3.Text = "Max laps for this runner has been entered"; } // Add to the order list and update stats RunnerOrder.Add(runnerNumber); PlayerNumbersEntered++; label8.Text = PlayerNumbersEntered.ToString(); // Fixed: tracks valid input count as intended textBox2.Clear(); }
Key Improvements Explained
- Per-Runner Count Tracking: The
_runnerLapCountsdictionary ensures each runner's input count is tracked independently—no more sharedcountvariable confusion. - Early Validation Checks: We first validate the input, then immediately block any entries for runners who've hit their lap limit, preventing accidental extra inputs.
- Clean State Management: We clear previous error messages at the start to avoid stale text, and properly handle first-time inputs by adding new entries to the dictionary.
- Fixed Stat Display: Updated
label8to show the valid input count instead of the runner number, matching your original comment's intent.
Additional Notes
- Ensure
ReadRunnerNumbers2(your valid runner list) is properly initialized before this event runs. - If you need to reset counts for a new race, just call
_runnerLapCounts.Clear();and resetRunnerOrderandPlayerNumbersEntered.
内容的提问来源于stack exchange,提问作者sJay89
相关产品推荐
相关产品推荐

