简体   繁体   English

(C# - Forms) 如何从 TextBox.Text 获取用户输入?

[英](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.我正在尝试编写一个将多边形绘制到 PictureBox 上的程序。 I want the user to enter values such as the center's X and Y point, length, angle, number of edges to textboxes.我希望用户输入值,例如中心的 X 和 Y 点、长度、角度、文本框的边数。 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.我想它将 TextBox.Text 作为空值或空字符串,因此我的代码的其他部分会失败。

How do I get TextBox.Text?如何获得 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);
            }
        }
    }
}

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)然后也使用这个:(如果 numberOfEdges = 0,这是你的 0 分区)

 double interiorAngle = 360;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# WPF:如何从另一个页面访问页面控件以获取 textbox.text - C# WPF : how can i access page controls from another page to get textbox.text 在C#单击按钮后,如何从DataGridView等于textbox.Text中选择一行? - How can I select a row from the DataGridView equals textbox.Text after clicking a button by C# ? C# 如何将 textbox.text 作为对象(在类中)变量导入? Windows Forms 应用程序 - C# How to import textbox.text as object(in class) variable? Windows Forms app 当对datagridview进行C#过滤时,如何将textbox.Text中的文本添加到datagridview.CurrentCell中? - How can I add text from textbox.Text to datagridview.CurrentCell when datagridview is filtered C#? Textbox.text值不能反映C#ASP.Net中代码隐藏的用户键入的值 - Textbox.text value not reflecting user typed value from codebehind C# ASP.Net 在C#中获取RadListBox ID值以更改Textbox.Text - Get RadListBox ID Value in C# to Change Textbox.Text 如何让文本框文本公开 C# forms - How can I get a textbox text to be public C# forms C#正则表达式从TextBox.Text匹配? - C# Regex Match from TextBox.Text? C#If(textbox.text = number)错误 - C# If(textbox.text=number) error C# Textbox.Text 转换成 SQL SmallMoney - C# Textbox.Text into SQL SmallMoney
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM