Question

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = panel1.CreateGraphics();
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        using (Pen p = new Pen(Color.White))
        {

            for (int i = panel1.Width; i >= 0; i -= 40)
            {
                g.DrawLine(p, i, 0, i, panel1.Height);

            }
        }

        using (Pen p = new Pen(Color.White))
        {
            // Draw all visible, vertical gridlines                     
            for (int i = panel1.Height; i >= 0; i -= 40)
            {
                g.DrawLine(p, 0, i, panel1.Width, i);

            }
        }

        Pen pen = new Pen(Color.Red, 6);
        g.DrawLine(pen, new Point(50,80), new Point(8050,  80));

    }

Output: enter image description here

its fine for me. but i want when i click and drag mouse right to left example(10px) the panel show me the next 10px line form right(that is hidden ) and hide 10px from left.

How can I do this... Please someone help me.

Was it helpful?

Solution

DonBoinott is quite right. You probably should create a line object class to model the behaviour you want. Not knowing how complex it will eventually grow, we can only advise to plan for growth. Here is a simple solution for multiple horizontal lines:

 //..
 // draw all my lines
   foreach(myLine aLine in myLines)
       using (Pen p = new Pen(aLine.LineColor, aLine.Stroke))
       {
           p.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
           g.DrawLine(p, aLine.X, aLine.Y, aLine.X + aLine.Length, aLine.Y);
       }



}

// keeping state of the mouse movement..
int lastX = 0; int currentX = 0; myLine hitLine = null;
// a collection of lines
List<myLine> myLines;
// a simple horizontal line class
public class myLine
{
    private int x;
    public int X
    {
        get { return x; }
        set { x = value; Rect = new Rectangle(x, Y-2, Length, Stroke+4); }
    }
    public int Y { get; set; }
    public int Length { get; set; }
    public int Stroke { get; set; }
    public Color LineColor { get; set; }
    public Rectangle Rect { get; set; }
    public string Name { get; set; }

    public myLine(int x, int y, int length, string name, Color color, int stroke)
    {
        X = x; Y = y; Length = length; Name = name; LineColor = color; Stroke = stroke;
        Rect = new Rectangle(x, Y - 2, Length, Stroke + 4);
    }
}

// create a few lines
private void initLines()
{
    myLines = new List<myLine>();
    myLines.Add(new myLine(100, 80, 400, "Tom", Color.Red, 6));
    myLines.Add(new myLine(100, 130, 400, "Jerry", Color.Orange, 8));
    myLines.Add(new myLine(100, 180, 400, "Barbera", Color.Blue, 10));
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
   // hittest, show line name in a panel (optional) 
   foreach (myLine aLine in myLines)
       if (aLine.Rect.Contains(e.Location)) { hitLine = aLine; label1.Text = hitLine.Name;  continue; }
   lastX = currentX;
   currentX = e.X;
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
   hitLine = null;
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
   if (e.Button == System.Windows.Forms.MouseButtons.Left)
   {
       lastX = currentX;
       currentX = e.X;
       if (hitLine != null)
       {
           hitLine.X += (currentX - lastX);
           // invalidate a bit more than really needed..
           panel1.Invalidate(new Rectangle(0, hitLine.Y - hitLine.Stroke, panel1.Width, hitLine.Y + hitLine.Stroke));
       }
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top