0

enter image description here

I'm working on a floor planner tool. More or less a program for scraping blueprints together with customization available.

Say I figure out how to import the furniture into the program. How do I go about making objects draggable on the form?

  • Possible duplicate of [How to allow user to drag a dynamically created control at the location of his choice](https://stackoverflow.com/questions/3868941/how-to-allow-user-to-drag-a-dynamically-created-control-at-the-location-of-his-c) – John Apr 29 '19 at 21:00
  • What classes have you built to manage your objects? If you have any `List`, you can determine which object has been selected with `[Object].Contains([Point])`, where `[Point]` is the Mouse pointer location. When you have detecte the object selected, you move it using the Mouse position until the left Button is released. You'll have to add constraints to the area where the mouse can move. Simple example here: [Don't move the Labels outside a PictureBox](https://stackoverflow.com/a/53317811/7444103). – Jimi Apr 29 '19 at 21:05
  • Have a look [here](https://stackoverflow.com/questions/30254957/usercontrol-drag-drop-on-panel/30255695#30255695) - Create the controls with a doubleclick in the listbox then drag to the positions.. – TaW Apr 30 '19 at 06:27

1 Answers1

0

One way of doing it would be to add your objects and change their location based on the mouse's coordinates while moving. Doing some more adjustments and centering the location movement would make the change in location smoother. Here's an example (in C#):

For example, you have a class for an object.

public class Object {
    string _name;
    int sizeX;
    int sizeY;

    int locationX;
    int locationY;

    public Object(string name) {
         locationX = 0;
         locationY = 0;

         switch(name) {
            case "chair":
                _name = name;
                sizeX = 50; //Predefined width of the chair object
                sizeY = 70; //Predefined height of the chair object
            case ... //Continue with the process
         }
    }

    public void setLocation(int x, int y) {
        locationX = x;
        locationY = y;
    }
}

So, now you could go into the main form.

public void moveObject() {
   Point coordinates = GetMouseCoordinates(); //There are a lot of ways.
   chair.setLocation(coordinates.X, coordinates.Y);
}

For even smoother movement do this:

public void moveObject() {
   MouseCoordinates coordinates = GetMouseCoordinates(); //There are a lot of ways.
   chair.locationY = coordinates.Y + chair.sizeY / 2;
   chair.locationX = coordinates.X + chair.sizeX / 2;
}

This is literally the simplest way. There are a ton of ways to make draggable objects on WinForm. However, if you want to make something like AutoCAD, 2D Animators etc. you will need to create a coordinate/grid system, which is really easy. You just need to create classes and use the concept of Object Oriented Programming(OOP).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33