Wednesday, 18 February 2015

Draggable class for .net

Draggable

In some cases, it is handy to move controls on a form around by using your mouse. In this project, there is a helper class which does all the stuff needed to do this. Not only can a control be moved, but also its container.

Only one line of code is used to make a control movable:

Control.DoDraggable(true);

OR

Control.DoDraggable(false);



This code do the all hard work to drag and drop control from a location to other. One advantage of this is that the Draggable class has only static methods.


Most interesting thing in this class is, when you reposition a control on your form, your application will remember your setting next you start your application.


You can download this class from here.


 here

Using the code


To make draggable and change the position of single control:

Control.DoDraggable(true);



To make draggable and change the position of all controls on form control:



                foreach (Control x in this.Controls)
                {
                    x.DoDraggable(true);
                    if (x.Controls.Count > 0)
                    {
                        foreach (Control c in x.Controls)
                        {
                            c.DoDraggable(true);
                        }
                    }
                }



To save the current control position:


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.SaveState();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.GetState();
        }





No comments:

Post a Comment