Having a lot of trouble in my quest to complete my Hangman game.

I've made an alteration to the program though. Rather than having a text box and the user typing a letter in, I've decided to add command buttons from A-Z.

I can't seem to pass the letter to the method which has all the logic. Check CmdZ Click method and you'll see what I mean:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hang
{
    public partial class Form1 : Form
    {
        string word;
        int length;
        Random random = new Random();
        string[] myArray = { "BLUE", "RED", "GOLD", "BLACK" };
        string letter;
        string tempString = null;
        string sentLetter;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MakeLabel();
        }

        private void MakeLabel()
        {

            lblOne.Visible = false;
            lblTwo.Visible = false;
            lblThree.Visible = false;
            lblFour.Visible = false;

            word = myArray[random.Next(0, myArray.Length)];
            length = word.Length;

            for (int i = 0; i <= length; i++)
            {
                lblWord.Text += "_ ";
            }

            Controls.Add(lblWord);
            lblWord.Visible = true;
        }

        public void Game(string letterz)
        {
            letter = letterz;
            int index = word.IndexOf(letter);

            if (index != -1)
            {
                tempString = lblWord.Text;
                MessageBox.Show(letter);
            }

        }

        private void cmdZ_Click(object sender, EventArgs e)
        {
            letter = "Z";
            Game(letter);

        }

        private void cmdY_Click(object sender, EventArgs e)
        {

        }

        private void cmdX_Click(object sender, EventArgs e)
        {

        }

        private void cmdW_Click(object sender, EventArgs e)
        {

        }

        private void cmdV_Click(object sender, EventArgs e)
        {

        }

        private void cmdU_Click(object sender, EventArgs e)
        {

        }

        private void cmdT_Click(object sender, EventArgs e)
        {

        }

        private void cmdS_Click(object sender, EventArgs e)
        {

        }

        private void cmdR_Click(object sender, EventArgs e)
        {

        }

        private void cmdQ_Click(object sender, EventArgs e)
        {

        }

        private void cmdO_Click(object sender, EventArgs e)
        {

        }

        private void cmdP_Click(object sender, EventArgs e)
        {

        }

        private void cmdN_Click(object sender, EventArgs e)
        {

        }

        private void cmdM_Click(object sender, EventArgs e)
        {

        }

        private void cmdL_Click(object sender, EventArgs e)
        {

        }

        private void cmdK_Click(object sender, EventArgs e)
        {

        }

        private void cmdJ_Click(object sender, EventArgs e)
        {

        }

        private void cmdI_Click(object sender, EventArgs e)
        {

        }

        private void cmdH_Click(object sender, EventArgs e)
        {

        }

        private void cmdG_Click(object sender, EventArgs e)
        {

        }

        private void cmdAF_Click(object sender, EventArgs e)
        {

        }

        private void cmdE_Click(object sender, EventArgs e)
        {

        }

        private void cmdD_Click(object sender, EventArgs e)
        {

        }

        private void cmdC_Click(object sender, EventArgs e)
        {

        }

        private void cmdB_Click(object sender, EventArgs e)
        {

        }

        private void cmdA_Click(object sender, EventArgs e)
        {

        }
    }
}

There's a pic added for illustrative purposes.

Just use one click handler instead of twenty or so.

private void button1_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button; //find out which button
            letter = btn.Text; //get the letter
            //etc
        }

Let all the eventhandlers point to the same Click.

I'm not following you. Tried to do it but....Not making 100% sense.

Please elaborate?

Start a new WindowsFormapplication, in the Form.cs file fill in the extra code you see here:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;

        public Form1()
        {
            InitializeComponent();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(29, 121);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(156, 121);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            //this.button2.Click += new System.EventHandler(this.button2_Click);
            //let it point to the Click event of button1 also
            /****/
            this.button2.Click += new System.EventHandler(this.button1_Click);
            /****/
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button; //get the button that was clicked
            MessageBox.Show("The button that was clicked was" + " " + btn.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // not used
        }
    }

Start the application, you will see 2 buttons. Click on them and see!
Hope you get it what I mean, else just ask a question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.