Home > Back-end >  Why wont my program draw the lines between 10 mouseclicks?
Why wont my program draw the lines between 10 mouseclicks?

Time:12-24

I am trying to make a program that will draw lines between 10 coordinates, determined by mouseclicks.

i can not get it to work. code:

EDIT:

i figured out i have to define the coordinates making x and y values. only it is still unclear how to do this.


public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    Point[] punten = new Point[2];
    int index = 0;
    int kliks = 0;
}

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
    Point[] punten = new Point[2];
    int index = 0;
    int kliks = 0;
    kliks = kliks   1;
    index = kliks;
    punten[index] = e.Location;

    if (index < 2)
    {
        punten[index] = e.Location;
    }
    else
    {
        Pen pen = new Pen(Color.Blue);
        Graphics papier = pictureBox1.CreateGraphics();
        papier.DrawLines(pen, punten);
    }
}        

I have re read the textbook 10 times.. its unclear where to create the variables intially, but if i dont create them under pictureBox1 mouse click, i also cant use them. Any help is appreciated.

greetings, Stefan.

CodePudding user response:

The Scope a variables is defined determines its accessibility within the process. You need to declare the punten array and the kliks int variables at the class level so they can be maintained across the button clicks.

public Form1()
{
    InitializeComponent();
}

// create variables at class scope so they are 
// accessible by all methods in this class and state is maintained
// across button clicks
private Point[] punten; 
private int kliks;

private void Form1_Load(object sender, EventArgs e)
{
    // initialize class level variables at form load
    punten = new Point[10];            
    kliks = 0;
}

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
    int index = kliks;
    if(index == 9) // arrays are zero based so our 10 items are at indexes 0 through 9
    {
        // append last mouse point clicked to list of points
        punten[index] = new Point(e.Location.X, e.Location.Y);

        // draw lines between all points
        Pen pen = new Pen(Color.Blue);
        Graphics papier = pictureBox1.CreateGraphics();
        papier.DrawLines(pen, punten);                
    }
    else
    {
        // append mouse point clicked to list of points
        punten[index] = new Point(e.Location.X, e.Location.Y);
    }
    kliks = kliks   1;
}


CodePudding user response:

i got it to work!

i need to keep telling my damn self: its all in the textbook... with the explanations given by you guys (declaring variables at class level) the instructions in the textbook work..

code:




public partial class Form1 : Form
    {
        Point[] punten = new Point[10];
        private int kliks = 0;
        private int lijst = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            kliks = kliks   1;
            lijst = kliks;
            punten[lijst] = e.Location;

            if (lijst < 5)
                

            {
                punten[lijst] = e.Location;

                }

            else
            {

                Pen pen = new Pen(Color.Blue);
                Graphics papier = pictureBox1.CreateGraphics();
                papier.DrawLines(pen, punten);
            }
        }
    }
}



  •  Tags:  
  • c#
  • Related