1
Answer

Problem in Drawing Rubberband lines

Ashokan Elumalai

Ashokan Elumalai

16y
5.3k
1
Hi all..

I have a problem in Drawing Rubber band lines

Consider the following:(Windows Application: C#)
I have two datagridview components in the form, one is left and another is right side.
there is some amount of space between these datagridview components .

I will click on the DataGridView cell and will drag a line from the left to the displayable column on the right DataGridView. Remember I cannot have two clicks.The line should be drawn to the mouse pointer as it moves around the screen (i.e. rubberband line).

The code is as follows:

private void Form1_Load(object sender, System.EventArgs e)
        {
          
            //Size size = this.Size;
            Size size = SystemInformation.PrimaryMonitorMaximizedWindowSize;
            bitmap = new Bitmap(size.Width, size.Height);
            gBm = Graphics.FromImage(bitmap);
            Color bckColor = this.BackColor;
            gBm.Clear(bckColor);
        }

     
        private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
           
                Point p = new Point(e.X, e.Y);
                x = p.X;
                y = p.Y;
                if (drag)
                {
                    Graphics gx = CreateGraphics();
                    gx.DrawImage(bitmap, 0, 0);
                    gx.Dispose();
                    Graphics g = CreateGraphics();
                    Pen pen = new Pen(Color.Blue);
                    g.DrawLine(pen, x0, y0, x, y);
                    xPrev = x;
                    yPrev = y;
                    g.Dispose();
                    pen.Dispose();
                }
           
       
        }

        private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Pen pen = new Pen(Color.Blue);
            gBm.DrawLine(pen, x0, y0, x, y);
            drag = false;
            pen.Dispose();
        }

        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics gx = CreateGraphics();
            gx.DrawImage(bitmap, 0, 0);
            gx.Dispose();
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            Point p = new Point(dtgXml.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Left + 100, dtgXml.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Top + dtgXml.Top + 8);
            x0 = dtgXml.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Left + 100;
            y0 = dtgXml.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Top + dtgXml.Top + 8;
            xPrev = dtgXml.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Left + 100;
            yPrev = dtgXml.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Top + dtgXml.Top + 8;
            drag = true;
        }


The problem is: When I click and drag over the form, the line doesn't appear. The line appears only for the following situation:
Click the cell and another click on the any area of the form, then the line appears.

How can I solve this?
Answers (1)