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

How To Make a Simple Draggable-Resizable Text Box

Rate me:
Please Sign up or sign in to vote.
4.79/5 (8 votes)
4 Jul 2014CPOL2 min read 27.5K   643   11   7
A simple draggable-resizable TextBox control inherited from RichTextBox

Introduction

Few months ago, when I needed to make my textbox draggable and resizable, I searched on the Internet and tried to program it.

This is a simplified code from my Chart-Drawing project. The code just shows a beginner how to move a control by mouse drag and how to resize it at run-time.

About "how to set transparency on textbox background" and "flicker-free moving", I will write in another article.

Feel free to comment. Share to be shared !!!

EDIT: In the last version, I forgot to attach the source, updated.

Background

Click to Add TextBox Button to toggle TextBox Mode, then click to the Panel below, new TextBox will be added to the Panel. The Select Mode Button will toggle Select Mode, add this Mode, you cannot click and add TextBox to Panel.

Using the Code

The main idea of "move by mouse dragging" is using 3 events : MouseDown, MouseMove and MouseUp.

  • MouseDown: Set the Flag to TRUE and save clicked point (Start Point)
  • MouseMove: if Flag is TRUE, change the Location properties of Control (using the distance of Start Point and current mouse location)
  • MouseUp: Set the Flag to FALSE
C++
public void SetMoveFunction()
        {
            bool isDragging = false;
            Point StartPoint = Point.Empty;
            this.MouseDown += delegate(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    isDragging = true;
                    StartPoint = new Point(e.X, e.Y);
                    this.Capture = true;
                }
            };
            this.MouseUp += delegate(object sender, MouseEventArgs e)
            {
                this.Cursor = Cursors.Arrow;
                isDragging = false;
                this.Capture = false;
            };
            this.MouseMove += delegate(object sender, MouseEventArgs e)
            {
                if (isDragging)
                {
                    this.Cursor = Cursors.NoMove2D;
                    this.Left = Math.Max(0, e.X + this.Left - StartPoint.X);
                    this.Top = Math.Max(0, e.Y + this.Top - StartPoint.Y);
                }
            };
        }

As you see, I wrote a method which called delegate of control's MouseDown-MouseUp-MouseMove events. So, in the Constructor class, just call SetMoveFunction() and your control is draggable. :D

C++
this.Capture = true;

This is a trick! Control.Capture property receives mouse input whether or not the cursor is within control's borders. It will make the dragging operation smoother. But remember to set it back to FALSE when dragging is over.

Next is a resize function. The main idea is add 8 square label to control (Handles), the handles location will be: east, west, south, north, northeast, northwest, southeast, southwest.

When the user "moves" (by dragging) the handle, it will move and the size of control will be set based on handle location. These methods are written at Resize Methods region in the source. Please download the source and feel free to comment if you have any questions.

License

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


Written By
Software Developer (Junior)
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow would I make the font resize when dragging a handle to increase the textbox size? Pin
RAGolko30-Mar-15 14:07
RAGolko30-Mar-15 14:07 
Questionsource files not downloadable Pin
Sid_Joshi4-Jul-14 21:41
professionalSid_Joshi4-Jul-14 21:41 
AnswerRe: source files not downloadable Pin
Nguyen.H.H.Dang4-Jul-14 23:20
professionalNguyen.H.H.Dang4-Jul-14 23:20 
GeneralRe: source files not downloadable Pin
Sid_Joshi4-Jul-14 23:32
professionalSid_Joshi4-Jul-14 23:32 
Thank you.....
QuestionSource files cannot be downloadable Pin
fredatcodeproject4-Jul-14 11:50
professionalfredatcodeproject4-Jul-14 11:50 
AnswerRe: Source files cannot be downloadable Pin
Nguyen.H.H.Dang4-Jul-14 23:20
professionalNguyen.H.H.Dang4-Jul-14 23:20 
GeneralRe: Source files cannot be downloadable Pin
fredatcodeproject4-Jul-14 23:34
professionalfredatcodeproject4-Jul-14 23:34 

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.