出于某种原因,我的代码不会绘制图上的第二个点。我从WindowsForm上的两个不同文本框中获取x,y值。我有一个包含x,y坐标的类。每次在文本框中输入新值时,我都会创建一个新对象,将两个新坐标添加到对象中并将该对象添加到列表中。绘图点
最后,我遍历对象列表并尝试绘制每个x,y坐标。为什么它没有显示所有的要点?
这里是我的代码:
Form1中
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PlotGraph
{
public partial class Form1 : Form
{
private List<TheList> allValuesList = new List<TheList>();
private int x = 240; // the position of the X axis
private int y = 0; // the position of the Y axis
public static Bitmap bmp = new Bitmap(360, 390);
public static Graphics g = Graphics.FromImage(bmp);
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
g.DrawLine(new Pen(Color.Red, 2), 5, 5, 5, 250);
g.DrawLine(new Pen(Color.Red, 2), 5, 250, 300, 250);
}
private void btnPlotGraph_Click(object sender, EventArgs e)
{
TheList latestCoordinate = new TheList();
if (textBoxX != null)
{
latestCoordinate.xCoordinate = Int16.Parse(textBoxX.Text);
}
if (textBoxY != null)
{
latestCoordinate.yCoordinate = Int16.Parse(textBoxY.Text);
}
allValuesList.Add(latestCoordinate);
plotTheValues(allValuesList);
}
public void plotTheValues(List<TheList> allValuesList)
{
Int16 x1 = 0;
Int16 y1 = 0;
foreach (TheList val in allValuesList)
{
x1 = val.xCoordinate;
y1 = val.yCoordinate;
g.DrawString("X", new Font("Calibri", 12), new SolidBrush(Color.Black), y + y1, x - x1);
PictureBox display = new PictureBox();
display.Width = ClientRectangle.Width;
display.Height = ClientRectangle.Height;
this.Controls.Add(display);
display.Image = bmp;
}
}
}
}
类的thelist
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PlotGraph
{
public class TheList
{
public Int16 xCoordinate = -1;
public Int16 yCoordinate = -1;
}
}
你已经接受了一个答案,所以只是留下你的问题。如果你抹掉它的内容,答案将是无用的。 – BoltClock