Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
        }
    }
}
Posted
Updated 31-Jul-21 23:07pm
v2

Look at the documentation: PointF Struct (System.Drawing) | Microsoft Docs[^]. It clearly shows that the constructor does not take a string.

As to your other question your code is completely wrong. You should create a loop that iterates through the points collection and draws a line between each and every pair in the collection.

[edit]
Don't convert numbers (especially floating point) to strings in order to save them to a file. Write them out as their binary values so you get the exact values back when you reload them. You can even use the built in serialize/deserialize classes provided by .NET
[/edit]
 
Share this answer
 
v3
There is no overload of the PointF struct which takes a string parameter.
Think about it: if you pass a string to a method that needs two floating point values, how does it know which value is which or even that there are two separate values in the string at all?
What you are going to have to do is look at the file and find out what is actually stored, then break it up and use TryParse methods to "recover" the X and Y values.
Hint: PointF.ToString generates strings that look like this for my current culture (though the number format may change in yours):
{X=12.3, Y=55.9}
So you need to extract the numbers from that.
There are many ways to do that: String.Split, String.Substring (with IndexOf or IndexOfAny), and Regex are the ones which spring to mind here.
 
Share this answer
 
Try like this:
string fileName = "points.txt";
List<PointF> Points = new List<PointF>();

for (int i = 0; i < 10; i++)
{
    Points.Add(new PointF(i, (float)i / 2));
}

StreamWriter writer = new StreamWriter(fileName);
foreach (var a in Points)
    writer.WriteLine($"{a.X};{a.Y}");
writer.Close();


Points.Clear();
StreamReader Reader = new StreamReader(fileName, true);
string str = Reader.ReadLine();

while (str != null)
{
    Console.WriteLine(str);
    var x = Convert.ToDouble(str.Split(';')[0]);
    var y = Convert.ToDouble(str.Split(';')[1]);
    Points.Add(new PointF((float)x, (float)y));
    str = Reader.ReadLine();
}


Reader.Close();
 
Share this answer
 
thanks
with your Code problem of load points is completely solved
but a new challenge
i can draw point on main form and i can save them on a file and then load from file
but when i insert a picturebox in my form
and send all of codes of form to picturebox
For example (All codes of
private void Form1_MouseClick(object sender, MouseEventArgs e)

to
<pre>        private void picturebox1_MouseClick(object sender, MouseEventArgs e)




but my app can not draw points on picture box (only can in main form!)
 
Share this answer
 
v3
Comments
RickZeeland 1-Aug-21 5:27am    
See: https://stackoverflow.com/questions/2729751/how-do-i-draw-a-circle-and-line-in-the-picturebox
The second answer shows how to use the paint event of the picture box.
(you should post this as a new question btw)
Richard Deeming 2-Aug-21 5:42am    
Your new question is not a "solution" to your old question.

And by marking your post as "the answer" to your question, it looks like you are trying to abuse the system.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900