I am pretty new to coding in general after being out of studying for 3 due to illness and I’m trying to wrap my head around the basics of C# for a project I am working on. My issue is with a basic shape drawing program using a windows form app that uses commands/parsing to output the desired shape. My program works perfectly as intended, but when I am trying to create a Unit Test for one function (draw rectangle) using the command “draw rectangle x y width height” but I am running into issues with what I believe to be to do with how I have structured my code, I am unsure though. Any help would be greatly appreciated and happy new year! 🙂

I will leave below the main areas of my code if anyone has time to have a brief look over it:

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace CommandParserApp
{
    public static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            MainForm mainForm = new MainForm();
            Application.Run(mainForm);
        }
    }

    public class MainForm : Form
    {
        //Declares the data stores for the program
        private CommandParser commandParser;
        private PictureBox canvasPictureBox;
        private TextBox commandTextBox;
        private ComboBox colorComboBox;
        private Button executeButton;
        private Button clearCanvasButton;

        public MainForm() //Initializes the main form
        {
            InitializeComponent();
            commandParser = new CommandParser();
        }

        private void InitializeComponent()
        {
            // Command TextBox that the user can enter commands into
            commandTextBox = new TextBox
            {
                Location = new Point(10, 10),
                Width = 300,
                Multiline = true
            };

            // Color ComboBox that allows the user to select a colour for their shape
            colorComboBox = new ComboBox
            {
                Location = new Point(320, 10),
                Width = 80,
                DropDownStyle = ComboBoxStyle.DropDownList //Uses a drop down list style on the combo box.
            };
            colorComboBox.Items.AddRange(new object[] { "Red", "Blue", "Green" }); //Adds the 3 colours to a range that the user can choose from.
            colorComboBox.SelectedIndex = 0;

            // Canvas PictureBox that is used to display shapes
            canvasPictureBox = new PictureBox
            {
                Location = new Point(10, 50),
                Size = new Size(400, 300),
                BackColor = Color.White
            };

            // Instructions Label that informs the user of how to use the program
            Label instructionsLabel = new Label
            {
                Location = new Point(10, 360),
                AutoSize = true,
                Text = "Instructions:n" +
                       "To draw a rectangle: draw rectangle x y width heightn" +
                       "To draw a circle: draw circle x y diametern" +
                       "To draw a triangle: draw triangle x1 y1 x2 y2 x3 y3"
            };

            // Example Commands Label just to show the user examples of working commands
            Label exampleCommandsLabel = new Label
            {
                Location = new Point(10, 440),
                AutoSize = true,
                Text = "Example Commands:n" +
                       "Rectangle: draw rectangle 50 50 100 80n" +
                       "Circle: draw circle 200 150 50 50n" +
                       "Triangle: draw triangle 50 300 200 300 125 200"
            };

            // Execute Button that runs the command and is linked to the executeButton method
            Button executeButton = new Button
            {
                Location = new Point(420, 10),
                Text = "Execute"
            };
            executeButton.Click += ExecuteButton_Click;

            // Clear Canvas Button, this allows the user to clear the canvas upon pressing the button
            Button clearCanvasButton = new Button
            {
                Location = new Point(420, 50),
                Text = "Clear Canvas"
            };
            clearCanvasButton.Click += ClearCanvasButton_Click;
            Button loadButton = new Button
            {
                Location = new Point(420, 90),
                Text = "Load"
            };
            loadButton.Click += LoadButton_Click; //Calls method when clicked

            // Save Button that allows the user to save the contents of the input box to a txt file
            Button saveButton = new Button
            {
                Location = new Point(420, 130),
                Text = "Save"
            };
            saveButton.Click += SaveButton_Click;

            // Adds the controls to the main form
            Controls.Add(commandTextBox);
            Controls.Add(colorComboBox);
            Controls.Add(canvasPictureBox);
            Controls.Add(instructionsLabel);
            Controls.Add(exampleCommandsLabel);
            Controls.Add(executeButton);
            Controls.Add(clearCanvasButton);
            Controls.Add(loadButton);
            Controls.Add(saveButton);
        }

        private void ExecuteButton_Click(object sender, EventArgs e)
        {
            // Get the command from the TextBox
            string command = commandTextBox.Text;

            // Get the selected color from the ComboBox
            Color selectedColor = GetSelectedColor();

            // Parse and execute the command
            try
            {
                commandParser.ExecuteCommand(command, selectedColor, canvasPictureBox.CreateGraphics());
            }
            catch (Exception ex) //Throws an error when command is invalid
            {
                MessageBox.Show($"Error: {ex.Message}", "Command Execution Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

Below is the code for my test plan, the area of the code with the error is the “MainForm mainForm = new MainForm():” section in the Arrange part.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Drawing;

[TestClass]
public class DrawingTests
{
    [TestMethod]
    public void DrawRectangleTest()
    {
        // Arrange
        MainForm mainForm = new MainForm();
        string command = "draw rectangle 50 50 50 50";
        mainForm.commandTextBox.Text = command;

        // Act
        mainForm.ExecuteButton_Click(null, null);

        // Assert
        
    }
}

Thanks for any help if anyone knows, no doubt it’s something stupid I have forgotten about since I last tried some coding.

EDIT: Upon adding a reference I have fixed the above error but I am now getting errors with the

mainForm.commandTextBox.Text = command;

// Act
mainForm.ExecuteButton_Click(null, null);

It seems to stay due to security of it, I cannot seem to use it in the test class, thanks again 🙂

Errors in Visual Studio

2