Reputation: 27
I just started learning C#, and I have an assigment where I have to
However, when ever I click somewhere on the screen, it draws a line with p1 being 0,0 and p2 being somewhere unlogically (different for each actor, but always the same location)
I've probably made some huge mistakes and I know my code is really bad, but please keep in mind I just started learning and I'm having quite some difficulties :)
would be great if anyone know how to fix this!
Relevant part (full code beneath):
int x;
int y;
Point p1;
Point p2;
List<Ellipses> ellipselijst = new List<Ellipses>();
List<Textbox> boxlijst = new List<Textbox>();
List<Lijn> lijnlijst = new List<Lijn>();
int actorselected;
int clicklocationx;
int clicklocationy;
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
Ellipses mooieellipse = new Ellipses(x, y);
ellipselijst.Add(mooieellipse);
Textbox mooiebox = new Textbox(x, y);
boxlijst.Add(mooiebox);
Lijn mooielijn = new Lijn(p1, p2);
lijnlijst.Add(mooielijn);
int clicklocationx = e.X;
int clicklocationy = e.Y;
this.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (radiolijn.Checked == true)
{
//Draw vanaf actor 1
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 1)
{
p1 = new Point (actor1.Location.X, actor1.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
//Draw vanaf actor 2
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 2)
{
p1 = new Point(actor2.Location.X, actor2.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
//Draw vanaf actor 3
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 3)
{
p1 = new Point(actor3.Location.X, actor3.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
}
}
private void actor1_Click(object sender, EventArgs e)
{
actorselected = 1;
}
private void actor2_Click(object sender, EventArgs e)
{
actorselected = 2;
}
private void actor3_Click(object sender, EventArgs e)
{
actorselected = 3;
}
Class "lijn":
public class Lijn
{
public Point punt1;
public Point punt2;
public Lijn(Point p1, Point p2)
{
punt1 = p1;
punt2 = p2;
}
}
Full code:
public partial class Formcase : Form
{
int aantalactors = 0;
public Formcase()
{
InitializeComponent();
actor1.Visible = false;
actor2.Visible = false;
actor3.Visible = false;
}
private void addactorbtn_Click(object sender, EventArgs e)
{
aantalactors++;
if (aantalactors == 1)
{
actor1.Visible = true;
}
if (aantalactors == 2)
{
actor2.Visible = true;
}
if (aantalactors == 3)
{
actor3.Visible = true;
}
}
int x;
int y;
Point p1;
Point p2;
List<Ellipses> ellipselijst = new List<Ellipses>();
List<Textbox> boxlijst = new List<Textbox>();
List<Lijn> lijnlijst = new List<Lijn>();
int actorselected;
int clicklocationx;
int clicklocationy;
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
Ellipses mooieellipse = new Ellipses(x, y);
ellipselijst.Add(mooieellipse);
Textbox mooiebox = new Textbox(x, y);
boxlijst.Add(mooiebox);
Lijn mooielijn = new Lijn(p1, p2);
lijnlijst.Add(mooielijn);
int clicklocationx = e.X;
int clicklocationy = e.Y;
this.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (radioellipse.Checked == true)
{
foreach (Ellipses p in ellipselijst)
{
g.DrawEllipse(new Pen(Brushes.Black), new Rectangle(new Point(p.x - 50, p.y - 50), new Size(100, 100)));
}
}
if (radiobtntekst.Checked == true)
{
foreach (Textbox t in boxlijst)
{
TextBox txtbox = new TextBox();
txtbox.Location = new Point(t.x + 125, t.y + 25);
this.Controls.Add(txtbox);
txtbox.BringToFront();
}
}
if (radiolijn.Checked == true)
{
//Draw vanaf actor 1
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 1)
{
p1 = new Point (actor1.Location.X, actor1.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
//Draw vanaf actor 2
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 2)
{
p1 = new Point(actor2.Location.X, actor2.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
//Draw vanaf actor 3
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 3)
{
p1 = new Point(actor3.Location.X, actor3.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
}
}
private void actor1_Click(object sender, EventArgs e)
{
actorselected = 1;
}
private void actor2_Click(object sender, EventArgs e)
{
actorselected = 2;
}
private void actor3_Click(object sender, EventArgs e)
{
actorselected = 3;
}
}
Upvotes: 0
Views: 702
Reputation: 28839
You have two sets of clicklocation variables - one global and one local to your mouse click handler. Get rid of the latter.
Upvotes: 1