C#窗体程序导入文件老是出现“未将对象引用设置到对象的实例”,该怎么解决?(下面为代码)
namespace 图形编程实验
{
public partial class Form1 : Form
{
struct MeasPoint
{
public string Name;
public double x;
public double y;
}
private double m_MaxX, m_MaxY, m_MinX, m_MinY;
private Rectangle m_rect;
private double m_scale;
private List<MeasPoint> m_ptlst;
public Form1()
{
InitializeComponent();
m_ptlst = new List<MeasPoint>();
}
private double CalcScale()
{
double ds = 1.0;
double dsx, dsy;
if ((m_MaxX - m_MinX != 0) && (m_MaxY - m_MinY != 0))
{
dsx = Math.Abs((m_MaxX - m_MinX) / m_rect.Height);
dsy = Math.Abs((m_MaxY - m_MinY) / m_rect.Width);
ds = Math.Max(dsx, dsy);
}
else
{
if (m_MaxX - m_MinX != 0)
{
ds = Math.Abs((m_MaxX - m_MinX) / m_rect.Height);
}
else
{
if (m_MaxY - m_MinY != 0)
{
ds = Math.Abs((m_MaxY - m_MinY) / m_rect.Width);
}
else
{
ds = 1;
}
}
}
return ds;
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openDlg = new OpenFileDialog();
//指定打开文本文件(后缀名为cor)
openDlg.Filter = "武大科傻(cor)文件|*.cor|所有文件(*.*)|*.*";
DialogResult drt = openDlg.ShowDialog();
if (drt == DialogResult.OK)
{
string sFileName = openDlg.FileName;
try
{
if (m_ptlst!= null)
m_ptlst.Clear();
FileStream file = new FileStream(sFileName, FileMode.Open);
StreamReader sr = new StreamReader(file, Encoding.Default);
string st = sr.ReadLine();
int ic = 0;
while (st!= null)
{
ic++;
if (ic <= 4)
{
st = sr.ReadLine();
continue;
}
}
//切割字符串
StringSplit spt = new StringSplitFun(" ");
spt.Duplicates = false;
string[] sts = spt.Splitting(st);
int icols = sts.Count();
if (icols != 5)
{
st = sr.ReadLine();
}
MeasPoint pt = new MeasPoint();
pt.Name = sts[0];
pt.x = double.Parse(sts[1]);
pt.y = double.Parse(sts[2]);
m_ptlst.Add(pt);
st = sr.ReadLine();
sr.Close();
file.Close();
m_MaxX = 0.0;
m_MaxY= 0.0;
m_MinX = 0.0;
m_MinY = 0.0;
GetMaxMinXY(ref m_MaxX, ref m_MaxY, ref m_MinX, ref m_MinY);
m_rect = pictureBox1.ClientRectangle;
m_scale = CalcScale();
pictureBox1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void GetMaxMinXY(ref double dmaxX, ref double dmaxY, ref double dminX, ref double dminY)
{
if (m_ptlst.Count <= 0)
return;
dmaxX = -1.0;
dmaxY = -1.0;
dminX = 1000000000;
dminY = 1000000000;
foreach (MeasPoint pt in m_ptlst)
{
if (dmaxX < pt.x)
dmaxX = pt.x;
if (dmaxY < pt.y)
dmaxY = pt.y;
if (dminX > pt.x)
dminX = pt.x;
if (dminY > pt.y)
dminY = pt.y;
}
}
private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
{
if (m_ptlst == null)
return;
if (m_ptlst.Count <= 0)
return;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Brush bush = new SolidBrush(Color.Red);
Font myFont = new Font("宋体", 8, FontStyle.Bold);
foreach (MeasPoint pt in m_ptlst)
{
double dx = (pt.y - m_MinY) / m_scale;
double dy = m_rect.Height - (pt.x - m_MinX) / m_scale;
int ix = (int)dx - 3;
int iy = (int)dy - 3;
Point[] point = new Point[3];
point[0] = new Point(ix, iy - 3);
point[1] = new Point(ix - 3, iy + 3);
point[2] = new Point(ix + 3, iy + 3);
g.FillPolygon(bush, point);
g.DrawString(pt.Name, myFont, bush, ix - 3, iy + 5);
}
}