[英]C# WPF : how can i access page controls from another page to get 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.