How to drag a control around a panel? - c#

Say I had a control like a Picture Box on my Windows Form and I wanted the user to be able to drag it around the panel like a canvas freely to any position they want. What would be the best way to do this?
Afterwards, I want to be able to retrieve the location of the image on the panel. Obviously I'd need the MouseDown event, and my first idea would be to set the location of the control equal to the cursors location, but this glitchy and not smooth.

This post might help as well HERE...
and to display the output, simply add a label or textbox and display the coordinates- something like:
textbox1.Text = pictureBox1.Location.ToString();
or take the coordinates by:
pictureBox.Location (which is a point) -or-
pictureBox.Location.X // pictureBox.Location.Y (which are integers)

Related

Dynamically Surrounding controls C#

In my code I have some pictureboxes that are clicked onto a panel by the user. Wherever the user clicks a picture box is placed. The location is stored in a database so that it can be called back later.
When a user clicks a picturebox it changes the backcolor property to red to give the impression of it being highlighted.
My issue is I want to add a groupbox (or another method of surrounding) around the highlighted boxes. So a user clicks say 4 picture boxes and clicks "Surround" button and it draws a groupbox around those 4 pb's. But I am at a loss since I don't know how to get the location of the outer pictureboxes (the ones that the group needs to surround) since they are all done on the fly?
Any advice would be great.
You probably have the list of all pictureboxes in your application somewhere (if you place them inside a container, that would be Children property). I suggest you simple foreach through all picture boxes and find min/max coordinates, and from there you can easily get the coordinates for the surrounding box. You might be able to do all that using one LINQ query.

Move controls between multiple containers in a single form

The questions says it all.
How can I move a control, say a PictureBox between multiple panels, or betwween a panel and a flow layout panel.
I'm aware I can drag and drop controls between multiple panels and such, however this is does not make the control visually movable between the containers. The mouse only changes to a diferent cursor and after you drag to the other control and release the mouse button the control appears on the other container. I require the control to be visually movable.
Can someone provide a simple example, so I can extract the idea to apply to my situation.
NOTE: Runtime of course.
Assuming you need it runtime:
You can save the control as bitmap using Control.SaveToBitmap method
Create cursor from image.
Set the current cursor which we created from control.
Once drag and drop completed reset the cursor.
Let's take a simple example of dragging a button around.
Suppose you have two types of container controls:
1) an X-Y layout
2) a flow layout (assume left to right)
When you click to drag the button, record the x-offset and y-offset from the click to the top left corner of the control. As well, record the index of the control within the Controls collection.
As the mouse moves, first check if the mouse has changed container controls.
If so, then remove the button from its current parent and add it to the new parent.
If the button is added to a flow control, then you need to calculate the new index. To do this, calculate the distance from the mouse to the closest edge of a bounding box of all other controls. Then if the mouse is left of the center of that control, insert to that control's index minus 1, otherwise insert to the right of that control (index + 1).
If the button is added to an X-Y layout, then the index doesn't matter that much. You can simple just set the button's location relative to the mouse plus the x-offset, and y-offset.
As the mouse is dragging, you will need to force the controls to refresh. I think calling Invalidate() on the container control should be sufficient.
This should give you the basic idea that you could use to start coding something.

move button along with picturebox vb/c#

I am working on a mapped DVR/cctv UI.
I made it myself, so I did not use google's API. I just cut off big part of the map where I need it.
so, if I do have a really big map, then it won't fit in my pc's resolution, I haven't found a code to move the picture inside the pictureBox, but what I did is to move the pictureBox inside a panel. then it looked like a map, with boundaries. :)
now, I want to be able to save/attach this button to the picture.. so whenever and wherever I move the pictureBox, button gets along with it. even if goes outside the form, but when I drag it back, it appears to where it was let's say, attached just imagine the button like googlemap's marker. that's what I wanted to happen.
its like I am building my own offline google map..
if you have queries, feel free to ask. TIA
Simply add your button as a child of your picturebox:
button1.Parent = pictureBox1;
//or
pictureBox1.Controls.Add(button1);
Then you can use the Location property of your button to set it accordingly, that location is relative to your pictureBox, not your form.
If you want to keep the design location of your button, you can try this code:
Point loc = pictureBox1.PointToClient(button1.PointToScreen(Point.Empty));
button1.Parent = pictureBox1;
button1.Location = loc;

C# Windows Forms, painting combobox borders

We need to show our disabled comboboxes as an image. The image has the same height as the standard combobox, but for some reason it cannot override the borders of the combobox.
Instead, it end up looking like this:
We would like them look like this image, i.e. that the image is shown on top of everything - including the combobox borders:
Any ideas?
Thanks.
First of all, what you are trying sounds really dirty - the best way would be, if your ComboBox would just look like your image as soon as you disable it!
If there is really no other way:
Create a PictureBox in front of your ComboBox. Set the image as the PictureBox's image, make it Visible whenever you want to "disable" your ComboBox.
But again, using controls to simulate behaviour you would expect to be part of another control is dirty.
Get the location of you textbox, set it invisible, then a the same location place an imagebox

C# WinForms Move elements between panels

I need to create a form with two panels:
1. Destination
2. Source
On the source panel there will be picture boxes. I need to be able to move it from source to point at destination panel with mouse.
I have a problem connected with different coordinates of the panels.
Please, help with advice or an idea what to do.
Moving those controls requires changing their Parent property. That's not easy to do, there is no good time to do this while the user is dragging with the mouse. You'll also get the effect of the panel clipping the control, you cannot display it on both with half of the control on one and the other half on the other panel. And yes, you have to change the Location property of the control when you change the parent or it will jump.
Punt the problem, don't use two panels. It only has to look like a panel, easily done by drawing one in the form's Paint method (or OnPaint override, better). Use e.Graphics.DrawRectangle or FillRectangle.

Categories

Resources