'(C# - Forms) How do i get user input from TextBox.Text?

I'm trying to write a program that paints a polygon onto a PictureBox. I want the user to enter values such as the center's X and Y point, length, angle, number of edges to textboxes. Then I want to use the values in textboxes as parameters.

The problem is that the program throws different types of exceptions as soon as I launch it without letting me enter any values into the textboxes. I guess it takes the TextBox.Text as a null value or an empty string so other parts of my code fail.

How do I get TextBox.Text?

Also, if you have any suggestions about calculating the vertex points of a polygon please share it with me.

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;
using ConsoleApp1;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public int CenterX, CenterY, Length, Angle;
        public int Edges;
        private void button1_Click(object sender, EventArgs e)
        {
            CenterX = int.Parse(textBox1.Text);
            CenterY = int.Parse(textBox2.Text);
            Length = int.Parse(textBox3.Text);
            Angle = int.Parse(textBox4.Text);
            Edges = int.Parse(textBox5.Text);
            pictureBox1.Invalidate();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            /////////////////////////////////////////////////////////////////////////////
            pictureBox1.CreateGraphics();
            /////////////////////////////////////////////////////////////////////////////
            int width = pictureBox1.ClientSize.Width;
            int height = pictureBox1.ClientSize.Height;
            int newWidth = width / 2;
            int newHeight = height / 2;
            e.Graphics.TranslateTransform((float)newWidth, (float)newHeight);
            /////////////////////////////////////////////////////////////////////////////
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            /////////////////////////////////////////////////////////////////////////////
            Pen pen = new Pen(Color.Black, 5);
            /////////////////////////////////////////////////////////////////////////////
            Polygon polygon = new Polygon(CenterX, CenterY);
            polygon.LENGTH = Length;
            polygon.ROTATIONANGLE = Angle;
            polygon.NUMBER_OF_EDGES = Edges;
            polygon.calculateEdgeCoordinates();
            polygon.rotatePolygon();
            /////////////////////////////////////////////////////////////////////////////
            PointF[] points = new PointF[polygon.rotatedPoints.Count];
            for (int i = 0; i < polygon.rotatedPoints.Count; i++)
            {
                points[i] = polygon.rotatedPoints[i];
            }
            e.Graphics.DrawPolygon(pen, points);
            e.Dispose();
        }
        public Form1()
        {
            InitializeComponent();           
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }
    }

    public class Polygon
    {
        Point2D center = new Point2D();
        public List<Point2D> edgePoints = new List<Point2D>();
        public List<PointF> rotatedPoints = new List<PointF>();
        private double radius, angle, length, rotationAngle; private int numberOfEdges;
        public double RADIUS { get => radius; set => radius = value; }
        public double LENGTH { get => length; set => length = value; }
        public double ROTATIONANGLE { get => rotationAngle; set => rotationAngle = value; }
        public int NUMBER_OF_EDGES { get => numberOfEdges; set => numberOfEdges = value; }

        public Polygon()
        {
            Point2D polarCoords = center.calculatePolarCoordinates();
            polarCoords.X = length;
            angle = polarCoords.Y;
        }
        public Polygon(double x, double y)
        {
            center.X = x;
            center.Y = y;
            Point2D polarCoords = center.calculatePolarCoordinates();
            polarCoords.X = length;
            angle = polarCoords.Y;
        }

        public void calculateEdgeCoordinates()
        {
            double interiorAngle = 360 / numberOfEdges;       
            for (int i=0; i < numberOfEdges; i++)
            {
                if (i == 0)
                {
                    Point2D point = new Point2D(length, angle);
                    edgePoints.Add(point);
                }
                else
                {
                    Point2D point = new Point2D(length, angle+interiorAngle);
                    edgePoints.Add(point);
                }                
            }
        }

        public void rotatePolygon()
        {
            for (int i=0; i < edgePoints.Count;  i++)
            {
                edgePoints[i].Y += rotationAngle;
                edgePoints[i].calculateCartesianCoordinates();

                PointF point = new PointF((float)(edgePoints[i].X), (float)(edgePoints[i].Y));
                rotatedPoints.Add(point);
            }
        }
    }
}



Solution 1:[1]

This is necessary to start off with

private void button1_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(textBox1.Text, out CenterX))
                CenterX = 0;
            if (!int.TryParse(textBox2.Text, out CenterY))
                CenterY = 0;
            if (!int.TryParse(textBox3.Text, out Length))
                Length = 0;
            if (!int.TryParse(textBox4.Text, out Angle))
                Angle = 0;
            if (!int.TryParse(textBox5.Text, out Edges))
                Edges = 0;
            pictureBox1.Invalidate();
        }

Then also use this: (here's your 0 division if numberOfEdges = 0)

 double interiorAngle = 360;

            if (numberOfEdges != 0)
                interiorAngle = 360 / numberOfEdges;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1