You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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_Click event 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

  1. Class-level _targetWord variable: This stores the word entered in txtMot so we can cross-reference which positions match the clicked letter later.
  2. 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).
  3. Simplified event handler: Shortened the event subscription syntax to btn.Click += btnLetter_Click; (valid in C# 2.0+ for cleaner code).
  4. Guard clause in btnLetter_Click: Prevents runtime errors if the target word is empty or null.
  5. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:06:01