Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / C#
Tip/Trick

C# Selector Tool

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
24 Jan 2013CPOL1 min read 13.5K   543   3   3
A simple selector tool in C#.

Introduction

While working on one of my latest projects, I found myself in need of a simple region selection tool. Pressed with more important functions that my app should provide, I tried searching around the web for a code, just like many times before. After a while, it was clear to me that I’ll have to do the job myself and here it is! You can take the code and implement it however you prefer.

Image 1

Using the code

It doesn’t really require a lot of code, just some creative thinking to get you on the right track. To give you a basic idea of what it’s all about, here’s a code that allows you to define a selection component into a simple form.

C#
Selection selection = null;
public Form1()
{
    InitializeComponent();
    selection = new Selection(this);
}

The selection is then implemented in a picture box class with defined methods events regarding specific mouse movements.

C#
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    selection.DrawSelection(e.Graphics);
    pictureBox1.Refresh();
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    selection.mouseDown(e);
    pictureBox1.Refresh();

}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    selection.mouseUp(e);
    pictureBox1.Refresh();
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    selection.mouseMoved(e);
    pictureBox1.Refresh();
}

Of course, this is not all the work you need to do. It’s not all that simple, but it explains the logic behind solving the problem. The form above above just calls for a Selection.cs file which handles the rest. If you believe this can help you, the full code and the demo are available to the rest of the community here at CodeProject. Play around with it, use it in your apps, or just show off in front of your friends. A lot of the guys here have helped me on more than one occasion, so this is my way of saying: “Thank you!”

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionI have a tabpage .tabpage content is a form,if i want to make a form that user can resize it like visual studio; Pin
Member 1431579018-Apr-19 21:55
Member 1431579018-Apr-19 21:55 
Questionhow can i select button by this method Pin
Member 1431579018-Apr-19 21:50
Member 1431579018-Apr-19 21:50 
Praisethanx Pin
Member 120369289-Nov-15 23:36
Member 120369289-Nov-15 23:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.