4
Answers

Hangman Character Issue

This is for a class, but our teacher had to go on medical leave, and the substitute doesn't know anything about coding, so I am completely stuck.  I created dynamic labels in a groupbox for the words that are randomly selected, and there are buttons that are not dynamic with the letters on them.  I am not sure how to reference the "non-existent" labels, and I could not find anything that said that you can reference all items in a groupbox at once. The program will not load, seeing as it is an error to reference specific dynamic elements.  I need to find a way to check the letter that is clicked against the letter that should be in the label (created via an array).  I have been fighting with this for the past week, so any help will be appreciated. Thank you.
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Hangman
{
   public partial class frmHang : Form
   {
      public frmHang()
      {
         InitializeComponent();
      }

      Random rndThing = new Random((int)DateTime.Now.Ticks);
      string[] strWords = new string[10]
      {
         "computer", "desktop", "hardware", "keyboard", "mouse", "glasses", "tower", "samsung",                "lenovo", "pencil"
      };

         private void frmHang_Load(object sender, EventArgs e)
         {

         }

      string strWordToGuess;

   private void btnStart_Click(object sender, EventArgs e)
   {
      Random rndWord = new Random();
      strWordToGuess = strWords[rndThing.Next(0, 10)];

      grpWordLetters.Controls.Clear();
      char[] wordChars = strWordToGuess.ToCharArray();
      int intWordLength = wordChars.Length;
      int refer = grpWordLetters.Width / intWordLength;

      for (int i = 0; i < intWordLength; i++)
      {
      Label lblCharacter = new Label();
      lblCharacter.Text = "_";
      lblCharacter.Location = new Point(10 + i * refer, grpWordLetters.Height - 30);
      lblCharacter.Parent = grpWordLetters;
      lblCharacter.BringToFront();
      }
   }

      private void btnA_Click(object sender, EventArgs e)
      {
      btnA.Enabled=false;
      }
   }
Answers (4)