hi guys
i wrote a code for winform c#
i want to select 28 points in my form (or in a picturebox)
then with a button i can save coorditane of all points in a text file
and it is working and made text file correctly
but problem is when i want to load those points from saved text file (this line of my code have problem =>
Points.Add(new PointF(str));
)
please if u can help me to solve it
other problem is when i select points, my app will draw a line between last 2 points only
i would like to my app show all lines of between all of my points (not only last 2 points)
if u can solve it too
thanks
i am waiting your kindly help
What I have tried:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
namespace howto_segment_intersection
{
public partial class Form1 : Form
{
private Button LoadBtn;
private Button SaveBtn;
FileDialog openFileDialog1 = new OpenFileDialog();
private SaveFileDialog Savefile;
List<PointF> Points = new List<PointF>();
public Form1()
{
InitializeComponent();
this.SaveBtn = new Button();
this.LoadBtn = new Button();
this.LoadBtn.Anchor = AnchorStyles.Bottom;
this.LoadBtn.BackColor = Color.DarkGreen;
this.LoadBtn.FlatAppearance.BorderColor = Color.Gold;
this.LoadBtn.FlatStyle = FlatStyle.Flat;
this.LoadBtn.ForeColor = Color.White;
this.LoadBtn.Location = new System.Drawing.Point(10, 10);
this.LoadBtn.Name = "LoadBtn";
this.LoadBtn.Size = new Size(75, 34);
this.LoadBtn.TabIndex = 8;
this.LoadBtn.Text = "Load";
this.LoadBtn.UseVisualStyleBackColor = false;
this.LoadBtn.Click += new EventHandler(this.LoadBtn_Click);
this.SaveBtn.Anchor = AnchorStyles.Bottom;
this.SaveBtn.BackColor = Color.Maroon;
this.SaveBtn.FlatAppearance.BorderColor = Color.Red;
this.SaveBtn.FlatStyle = FlatStyle.Flat;
this.SaveBtn.ForeColor = Color.White;
this.SaveBtn.Location = new System.Drawing.Point(100, 10);
this.SaveBtn.Name = "SaveBtn";
this.SaveBtn.Size = new Size(75, 34);
this.SaveBtn.TabIndex = 8;
this.SaveBtn.Text = "Save";
this.SaveBtn.UseVisualStyleBackColor = false;
this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
this.Controls.Add((Control)this.SaveBtn);
this.Controls.Add((Control)this.LoadBtn);
}
private void SaveBtn_Click(object sender, EventArgs e)
{
this.Savefile = new SaveFileDialog();
Savefile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
Savefile.ShowDialog();
if (Savefile.ShowDialog() == DialogResult.OK)
{
StreamWriter writer = new StreamWriter(Savefile.FileName);
foreach (var a in Points)
writer.WriteLine(a.ToString());
writer.Close();
}
}
private void LoadBtn_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
StreamReader Reader = new StreamReader(openFileDialog1.FileName, true);
string str = Reader.ReadLine();
while (str != null)
{
Points.Add(new PointF(str));
str = Reader.ReadLine();
}
Reader.Close();
this.Invalidate();
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Points.Add(new PointF(e.X, e.Y));
this.Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if (Points.Count == 2)
{
e.Graphics.DrawLine(Pens.Green, Points[0], Points[1]);
}
if (Points.Count == 3)
{
e.Graphics.DrawLine(Pens.Green, Points[1], Points[2]);
}
if (Points.Count == 4)
{
e.Graphics.DrawLine(Pens.Green, Points[2], Points[3]);
}
if (Points.Count == 5)
{
e.Graphics.DrawLine(Pens.Green, Points[3], Points[4]);
}
if (Points.Count == 6)
{
e.Graphics.DrawLine(Pens.Green, Points[4], Points[5]);
}
if (Points.Count == 7)
{
e.Graphics.DrawLine(Pens.Green, Points[5], Points[6]);
}
if (Points.Count == 8)
{
e.Graphics.DrawLine(Pens.Green, Points[6], Points[7]);
}
if (Points.Count == 9)
{
e.Graphics.DrawLine(Pens.Green, Points[7], Points[8]);
}
if (Points.Count == 10)
{
e.Graphics.DrawLine(Pens.Green, Points[8], Points[9]);
}
if (Points.Count == 11)
{
e.Graphics.DrawLine(Pens.Green, Points[9], Points[10]);
}
if (Points.Count == 12)
{
e.Graphics.DrawLine(Pens.Green, Points[10], Points[11]);
}
if (Points.Count == 13)
{
e.Graphics.DrawLine(Pens.Green, Points[11], Points[12]);
}
if (Points.Count == 14)
{
e.Graphics.DrawLine(Pens.Green, Points[12], Points[13]);
}
if (Points.Count == 15)
{
e.Graphics.DrawLine(Pens.Green, Points[13], Points[14]);
}
if (Points.Count == 16)
{
e.Graphics.DrawLine(Pens.Green, Points[14], Points[15]);
}
if (Points.Count == 17)
{
e.Graphics.DrawLine(Pens.Green, Points[15], Points[16]);
}
if (Points.Count == 18)
{
e.Graphics.DrawLine(Pens.Green, Points[16], Points[17]);
}
if (Points.Count == 19)
{
e.Graphics.DrawLine(Pens.Green, Points[17], Points[18]);
}
if (Points.Count == 20)
{
e.Graphics.DrawLine(Pens.Green, Points[18], Points[19]);
}
if (Points.Count == 21)
{
e.Graphics.DrawLine(Pens.Green, Points[19], Points[20]);
}
if (Points.Count == 22)
{
e.Graphics.DrawLine(Pens.Green, Points[20], Points[21]);
}
if (Points.Count == 23)
{
e.Graphics.DrawLine(Pens.Green, Points[21], Points[22]);
}
if (Points.Count == 24)
{
e.Graphics.DrawLine(Pens.Green, Points[22], Points[23]);
}
if (Points.Count == 25)
{
e.Graphics.DrawLine(Pens.Green, Points[23], Points[24]);
}
if (Points.Count == 26)
{
e.Graphics.DrawLine(Pens.Green, Points[24], Points[25]);
}
if (Points.Count == 27)
{
e.Graphics.DrawLine(Pens.Green, Points[25], Points[26]);
}
if (Points.Count == 28)
{
e.Graphics.DrawLine(Pens.Green, Points[26], Points[27]);
}
foreach (PointF pt in Points)
{
DrawPoint(e.Graphics, pt, Brushes.Red, Pens.Black);
}
}
private void DrawPoint(Graphics gr, PointF pt, Brush brush, Pen pen)
{
const int RADIUS = 5;
gr.FillEllipse(brush,
pt.X - RADIUS, pt.Y - RADIUS,
2 * RADIUS, 2 * RADIUS);
gr.DrawEllipse(pen,
pt.X - RADIUS, pt.Y - RADIUS,
2 * RADIUS, 2 * RADIUS);
}
}
}