Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
it have a textbox but i want to use buttons and not the textbox this is the code:
namespace HangMan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private System.Media.SoundPlayer mediaSoundPlayer = new System.Media.SoundPlayer();
        string w = "";
        List<Label> labels = new List<Label>();
        int missed = -1;
        string name = "dude";
        int choice = -1;
        int score = 0;

       

        enum bparts
        {
            Head, shirt, RightArm, LeftArm, RightLeg, LeftLeg,
        }

        

        void DrawBodyParts(bparts bp)
        {
           

            if (bp == bparts.Head)
                pictureBox1.Image = Image.FromFile("hung2.png");
            
            else if (bp == bparts.shirt)
                pictureBox1.Image = Image.FromFile("shirt.png");
            

            else if (bp == bparts.RightArm)
                pictureBox1.Image = Image.FromFile("rightarm.png");
            

            else if (bp == bparts.LeftArm)
                pictureBox1.Image = Image.FromFile("leftarm.png");
            

            else if (bp == bparts.RightLeg)
                pictureBox1.Image = Image.FromFile("rightleg.png");

            else if (bp == bparts.LeftLeg)
                pictureBox1.Image = Image.FromFile("leftleg.png");

            
        }


        void makelabels()
        {
            name = textBox1.Text;
            w = getRandomWords().ToLower();
            w.Replace(" ", "");


            char[] ch = w.ToCharArray();
            int space = 569 / ch.Length - 1;

            for (int i = 0; i < ch.Length - 1; i++)
            {

                labels.Add(new Label());
                labels[i].Location = new Point((i * space) + 10, 109);
                labels[i].Parent = gb2;
                labels[i].Text = "_";
                labels[i].BringToFront();
                labels[i].CreateControl();

            }

            label1.Text = "Length: " + (ch.Length - 1).ToString();

        }

        void Drawstick()
        {
            //Graphics hp = panel1.CreateGraphics();
            //Pen p = new Pen(Color.Black, 10);
            //hp.DrawLine(p, new Point(170, 337), new Point(170, 8));
            //hp.DrawLine(p, new Point(175, 8), new Point(105, 8));
            //hp.DrawLine(p, new Point(100, 0), new Point(100, 50));

            // a tryout only :P
        }


        string getRandomWords()
        {

            // Default is "Motivating Words" 0: Adjectives/ 1:Sports / 2:Animals / 
            // 3:countries /4:Motivating words/5:Action Words


            //If program didn't work you have to remove all commented lines , and read from WEB


            // WebClient wc = new WebClient();
            

            // string list = wc.DownloadString("http://dictionary-thesaurus.com/wordlists/MotivatingWords%28101%29.txt");

            string list="";

            if (choice == 0)
                // list = wc.DownloadString("http://dictionary-thesaurus.com/wordlists/Adjectives%2850%29.txt");

               
                list = System.IO.File.ReadAllText("adjectives.txt");


            else if (choice == 1)
                // list = wc.DownloadString("http://dictionary-thesaurus.com/wordlists/Sportsgames%28133%29.txt");

                list = System.IO.File.ReadAllText("sports.txt");

            else if (choice == 2)
                // list = wc.DownloadString("http://dictionary-thesaurus.com/wordlists/Animals%2865%29.txt");

                list = System.IO.File.ReadAllText("Animals.txt");


            else if (choice == 3)
                // list = wc.DownloadString("http://dictionary-thesaurus.com/wordlists/Countries%2893%29.txt");

                list = System.IO.File.ReadAllText("countries.txt");


            else if (choice == 4)
               // list = wc.DownloadString("http://dictionary-thesaurus.com/wordlists/MotivatingWords%28101%29.txt");

                list = System.IO.File.ReadAllText("motivating.txt");


            else if (choice == 5)
                // list = wc.DownloadString("http://dictionary-thesaurus.com/wordlists/ActionWords%28114%29.txt");

                list = System.IO.File.ReadAllText("actions.txt");


            string[] words = list.Split('\n');
            Random rand = new Random();
            return words[rand.Next(0, words.Length - 1)];
        }


        private void Form1_Shown(object sender, EventArgs e)
        {
            Drawstick();
            makelabels();
        }

        private void Letter_Click(object sender, EventArgs e)
        {


            try
            {
                char letter = textBox1.Text.ToLower().ToCharArray()[0];
                
                
                if (!char.IsLetter(letter))
                {
                    errorProvider1.BlinkRate = 100;
                    errorProvider1.SetError(textBox1, "Only a letter Dude!");
                }

                textBox1.Text = "";

                if (w.Contains(letter))
                {

                    string location = @"Backs.wav";
                    mediaSoundPlayer.SoundLocation = location;
                    mediaSoundPlayer.Play();

                    char[] LS = w.ToLower().ToCharArray();
                    for (int i = 0; i < LS.Length; i++)
                    {
                        if (LS[i] == letter)
                        {
                            labels[i].Text = letter.ToString();
                            answer.Image = Image.FromFile("okay.jpg");
                        }

                    }

                    foreach (Label l in labels)
                        if (l.Text == "__") return;
                    MessageBox.Show("You've Guessed it " + name + "\n\n You saved this innocent man", "Mabrook");
                    newgame();
                    score++;
                    label3.Text = "Score: " + score.ToString();



                }
                else
                {

                    string location = @"steak.wav";
                    mediaSoundPlayer.SoundLocation = location;
                    mediaSoundPlayer.Play();


                    answer.Image = Image.FromFile("wrong.GIF");
                    label2.Text += " " + letter.ToString() + " |";
                    missed++;
                    DrawBodyParts((bparts)missed);
                    if (missed == 5)
                    {
                        MessageBox.Show("You're so lucky " + name + ", you're not our Hangman! \n\n You've Lost :)\n\n Word Was: " + w);
                        newgame();
                    }

                }

                textBox1.Text = "";
            
        }

        catch(Exception X)
            {
        MessageBox.Show("Please enter a letter");
            }
    }

        void newgame()
        {
            getRandomWords();
            Drawstick();
            makelabels();
            label2.Text = "m i s s e d : ";
            textBox1.Text = "";
            missed = -1;
            pictureBox1.Image = Image.FromFile("hung1.png");
            
        }

        

        private void Form1_Load(object sender, EventArgs e)
        {
            string location = @"Shot.wav";
            mediaSoundPlayer.SoundLocation = location;
            mediaSoundPlayer.Play();

            Form2 welcome = new Form2();
            welcome.ShowDialog();
            welcome.Dispose();

            
            choice = welcome.returnSelect();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
      //<pre lang="c#">

}

private void button3_Click(object sender, EventArgs e)
{

}

private void GB3_Enter(object sender, EventArgs e)
{

}
}
}

What I have tried:

i tried to add buttons but a only write the letters in the textbox but i need to do enter and play
Posted
Updated 28-May-17 19:23pm

1 solution

Don't use buttons - 26 of them is a pain: you can do it, just add 26 buttons, give each a Text property between "A" and "Z" then give them all the same Click handler:
private void CharacterKey__Click(object sender, System.EventArgs e)
   {
   Button b = sender as Button;
   if (b != null)
      {
      string pressed = b.Text;
      ...
      }
   }

But I'd handle the TextChanged event of the TextBox, get the character from there and clear the TextBox instead. That doesn't need ENTER to work.
 
Share this answer
 
Comments
Esteban Flores Villegas 29-May-17 2:58am    
Um i have a question , i dont say that its a textbox and i add the letter with the keyboard ther a do click in a button to enter the letter , can you say me or explain me how i can remove it or change it, because i want to enter the letters only with the buttons and with the keyboard but no write it in a textbox and do click .
OriginalGriff 29-May-17 3:51am    
Um ... yes you do: "it have a textbox but i want to use buttons ..."
Please, try to explain in fuller terms exactly what you want to do - using shorthand doesn't help because I get no context other than exactly what you type!
Esteban Flores Villegas 29-May-17 3:58am    
the game have a textbox with a button to play, then if i put buttons 26 A to Z to play it how can i do it? to remove the textbox and the button, and play with only the other buttons A-Z , and dont use the textbox
OriginalGriff 29-May-17 4:13am    
I told you how to do that: "just add 26 buttons, ... "
WHich part is difficult for you? I'll try to explain in more detail.
Esteban Flores Villegas 29-May-17 4:19am    
XD Ok.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900