C# Tutorial – Make a Binary Calculator Game in Windows Form Application and Visual Studio

In this tutorial we will make Binary calculator quiz game using windows form application and Visual Studio. This app will have 9 combo boxes, each combo box will contain either a 0 or 1. A random number will be generated and  the app will ask the user to solve that number in binary. All of the combo boxes have a value marked on top of them so we can select from 1, 2, 4, 8, 16, 32, 64, 128 and 256. If the binary number we have added is correct then the program will give us another number to solve or we can keep trying to solve it until we get it. This was a fun application to make because half way through I decided to add all of the components from the C# script. So all the combo boxes and labels are added dynamically from the script. Nothing was added from the tool box. Also the events are added to the program from the script and its also managed in the same script. I think this will give you a better idea on how to create a simple app just from the script and have it work jus the way you want it to.

Lesson Objectives –

  1. Create a binary calculator quiz game in windows form and visual studio
  2. Create the whole game from the C# script dynamically
  3. Add the combo boxes and labels from the script
  4. Add random colors to the labels and combo boxes
  5. change the form height, width and background color from the script
  6. Add events to the combo boxes dynamically

 

Video

Download the Binary Calculator Tutorial on GitHub
Full Source Code –

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 Binary_Calculator_Game_.NET_MOO_ICT
{
    public partial class Form1 : Form
    {

        int[] binaryValues = {1, 2, 4, 8, 16, 32, 64, 128, 256};
        Random random = new Random();
        List<ComboBox> comboBoxes = new List<ComboBox>();
        int total;
        int questionNumber;

        Label lblTotal = new Label();
        Label question = new Label();
        Label header = new Label();



        public Form1()
        {
            InitializeComponent();

            LoadGame();
        }


        private void LoadGame()
        {
            Array.Reverse(binaryValues);

            questionNumber = 12;

            this.BackColor = Color.FromArgb(64, 64, 64);
            this.Size = new Size(600, 383);

            // adding header label

            header.Text = "Binary Calculator Game " + Environment.NewLine + "by MOO ICT";
            header.AutoSize = false;
            header.Width = this.ClientSize.Width;
            header.Height = 70;
            header.TextAlign = ContentAlignment.MiddleCenter;
            header.ForeColor = Color.White;
            header.Font = new Font("Arial", 20);
            this.Controls.Add(header);

            // end of header

            // now add the questions label

            question.Text = "What is - " + questionNumber + " - in Binary?";
            question.AutoSize = false;
            question.Width = this.ClientSize.Width;
            question.Height = 50;
            question.TextAlign = ContentAlignment.MiddleCenter;
            question.Font = new Font("Arial", 20);
            question.Top = 180;
            question.ForeColor = Color.LightGreen;
            this.Controls.Add(question);

            // end of question label

            // adding total label

            lblTotal.Text = "Total: " + total;
            lblTotal.AutoSize = false;
            lblTotal.Width = this.ClientSize.Width;
            lblTotal.Height = 50;
            lblTotal.TextAlign = ContentAlignment.MiddleCenter;
            lblTotal.ForeColor = Color.White;
            lblTotal.Font = new Font("Arial", 20);
            lblTotal.Top = 230;
            this.Controls.Add(lblTotal);

            // end total label

            int x = 30;
            int y = 120;

            for (int i = 0; i < 9; i++)
            {
                ComboBox box = new ComboBox();

                box.Width = 50;
                box.Items.Add("0");
                box.Items.Add("1");
                box.SelectedIndex = 0;
                box.Font = new Font("Arial", 16);
                box.DropDownStyle = ComboBoxStyle.DropDownList;
                box.Tag = binaryValues[i].ToString();
                box.Left = x;
                box.Top = y;
                box.FlatStyle = FlatStyle.Flat;
                box.SelectionChangeCommitted += Box_SelectionChangeCommitted;
                comboBoxes.Add(box);

                // end of the combo boxes

                Label lblVal = new Label();
                lblVal.Text = binaryValues[i].ToString();
                lblVal.Font = new Font("Arial", 16);
                lblVal.Location = new Point(x, y - 32);
                lblVal.AutoSize = false;
                lblVal.Width = 50;
                lblVal.Height = 30;

                Color c = Color.FromArgb(random.Next(200, 255), random.Next(150, 255), random.Next(150, 255));

                lblVal.BackColor = c;
                lblVal.ForeColor = Color.Black;
                lblVal.TextAlign = ContentAlignment.MiddleCenter;
                box.BackColor = c;

                this.Controls.Add(box);
                this.Controls.Add(lblVal);

                x += 60;

            }

        }

        private void Box_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string binaryCode = null;
            total = 0;

            foreach (ComboBox combo in comboBoxes)
            {
                if (combo.SelectedIndex == 1)
                {
                    int i = Convert.ToInt32(combo.Tag);

                    total += i;
                }

                binaryCode += combo.Text;
            }


            lblTotal.Text = "Total: " + total + " Binary: " + binaryCode;

            if (total == questionNumber)
            {
                MessageBox.Show("Correct! Well done, Now try another", "Moo Says:");
                questionNumber = random.Next(1, 510);

                question.Text = "What is - " + questionNumber + " - in Binary?";
                total = 0;
                binaryCode = null;
                ResetGame();
                lblTotal.Text = "Total: " + total + " Binary: " + binaryCode;
            }
        }

        private void ResetGame()
        {

            foreach (ComboBox combo in comboBoxes)
            {
                combo.SelectedIndex = 0;
            }
        }

    }
}

 

 




Comments are closed.