Draw a line when you click a botton

Calvinche 21 Reputation points
2022-11-23T19:07:54.693+00:00

Hi!

I am doing a university project and I have to make and interface that show different routes depending the button you click.

So I have different buttons on interface and his method of clicking which is like this:

private void button_tienda1_Click(object sender, EventArgs e)  
        {  
            label_boton_pulsado.Text = "You choose A";  
        }  

As you can see, in this method I am able to change the text of a label that I have called ("label_boton_pulsado")

Then I have a method that ables to draw a line on a picture that I loaded.

private void pictureBox1_Paint(object sender, PaintEventArgs e)  
        {  
            //Event for drawing lines  
            Graphics g = e.Graphics;  
            //Configuration of the pencil  
            Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 5);  
  
            //Draw  
            g.DrawLine(greenPen, 220, 370, 220, 290);  
        }  

What I need is that the line only appears when the button is clicked. If it is not clicked, I do not want the line to appear.
Hope that someone could help me, thank you!

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,871 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,875 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,630 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.6K Reputation points
    2022-11-23T19:16:17.203+00:00

    Try something like this:

    private void button_tienda1_Click(object sender, EventArgs e)  
    {  
        label_boton_pulsado.Text = "You choose A";  
        drawLine = true;  
        Invalidate( );  
    }  
      
    bool drawLine = false;  
      
    private void pictureBox1_Paint(object sender, PaintEventArgs e)  
    {  
        if( drawLine)  
        {  
            Graphics g = e.Graphics;  
            //Configuration of the pencil  
            Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 5);  
            //Draw  
            g.DrawLine(greenPen, 220, 370, 220, 290);  
        }  
    }  
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Calvinche 21 Reputation points
    2022-11-23T19:38:47.68+00:00

    @Viorel And do you know how can I delete that first route if I click another button in order to have that other route?
    private void button_tienda1_Click(object sender, EventArgs e)
    {
    label_boton_pulsado.Text = "Se ha seleccionado Adidas";
    drawLine = true;
    pictureBox1.Invalidate();
    }
    bool drawLine = false;
    private void button_tienda2_Click(object sender, EventArgs e)
    {
    label_boton_pulsado.Text = "Se ha seleccionado Apple";
    drawLine_apple = true;
    pictureBox1.Invalidate();
    }
    bool drawLine_apple = false;
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
    if (drawLine)
    {
    Graphics g = e.Graphics;
    //Configuration of the pencil
    Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 5);
    //Draw
    g.DrawLine(greenPen, 220, 370, 220, 290);
    /////
    ///Ruta Adidas
    /////
    //Dibujamos la linea de entrada al C.C
    g.DrawLine(greenPen, 220, 290, 110, 290);
    g.DrawLine(greenPen, 110, 290, 110, 310);
    }
    if (drawLine_apple)
    {
    Graphics g = e.Graphics;
    //Configuration of the pencil
    Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 5);
    //Draw
    g.DrawLine(greenPen, 10, 10, 220, 290);
    }
    }