Draw a picturebox, drawing lines and joining points

1

I have a form with winForms and c # where I have two pictureboxes and some textboxes that fill in these values generate a graphic joining the points x1 - > x2 ... then x2 - > x3 and so on. The code I have is this.

In the textChanged event of the textbox I have this code

private void modificarDatos_Audio_OD_TextChanged(object sender, EventArgs e)
    {
           botonPintarGrafica_Click(null, null);
    }

And in the function I have the following

 private void botonPintarGrafica_Click(object sender, EventArgs e)
    {
        int x = 0, y = 0, multiplicador = 42;
        int x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0;
        int y1 = 0, y2 = 0, y3 = 0, y4 = 0, y5 = 0;

        List<DibujoRXModel> listaPuntos = new List<DibujoRXModel>();
        DibujoRXModel tonoOD_A = new DibujoRXModel();


        tonoOD_A.pos1 = Convert.ToInt32(DataUtils.IntNullToZero(pos1a.Text));
        tonoOD_A.pos2 = Convert.ToInt32(DataUtils.IntNullToZero(pos2a.Text));
        tonoOD_A.pos3 = Convert.ToInt32(DataUtils.IntNullToZero(pos3a.Text));
        listaPuntos.Add(tonoOD_A);



        Graphics grafico1;
        grafico1 = pictureBox1.CreateGraphics();
        grafico1.Clear(Color.LightGreen);



        Pen lapiz = new Pen(Color.Red, 2);

        foreach (DibujoRXModel tono in listaPuntos)
        {
            if (tono.pos1 >= 0)
            {
                x1 = 1 * multiplicador;
                y1 = tono.pos1;
                grafico1.DrawRectangle(lapiz, new Rectangle(x1, y1, 5, 5));
            }

            if (tono.pos2 >= 0)
            {
                x2 = 2 * multiplicador;
                y2 = tono.pos2;
                grafico1.DrawRectangle(lapiz, new Rectangle(x2, y2, 5, 5));
            }

            if (tono.pos3 >= 0)
            {
                x3 = 3 * multiplicador;
                y3 = tono.pos3;
                grafico1.DrawRectangle(lapiz, new Rectangle(x3, y3, 5, 5));
            }

        }


        grafico1.DrawLine(lapiz, x1, y1, x2, y2);
        grafico1.DrawLine(lapiz, x2, y2, x3, y3);        

    }

This what it does is that as I change the values in the textbox is repainting the graph, the problem I have is that if I move with the mouse between the textbox nothing happens, but if I move with the tabulator between textbox the graphic "disappears" and I do not see the problem where it comes from.

    
asked by ilernet 29.05.2017 в 10:08
source

1 answer

1

The problem you have is that you are drawing in the PictureBox but at the moment in which the control must be redrawn, everything you have done is deleted. To check it, note that once you have the graph on screen, if you minimize the form and re-maximize it, your PictureBox will also be deleted.

There are two solutions to the problem. On the one hand, instead of painting on the control directly, you can paint on an image and assign it to PictureBox .

The second solution is to draw your graph in the event Paint of PictureBox . That way, every time the control must redraw, it will also redraw your graphic. It would be something like this:

  • Event TextChanged of your textboxes:

    private void modificarDatos_Audio_OD_TextChanged(object sender, EventArgs e)
    {
         botonPintarGrafica_Click(null, null);
    }
    
  • Event Click of the button

    private void botonPintarGrafica_Click(object sender, EventArgs e)
    {
        pictureBox1.Invalidate(); //esto hace que se llame al evento Paint del PictureBox  
    }
    
  • Event Paint of the PictureBox

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        int x = 0, y = 0, multiplicador = 42;
        int x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0;
        int y1 = 0, y2 = 0, y3 = 0, y4 = 0, y5 = 0;
    
        List<DibujoRXModel> listaPuntos = new List<DibujoRXModel>();
        DibujoRXModel tonoOD_A = new DibujoRXModel();
    
        tonoOD_A.pos1 = Convert.ToInt32(pos1a.Text.Trim().Length == 0 ? "0" : pos1a.Text);
        tonoOD_A.pos2 = Convert.ToInt32(pos2a.Text.Trim().Length == 0 ? "0" : pos2a.Text);
        tonoOD_A.pos3 = Convert.ToInt32(pos3a.Text.Trim().Length == 0 ? "0" : pos3a.Text);
        listaPuntos.Add(tonoOD_A);
    
        Graphics grafico1;
        grafico1 = e.Graphics; //OJO:obtenemos el grafico de los argumentos del evento
        grafico1.Clear(Color.LightGreen);
    
        Pen lapiz = new Pen(Color.Red, 2);
    
        foreach (DibujoRXModel tono in listaPuntos)
        {
            if (tono.pos1 >= 0)
            {
                x1 = 1 * multiplicador;
                y1 = tono.pos1;
                grafico1.DrawRectangle(lapiz, new Rectangle(x1, y1, 5, 5));
            }
    
            if (tono.pos2 >= 0)
            {
                x2 = 2 * multiplicador;
                y2 = tono.pos2;
                grafico1.DrawRectangle(lapiz, new Rectangle(x2, y2, 5, 5));
            }
    
            if (tono.pos3 >= 0)
            {
                x3 = 3 * multiplicador;
                y3 = tono.pos3;
                grafico1.DrawRectangle(lapiz, new Rectangle(x3, y3, 5, 5));
            }
    
        }
    
    
        grafico1.DrawLine(lapiz, x1, y1, x2, y2);
        grafico1.DrawLine(lapiz, x2, y2, x3, y3);
    }
    
answered by 29.05.2017 / 11:10
source