Want to make a Selection Rubber Band Line


Selection Rubber Band Line like in Windows

You have noticed that when selecting some multiple items on desktop you see a
rubber band like selection rectangle.The following is the code to make just
that type of selection band in Windows Form.


//Form's Mouse Down Event
private void form_MouseDown(object sender, MouseEventArgs e)
{
first = new Point(e.X, e.Y);
}

Now to make a Form's MouseMove Event to draw the rubber band selection.
TO give the Dash effect a pen class is used.

//Form's Move Event
private void form_MouseMove(object sender, MouseEventArgs e)
{
Pen p = new Pen(Brushes.Black, 2);
//Pen Dash Style to Dot
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
g = this.CreateGraphics();
g.Clear(this.BackColor);
Point second = new Point(e.X,e.Y);
if (e.Button == MouseButtons.Left)
{
//Vertical and Horizontal Lines
g.DrawLine(p,first.X,first.Y,second.X,first.Y);
g.DrawLine(p,second.X,second.Y,first.X,second.Y);
g.DrawLine(p,first.X,first.Y,first.X,second.Y);
g.DrawLine(p, second.X,second.Y, second.X, first.Y);
}
}

That,s all.
Thanks.
If any problem occurs plz contact softwarenik@yahoo.in


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: