Click here to Skip to main content
15,897,334 members
Home / Discussions / C#
   

C#

 
GeneralRe: Can classes be nested Pin
Ralf Meier7-Mar-19 0:08
mveRalf Meier7-Mar-19 0:08 
GeneralRe: Can classes be nested Pin
Brian_TheLion7-Mar-19 10:53
Brian_TheLion7-Mar-19 10:53 
GeneralRe: Can classes be nested Pin
Dave Kreskowiak7-Mar-19 11:50
mveDave Kreskowiak7-Mar-19 11:50 
GeneralRe: Can classes be nested Pin
BillWoodruff8-Mar-19 17:03
professionalBillWoodruff8-Mar-19 17:03 
GeneralRe: Can classes be nested Pin
Brian_TheLion9-Mar-19 18:50
Brian_TheLion9-Mar-19 18:50 
AnswerRe: Can classes be nested Pin
Richard Deeming6-Mar-19 21:46
mveRichard Deeming6-Mar-19 21:46 
QuestionCRTP in c#/.NET? Pin
pr1mem0ver6-Mar-19 13:19
pr1mem0ver6-Mar-19 13:19 
AnswerRe: CRTP in c#/.NET? Pin
BillWoodruff6-Mar-19 21:15
professionalBillWoodruff6-Mar-19 21:15 
I will be posting this as a Tip/Trick or Article here in the future with comments and explanations, but, I'm in a rush right now, and don't have time to revise/comment my extended version of this, but, this should give you some ideas.

In a Form Load eventhandler, make some Controls movable like this:

button1.SetMovable();
textBox1.SetMovable();
panel1.SetMovable();

The code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace YOURNAMESPACE
{
    public static class ControlMovableExtensions
    {
        private static Control currentControl = null;

        private static Control currentContainer= null;

        private static List<Control> MovableControls = new List<Control>();

        private static Rectangle ClippingRect;

        private static Rectangle ContainerRect;

        private static Point lastGoodLocation;

        private static Point nextLocation;

        private static int mdx, mdy;

        private static bool IsMouseUp = true;

        // experiment with these settings
        private static bool UseClipping = false;
        private static bool UseCapture = false;

        public static void SetMovable(this Control control)
        {
            if (MovableControls.Contains(control))
            {
                throw new ArgumentException("control already movable");
            }

            MovableControls.Add(control);

            control.MouseDown += ControlOnMouseDown;
            control.MouseUp += ControlOnMouseUp;
        }

        public static void RemoveMovable(this Control control)
        {
            if (! MovableControls.Contains(control))
            {
                throw new ArgumentException("control is not movable");
            }

            control.MouseDown -= ControlOnMouseDown;
            control.MouseUp -= ControlOnMouseUp;
        }

        private static void ControlOnMouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseUp) return;

            nextLocation =  new Point(currentControl.Left + e.X - mdx, currentControl.Top + e.Y - mdy);

            if (ContainerRect.Contains(new Rectangle(nextLocation, currentControl.Size)))
            {
                lastGoodLocation = nextLocation;
            }

            currentControl.Location = lastGoodLocation;
        }

        private static void ControlOnMouseUp(object sender, MouseEventArgs e)
        {
            IsMouseUp = true;

            currentControl.MouseMove -= ControlOnMouseMove;

            if (UseClipping) Cursor.Clip = Rectangle.Empty;

            if (UseCapture) currentControl.Capture = false;

            currentControl = null;
        }

        private static void ControlOnMouseDown(object sender, MouseEventArgs e)
        {
            currentControl = sender as Control;

            if (currentControl == null)
            {
                throw new ArgumentException("control is null");
            }

            currentContainer = currentControl.Parent;

            ContainerRect = currentContainer.ClientRectangle;

            lastGoodLocation = currentControl.Location;

            mdx = e.X;
            mdy = e.Y;

            if (UseClipping)
            {
                ClippingRect = currentContainer.RectangleToScreen(currentContainer.ClientRectangle);
                Cursor.Clip = ClippingRect;
            }

            if (UseCapture) currentControl.Capture  = true;

            currentControl.MouseMove += ControlOnMouseMove;

            IsMouseUp = false;
        }
    }
}

«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot


modified 7-Mar-19 7:14am.

GeneralRe: CRTP in c#/.NET? Pin
pr1mem0ver7-Mar-19 5:09
pr1mem0ver7-Mar-19 5:09 
GeneralRe: CRTP in c#/.NET? Pin
BillWoodruff7-Mar-19 7:00
professionalBillWoodruff7-Mar-19 7:00 
AnswerRe: CRTP in c#/.NET? Pin
Gerry Schmitz7-Mar-19 7:26
mveGerry Schmitz7-Mar-19 7:26 
GeneralRe: CRTP in c#/.NET? Pin
BillWoodruff8-Mar-19 17:16
professionalBillWoodruff8-Mar-19 17:16 
GeneralRe: CRTP in c#/.NET? Pin
pr1mem0ver11-Mar-19 16:55
pr1mem0ver11-Mar-19 16:55 
QuestionOnly Numeric Textbox ( Back Space is working ) Pin
Bayram Demirci6-Mar-19 1:31
Bayram Demirci6-Mar-19 1:31 
AnswerRe: Only numeric ( Back Space is working also ) Pin
OriginalGriff6-Mar-19 1:58
mveOriginalGriff6-Mar-19 1:58 
GeneralRe: Only numeric ( Back Space is working also ) Pin
Bayram Demirci6-Mar-19 2:32
Bayram Demirci6-Mar-19 2:32 
GeneralRe: Only numeric ( Back Space is working also ) Pin
OriginalGriff6-Mar-19 2:47
mveOriginalGriff6-Mar-19 2:47 
GeneralRe: Only numeric ( Back Space is working also ) Pin
pr1mem0ver6-Mar-19 13:29
pr1mem0ver6-Mar-19 13:29 
GeneralRe: Only numeric ( Back Space is working also ) Pin
Eddy Vluggen7-Mar-19 1:03
professionalEddy Vluggen7-Mar-19 1:03 
GeneralRe: Only numeric ( Back Space is working also ) Pin
pr1mem0ver7-Mar-19 4:54
pr1mem0ver7-Mar-19 4:54 
GeneralRe: Only numeric ( Back Space is working also ) Pin
Eddy Vluggen7-Mar-19 9:51
professionalEddy Vluggen7-Mar-19 9:51 
GeneralRe: Only numeric ( Back Space is working also ) Pin
BillWoodruff8-Mar-19 23:20
professionalBillWoodruff8-Mar-19 23:20 
AnswerRe: Only numeric ( Back Space is working also ) Pin
Dave Kreskowiak6-Mar-19 2:23
mveDave Kreskowiak6-Mar-19 2:23 
GeneralRe: Only numeric ( Back Space is working also ) Pin
Bayram Demirci6-Mar-19 2:56
Bayram Demirci6-Mar-19 2:56 
GeneralRe: Only numeric ( Back Space is working also ) Pin
Dave Kreskowiak6-Mar-19 5:49
mveDave Kreskowiak6-Mar-19 5:49 

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.