WPF C# 猜单词游戏开发:点击字母按钮填充缺失字母
Solution to Replace Underscores with Clicked Letters
Got it, let's fix this functionality step by step. Here's how you can make the letter buttons replace the underscores in their correct positions:
Key Changes Needed
- We need to store the target word (from
txtMot.Text) as a class-level variable so we can reference it when a letter button is clicked. - Modify the
btnLetter_Clickevent to check every position in the target word, and update the corresponding label if the clicked letter matches.
Modified Full Code
First, add a private field to your class to hold the target word:
// Add this at the class level (outside any method) private string _targetWord; private void TypedWord_Click(object sender, RoutedEventArgs e) { MessageBoxResult confirmatBoxResult = System.Windows.MessageBox.Show("Are you sure ?", "Confirm", System.Windows.MessageBoxButton.YesNo); if (confirmatBoxResult == MessageBoxResult.Yes) { // Store the target word for later use _targetWord = txtMot.Text.Trim(); // Generate letter buttons for (char i = 'a'; i < 'z'; i++) { Button btn = new Button(); btn.Content = i; btn.Width = 60; btn.Height = 60; btn.FontSize = 36; btn.Click += btnLetter_Click; // Simplified event handler syntax panel_lettre.Children.Add(btn); } // Generate underscore labels based on target word length panel_label.Children.Clear(); // Clear existing labels first (safe guard) foreach (char c in _targetWord) { Label Lbl = new Label(); Lbl.Content = "_"; Lbl.FontSize = 36; Lbl.Width = 30; panel_label.Children.Add(Lbl); } // Disable the trigger button after first click ((Button)sender).IsEnabled = false; } } private void btnLetter_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(_targetWord)) return; // Guard clause to avoid empty word errors Button clickedBtn = (Button)sender; char clickedLetter = Convert.ToChar(clickedBtn.Content); // Iterate through each character in the target word and matching label for (int i = 0; i < _targetWord.Length; i++) { if (_targetWord[i] == clickedLetter) { // Get the corresponding label and update its content Label targetLabel = (Label)panel_label.Children[i]; targetLabel.Content = clickedLetter; } } }
Explanation of Changes
- Class-level
_targetWordvariable: This stores the word entered intxtMotso we can cross-reference which positions match the clicked letter later. - Clearing existing labels: Added
panel_label.Children.Clear()to avoid duplicate labels if the trigger button is accidentally clicked multiple times (even though we disable it, it's a safe guard). - Simplified event handler: Shortened the event subscription syntax to
btn.Click += btnLetter_Click;(valid in C# 2.0+ for cleaner code). - Guard clause in
btnLetter_Click: Prevents runtime errors if the target word is empty or null. - Position matching logic: We loop through each index of the target word, check if the character matches the clicked letter, and update the corresponding label in
panel_label(since labels were created in the exact order of the target word's characters).
This will now correctly replace all matching underscores with the clicked letter—for example, clicking "t" when the target word is "test" will update the first and fourth labels to "t", resulting in t _ _ t.
内容的提问来源于stack exchange,提问作者Finalmix6
相关产品推荐
相关产品推荐

