簡體   English   中英

(C# - Forms) 如何從 TextBox.Text 獲取用戶輸入?

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

我正在嘗試編寫一個將多邊形繪制到 PictureBox 上的程序。 我希望用戶輸入值,例如中心的 X 和 Y 點、長度、角度、文本框的邊數。 然后我想使用文本框中的值作為參數。

問題是程序在我啟動它時會立即引發不同類型的異常,而不會讓我在文本框中輸入任何值。 我想它將 TextBox.Text 作為空值或空字符串,因此我的代碼的其他部分會失敗。

如何獲得 TextBox.Text?

另外,如果您對計算多邊形的頂點有任何建議,請與我分享。

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);
            }
        }
    }
}

這是開始的必要條件

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();
        }

然后也使用這個:(如果 numberOfEdges = 0,這是你的 0 分區)

 double interiorAngle = 360;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM