C#, Code

D&D Battle Tracker GUI

Download code at https://github.com/brettius/d-and-d-battle-tracker-gui

Download program at http://code.brettosteen.com

Built in C# and Visual Studio, this small GUI program is something I made to keep track of important statistics during a dungeons and dragons game, while referencing a PDF of a character sheet. The save and open buttons function and they read and write to a .txt file.

The first part of the code involves if the plus or – buttons for health are clicked, add 1 or minus 1 from the current health, with if else block to determine if a number is entered or not.

        private void addHealth_Click(object sender, EventArgs e)

        {

            int plusHealth;

            if (int.TryParse(currentHealth.Text, out plusHealth))

                {

                plusHealth += 1;

                currentHealth.Text = plusHealth.ToString();

            }

            else

            {

                MessageBox.Show("Enter a valid number");

            }

        }

        private void decreaseHealth_Click(object sender, EventArgs e)

        {

            int minusHealth;


            if (int.TryParse(currentHealth.Text, out minusHealth))

            {

                minusHealth -= 1;

                currentHealth.Text = minusHealth.ToString();

            }

            else

            {

                MessageBox.Show("Enter a valid number");

            }
        }

The next two sets of buttons and text boxes do the same thing. For an update to the program a method could be used here so the calculation for adding or subtracting is not coded three times.

        private void ragePlus_Click(object sender, EventArgs e)

        {

            int plusHealth;



            if (int.TryParse(rageBox2.Text, out plusHealth))

            {

                plusHealth += 1;

                rageBox2.Text = plusHealth.ToString();

            }

            else

            {

                MessageBox.Show("Enter a valid number");

            }

        }

        private void rageMinus_Click(object sender, EventArgs e)

        {

            int minusHealth;

            if (int.TryParse(rageBox2.Text, out minusHealth))

            {

                minusHealth -= 1;

                rageBox2.Text = minusHealth.ToString();

            }

            else

            {

                MessageBox.Show("Enter a valid number");

            }

        }

        private void hitDicePlus_Click(object sender, EventArgs e)

        {

            int plusHealth;

            if (int.TryParse(hitDiceBox.Text, out plusHealth))

            {

                plusHealth += 1;

                hitDiceBox.Text = plusHealth.ToString();

            }

            else

            {

                MessageBox.Show("Enter a valid number");

            }

        }

        private void hitDiceMinus_Click(object sender, EventArgs e)

        {

            int minusHealth;

            if (int.TryParse(hitDiceBox.Text, out minusHealth))

            {

                minusHealth -= 1;

                hitDiceBox.Text = minusHealth.ToString();

            }

            else

            {

                MessageBox.Show("Enter a valid number");

            }

        }

The next code is involved in using Streamwriter to send each text box to a text file. Future updates here are entering code so the data can be saved and loaded using a binary file.

        private void saveToolStripButton_Click(object sender, EventArgs e)

        {

            try

            {

                StreamWriter outputFile;

                outputFile = File.CreateText("tracker.txt");

                outputFile.WriteLine(nameBox.Text);

                outputFile.WriteLine(classBox.Text);

                outputFile.WriteLine(currentHealth.Text);

                outputFile.WriteLine(maxHealth.Text);

                outputFile.WriteLine(expBox.Text);

                outputFile.WriteLine(rageBox2.Text);

                outputFile.WriteLine(hitDiceBox.Text);

                outputFile.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

        private void openToolStripButton_Click(object sender, EventArgs e)

        {

            try

            {

                string name;

                StreamReader inputFile;

                inputFile = File.OpenText("tracker.txt");

                name = inputFile.ReadLine();

                nameBox.Text = name;

                name = inputFile.ReadLine();

                classBox.Text = name;

                name = inputFile.ReadLine();

                currentHealth.Text = name;

                name = inputFile.ReadLine();

                maxHealth.Text = name;

                name = inputFile.ReadLine();

                expBox.Text = name;

                name = inputFile.ReadLine();

                rageBox2.Text = name;

                name = inputFile.ReadLine();

                hitDiceBox.Text = name;

                inputFile.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

Clear button provided to start a new battle tracker.

        private void newToolStripButton_Click(object sender, EventArgs e)

        {

            nameBox.Clear();

            classBox.Clear();

            currentHealth.Clear();

            maxHealth.Clear();

            expBox.Clear();

            rageBox2.Clear();

            hitDiceBox.Clear();

        }

The code finishes here with an event for the help button.

        private void helpToolStripButton_Click(object sender, EventArgs e)

        {

            MessageBox.Show("You dont really need a helpfile, do you?");

        }

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *