vb.net check if two panels are overlapping - vb.net

I have a panel that I can move around with a drag/drop system. I would like it to snap to a certain position when it is drug on top of another panel. How would I check if the panels are overlapping?

To simply check if panels are overlapping, use Rectangle.IntersectsWith.
If you need to know how exactly they are overlapping, use Rectangle.Intersect.
In both cases you need to be using Panel.Bounds as arguments, so either
Dim b As Boolean = panel1.Bounds.IntersectsWith(panel2.Bounds)
or
Dim r As Rectangle = Rectangle.Intersect(panel1.Bounds, panel2.Bounds)
Then check if r is large enough for drag and drop, for example.

In order to check if panels are overlapping, you can check if panel bounds overlap:
Dim arePanelsOverlapping = panel1.Bounds.IntersectsWith(panel2.Bounds)
To snap to the position, you can also make use of Top, Left, Bottom and Right properties.

Related

Colors[4] not showing color selection on Front Panel

I am trying to set the color of an indicator to different colors based on different values, like 1 = red, 2 = blue etc. Using guidance from a Youtube video (accessible using this link: https://www.youtube.com/watch?v=czUmPQmKmGU), I have created a Colors[4] control for the indicator I have after changing it to the "write" function.
The Problem on the front panel is that I am getting a control with numbers instead of a color box where I can select the colors to show based on the value. This was the control I got instead.
This is the control I am trying to achieve (below):
Is there any way that I can get the color box on my control instead of the number controls? I am not sure if it can be changed through a control on the front panel or something but what I have tried so far keeps leading me back to this problem.
Any advice is much appreciated
A color box control is just a U32 number (three bytes for RGB and one which is always 0), which is why that's what you get.
There is a right click plugin which adds a replace with color box option directly to the right click menu of unsigned 32 bit numbers, but I don't remember if it ships with LV or not.
If you don't have that, you can always just right click the indicator inside the cluster, select replace and navigate the palettes to find the color box. You can also copy a color box and then select the indicator and paste, which replaces the selected control.
The color[4] is actually an array of 4 colors (UInt32 as Yair said), that define 2 color gradients, one for the 'Off' state, and the other for the 'On' State of the control.
If you want to set the control's color, you will have to define all 4 of them.

How to identify which monitor a winform is displayed on, in a multi monitor setup?

I have found a lot of answers researching this, but non address my question.
Assume that user has 2 monitors, say a laptop screen with 1600×1200 res, and external monitor of 2560×1440 res. Regardless how the 2 displays are set up, if a client moves the main-form of the program to the external monitor, I would like it to report that its on 2560×1440 res. When the main-form is moved on to the laptop it should report the 1600x1200.
Is this possible? I know how to report the res, I just do not know how to identify which monitor the main-form is sitting on.
You need to first identify what you mean by the form being on a screen, because it's possible for a single form to span multiple screens. You're going to be using the Screen class regardless, but the calculations will be different. The simplest option would be to use the Location property, e.g.
For Each scrn In Screen.AllScreens
If scrn.Bounds.Contains(Location) Then
MessageBox.Show($"Resolution: {scrn.Bounds.Width} x {scrn.Bounds.Height}")
Exit For
End If
Next
Another option would be to use the screen that contains the largest proportion of the form, e.g.
Dim maxArea = 0
Dim resolution = Size.Empty
For Each scrn In Screen.AllScreens
Dim intersection = Rectangle.Intersect(scrn.Bounds, Bounds)
Dim area = intersection.Width * intersection.Height
If area > maxArea Then
maxArea = area
resolution = scrn.Bounds.Size
End If
Next
MessageBox.Show($"Resolution: {resolution.Width} x {resolution.Height}")
Note that this code will display the resolution of the first encountered if the equal parts of the form are on multiple screens.
There may be other options too, although these seem the most likely.
EDIT:
It's also worth noting that the first code won't work for a maximised form because the actual Location value will be outside the bounds of the screen it's on, so you'd need a bit of jiggery-pokery to handle that. The second code will handle that without issue.

Do not show partially visible rows in datagridview

I try to implement the following in winform vb net project (I see this work in an app written in delphi).
I wish to hide or set visibility to false of the bottom row that partially visible in dgv that is docked to fill.
I tried to implement something like this:
DataGridView1.Rows(DataGridView1.DisplayedRowCount(true) - 1).Visible = False
I think it should be called during DataBindingComplete and Resize/scroll events, but it doesn't work.
Do you have any ideas / solutions?
So what I use on one of my datagridviews is:
Dim ind As Integer = 0
ind = DataGridView1.Rows.Count - 1
DataGridView1.Rows(ind).Visible = False
which hides the last displayed row of the datagridview.
You requirement sounds somewhat odd. Your comment ”I wish to hide or set visibility to false of the bottom row that partially visible in dgv that is docked to fill.”... I am curious how you would know this is the last row? Is it not possible that there are more rows below the last one visible? If the scroll bars are available you should see the vertical one if rows go beyond its bounding box. If one of the rows is cut in half by the bounding box and there is more than 1 row below this row, then making invisible/hiding/deleting that row will simply move the next one up.
Since the DataGridView is docked you may have to resize the rows manually if you do not want the rows to be split by the bounding box. Another possible solution is to use the DataGridViews AutoSizeRowsMode Like below.
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
This will set the rows so that a row will not be chopped if it is outside the bottom of the bounding box. The rows will auto size to fit evenly There are seven (7) AutoSizeRowsMode options and I am guessing one of them may do what you are looking for. I am guessing DisplayedCells may work for what you describe. If the grid is re-sized often, you may have to implement this row resizing. Hope this helps.

Movable/Draggable RectangleAnnotation to Select a Range

I'm using OxyPlot to show a rectangle annotation to the user, so it can select a range. However , I want to make this RectangleAnnotation Movable across the X-axis. Besides that, the user should be able to change the range by dragging the two sides of the rectangle (MinimumX and MaximumX).
Is this even possible? Any ideas or starter points for me to do this?

is it possible to anchor a control to another control?

Is it possible to anchor a control to another control?
Lets say i want my Button1 to keep its 4px distance from Textbox1 control without
using FlowLayoutTable control?
"Why would be this advantageous?" - one could ask
Well let's say you have a SplitContainer with a vertical splitter and you have Textboxes
in SplitContainer.Panel1 which are anchored to the left and right but their maximum size's
width is smaller than you allow to the SplitContainer.Panel1's width to have (maybe
because you want text to show up there or because additional padding or whatever,you name it)
Now let's say you also have a button next to Textbox1 and you dont want Textbox1 to be
overlapped by the Button1 because its extends to far.
If i want to have my textbox fill the SplitContainer.Panel1 in a fashion that it leaves space for
Button1 control while still both of them are anchored to the right how would i do it?
I extensively use TableLayoutPanels and FlowLayoutPanels to accomplish this. For you specific circumstance I would use a TableLayoutPanel with three columns and a row for each TextBox.
Column 1: Auto-width, contains Labels all with AutoSize = True.
Column 2: 100% width, contains TextBoxes all with Anchor = Left, Right.
Column 3: Auto-width, contains the Button in the appropriate row.
Next, I set all text boxes, except for the one next to the button, ColumnSpan = 2. Then just put the TableLayoutPanel in the SplitPanel and set Dock = Fill.
it will be a sequence in live which should be flow out from left and keep working lets the right side should be layout.
List item safty cares should be provided.
List item all things that use in this method should be provided and be check;