please help me on c# app

payam mohammadi 21 Reputation points
2021-06-23T05:26:34.467+00:00

This is main part of my C# code, this is a Windows Forms app.

My code works fine, and it runs the app very well, but I need to change it.

With this app, I can draw some lines in a picturebox, and after I draw the lines, then I can hide lines with press a Key "E" and I can show these lines with press the same key again.

When the lines are hidden, I can left click on the place of a drawn line by mouse, then the app shows that line, and I can hide this line again with a same key "E"

Now my problem is that I can not write a code that: when the mouse moves over the picturebox horizontally, I don't need to click, the app shows that line automatically and when the mouse passed through the picturebox on place of that line, then hide that line again and when mouse arrive to place of next line, app show that other line that mouse in on it and ...

What is your suggest to change the code?

you can see my app here: https://i.stack.imgur.com/fZySn.gif[^]

This is my code:

namespace lines
{
public partial class Form1 : Form
{
private Graphics g;
private bool drawingMode = true, lineSelected;
private Pen semiTransPen;
List<line> lines = new List<line>();
public Form1()
{
InitializeComponent();
var pb = pictureBox1;
pb.Size = new Size(400, 400);
pb.Location = new Point(20, 20);

Line line = null;

pb.MouseMove += (s, e) =>
{
if (e.Button == MouseButtons.Left && drawingMode)
{
line.End = e.Location;

if (e.Location.Y < 0)
line.End = new Point(e.Location.X, 0);
else if (e.Location.Y > pb.Size.Height)
line.End = new Point(e.Location.X, pb.Size.Height);

if (e.Location.X < 0)
line.End = new Point(0, e.Location.Y);
else if (e.Location.X > pb.Size.Width)
line.End = new Point(pb.Size.Width, e.Location.Y);

pb.Invalidate();
}

};
pb.MouseDown += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
if (drawingMode)
line = new Line (e.Location, e.Location, Pens.Red );
else
{
lineSelected = false;
foreach (var a in lines)
{
if (a.OnLine(e.Location))
{
a.linePen = semiTransPen = new Pen(Color.FromArgb(75, 0, 0, 255), 5);
lineSelected = true;
}
else
a.linePen = semiTransPen = new Pen(Color.FromArgb(1, 0, 0, 255), 5);
}
pb.Invalidate();
}
}
};
pb.MouseUp += (s, e) =>
{
if (e.Button == MouseButtons.Left && drawingMode && line.Magnitude() > 1)
lines.Add(line);
};
pb.Paint += (s, e) =>
{
g = pb.CreateGraphics();

if (line != null)
e.Graphics.DrawLine(line.linePen, line.Start, line.End);

foreach (var l in lines)
e.Graphics.DrawLine(l.linePen, l.Start, l.End);

};
this.KeyPress += (s, e) =>
{
if (e.KeyChar == 'e')
{
if (drawingMode)
{
foreach (var a in lines)
{
a.linePen = semiTransPen = new Pen(Color.FromArgb(1, 0, 0, 255), 5);
}
}

else
{
foreach (var a in lines)
{
a.linePen = Pens.Red;
}
lineSelected = false;
}
drawingMode = !drawingMode;
pb.Invalidate();
}
};
}

class Line
{
public Line(Point s, Point e, Pen p)
{
Start = s;
End = e;
linePen = p;
}
public Line(string str)
{
Point s = new Point(0,0);
s.X = Int32.Parse(str.Substring(0, str.IndexOf(' ')));
str = str.Substring(str.IndexOf(' ') + 1);
s.Y = Int32.Parse(str.Substring(0, str.IndexOf(' ')));
str = str.Substring(str.IndexOf(' ') + 1);
Start = s;
s.X = Int32.Parse(str.Substring(0, str.IndexOf(' ')));
str = str.Substring(str.IndexOf(' ') + 1);
s.Y = Int32.Parse(str);
End = s;
linePen = Pens.Red;
}
public Pen linePen;
public Point Start { get; set; }
public Point End { get; set; }

public bool OnLine(Point p)
{
if (p == End || p == Start)
return true;

Point v1 = new Point(p.X - Start.X, p.Y - Start.Y);
Point v2 = new Point(p.X - End.X, p.Y - End.Y);

double d1 = v1.X * v2.X + v1.Y * v2.Y;
double d2 = Math.Sqrt(v1.X * v1.X + v1.Y * v1.Y) * Math.Sqrt(v2.X * v2.X + v2.Y * v2.Y);

d1 /= d2;

return (Math.Abs(d1 + 1) < 0.01);
}

public int Magnitude()
{
Point v1 = new Point(End.X - Start.X, End.Y - Start.Y);
return v1.X * v1.X + v1.Y * v1.Y;
}

public override string ToString()
{
return Start.X + " " + Start.Y + " " + End.X + " " + End.Y;
}
}
}

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,820 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-06-24T02:03:37.37+00:00

    Hi payammohammadi-6610,
    You can use MouseMove event to achieve it.
    Please add the following code segment in to your code:

    pb.MouseMove += (s, e) =>  
                {  
                    if (!drawingMode)  
                    {  
                        lineSelected = false;  
                        foreach (var a in lines)  
                        {  
                            if (a.OnLine(e.Location))  
                            {  
                                a.linePen = semiTransPen = new Pen(Color.FromArgb(75, 0, 0, 255), 5);  
                                lineSelected = true;  
                            }  
                            else  
                                a.linePen = semiTransPen = new Pen(Color.FromArgb(1, 0, 0, 255), 5);  
                        }  
      
                        pb.Invalidate();  
                    }  
      
                };  
    

    The result:
    108699-624.gif
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful