Dynamically Surrounding controls C# - 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.

Related

Multiple listview objects at the same position

I have a chart with multiple chart areas and I want to have a listview where the user checks different signals, that are being plotted in the areas.
Since I want some signals to be displayed in more than one chart areas, I need the listview to reset every time the user selects a different chart Area.
To achieve this I think it is a good idea to create a new listview every time the user selects another chart area and bring the new to front.
My question is , how can I create multiple listviews at the exact same location, and also if someone has a better idea to this.
As you know, you can define the listview in code and do all the styling by hand or you can create a customControl for that.
But then, when the load event of your main form is fired, you create all the listview/customListViewObjects that you need and place them in the same location with the same size by setting their Location and Size property. Finally, you can use BrintToFront() on the required listview when an area is selected.
There are some other ways, but they aren't beautiful in winforms.
A) you can create a tab control with N tabs, then place a listview in each tab and set the tabControl style to the tab header/title is not shown. But as far as I remember, the tabControl will add a border frame that your cannot get rid of.
B) If you don't have many areas and its number is not going to change, you can create the listviews manually in your form editor, and place them wherever in your form (with the appropriate parent, of course). Then in your load event, you can set their location and size properties to the one you need. This is kind of ugly and you may see the controls moving when the form is loaded if you are not careful.
On the other hand, you can just have one listview and then, when the area is clicked you can reload all its child controls for the different signals.

Save image depending on which panel is topmost

I am making an interface where on the left is a column of menu buttons.
A couple of the buttons bring different graphs (each on it's own panel) to the front to view, using the BringToFront() method. I also have a "Save image" button where you can save the graph.
How do I setup the button to save whichever panel is in the front? Is it better practice to use "Enable/Disable" instead of BringToFront and then save whichever panel is enabled?
Previously, I had all these panels within a TabControl, and I had the button save the graph on whichever tab was selected, but I got rid of the tabs because I did not like the way it looked.
Thanks in advance.
The most easiest way is to store the actual active image in a variable, changing it's value by clicking the buttons. If you store the image itself or just the index or whatever to determine which image is actually infront is on your choice.
Another aproach could be, show and hide your panels, then you can iterate over your panel list and check which panel is not hidden.

How to drag a control around a panel?

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)

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.

How to print a WinForm which contains a scrollDown panel?

I have a panel that displays database information in labels (a lot of labels).
Now, the labels extend the size of the panel and you need to scroll down to see them all. Everything is working, and you can see the labels if you scroll down.
How do I print the form and the panel with all its content? I tried to capture the screen, but it's bigger than the screen.
Is there a function like panel.printAllContent().IntoAnA4Paper;?
I don't think that's the right way. As you said you display information taken from a database so you already have your data model. You should print the data, not the interface.
But, if you want to print the UI, I think you should first put the interface inside a Bitmap, and then print the Bitmap.
Here's a link with an example: http://www.codeproject.com/Answers/200887/How-to-print-part-of-the-window-form-in-c-window-a#answer4

Categories

Resources