VB.NET Getting Value On Dynamic Control - vb.net

I am new to VB and run into some problems.
I have created sub routine that will automatically add a control to a panelcontrol each time i click on the button, so it can create as many as i want.
Here is the code for the subroutine.
Private Sub CreateControl()
'CREATE TEXTBOX ITEMNO
Dim i_Itemno As Integer = TextEditItemno.Length
ReDim Preserve TextEditItemno(i_Itemno)
TextEditItemno(i_Itemno) = New TextEdit
With TextEditItemno(i_Itemno)
.Name = "Txtitemno" & i_Itemno.ToString()
If TextEditItemno.Length < 2 Then
.SetBounds(0, 0, 32, 20)
Else
.Left = TextEditItemno(i_Itemno - 1).Left
.Top = TextEditItemno(i_Itemno - 1).Top + TextEditItemno(i_Itemno - 1).Height + 4
.Size = TextEditItemno(i_Itemno - 1).Size
End If
.Tag = i_Itemno
End With
AddHandler TextEditItemno(i_Itemno).TextChanged, AddressOf TextEditItemno_TextChanged
PanelControl5.Controls.Add(TextEditItemno(i_Itemno))
'CREATE TEXTBOX PRICE
Dim i_Price As Integer = TextEditPrice.Length
ReDim Preserve TextEditPrice((i_Price))
Dim PriceX As Int16 = LblHarga.Location.X
TextEditPrice(i_Price) = New TextEdit
With TextEditPrice(i_Price)
.Name = "Txtprice" & i_Price.ToString()
If TextEditSatuan.Length < 2 Then
.SetBounds(PriceX, 0, 70, 20)
Else
.Left = TextEditPrice(i_Price - 1).Left
.Top = TextEditPrice(i_Price - 1).Top + TextEditPrice(i_Price - 1).Height + 4
.Size = TextEditPrice(i_Price - 1).Size
End If
.Tag = i_Price
End With
AddHandler TextEditPrice(i_Price).TextChanged, AddressOf TextEditPrice_TextChanged
PanelControl5.Controls.Add(TextEditPrice(i_Price))
End Sub
And i call it in a button click.
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
CreateControl()
End Sub
Now what i am looking for is how to loop and get the value of those textboxes no matter how many textboxex i have create.
For i As Integer = 0 To TextEditItemno.Length - 1
' code to get the value of each textbox
Next
Thank you

This code goes in to your loop and gets the value of each textbox based on i.
Dim Text as String = TextEditItemno(i).Text
You may also be better served by using a List(of Textbox) rather than an array of textboxes. You don't need to worry about redimming the array, you can just do MyListOfTextboxes.Add(TheNewTextBox). You can still retrieve the value of each textbox the same way as the array.

Related

Random picturebox arrangement

I am new to visual basics and was wondering how to do the following program: I have 9 picture boxes and a button "arrange". For my program, I would like that all picture boxes come together like a puzzle randomly to make a square that has a width and height of three picture boxes. The square made would have all nine picture boxes in one and every time you click the button "arrange" the picture boxes would change to a random location within the square. So far, I have written so that all the picture boxes become the same size but i don't know how to make them come together in a square. Thanks in advance.
Public Class frm1
Dim Placement As Integer
Private Sub btnArrange_Click(sender As Object, e As EventArgs) Handles btnArrange.Click
picDeux.Size = picgris.Size
picTrois.Size = picgris.Size
picQuatre.Size = picgris.Size
picCinq.Size = picgris.Size
picSix.Size = picgris.Size
picSept.Size = picgris.Size
picHuit.Size = picgris.Size
picNeuf.Size = picgris.Size
lstNum.Items.Clear()
For i = 1 To 3
For j = 1 To 3
Dim L As New Point(picgris.Width * j + 100, picgris.Height * i)
lstNum.Items.Add(L)
Next
Next
For i = 1 To 3
For j = 1 To 3
Placement = Int(Rnd() * (lstNum.Items.Count))
Next
Next
End Sub
End Class
I created nine pictures boxes at design time. You would assign a different image to each picture box. They are all square and the same size. Mine are 100 x 100 to make the arithmetic easy.
I made an array of points as a form level variable. These point will form a 300 x 300 square with the picture boxes. I also declared an array of PictureBox. In the Form.Load I added the pictures boxes to the array.
To reposition the picture boxes assigned the array to a list. Items in this list will be removed because we don't want to assign the same location to more the one picture box. This will not effect the original array.
Looping through the picture boxes we assign a random position to the box then remove that point from the list.
Public Class PictureSort
Private Rand As New Random()
Private PointArray As Point() = {New Point(100, 100), New Point(200, 100), New Point(300, 100), New Point(100, 200), New Point(200, 200), New Point(300, 200), New Point(100, 300), New Point(200, 300), New Point(300, 300)}
Private PictureBoxArray(8) As PictureBox
Private Sub PictureSort_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBoxArray = {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8, PictureBox9}
End Sub
Private Sub RepositionPictureBoxes()
Dim lst = PointArray.ToList
For Each pb In PictureBoxArray
Dim index = Rand.Next(0, lst.Count)
pb.Location = lst(index)
lst.RemoveAt(index)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RepositionPictureBoxes()
End Sub
End Class
My advice is to use a control array - you have an example here that should help: VB.NET Control Array- Declaration and Application.
You just need to initiate that array of controls once, this can be done at form load.
The next step is to sort that array in a random manner. Finally, loop on the array and every time your current loop index modulo 3 = 0, then you increase the Y coordinates and reset the X position.
Here is an example. You can see that each time you click on the button, the picture boxes are rearranged on the form in random order using an ad hoc function. For each picture box, a bitmap is generated on the fly to show the index of the control.. this is for demonstration purposes.
Public Class frmPics
Private pics As New List(Of PictureBox)
Private Const picture_width As Integer = 100, picture_height As Integer = 50
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' instantiate controls
Dim font As New Font("Arial", 20, FontStyle.Regular, GraphicsUnit.Pixel)
For i As Integer = 1 To 9
Dim pic As New PictureBox
pic.Visible = False
pic.Name = "pic" & i
pic.Text = i.ToString
Console.WriteLine("Create control: name: " & pic.Name)
' generate an ad-hoc bitmap image showing the index of the control
Dim bitmap As New Bitmap(picture_width, picture_height)
Using g As Graphics = Graphics.FromImage(bitmap)
Dim width As Integer = CInt(g.MeasureString(Text, font).Width)
Dim height As Integer = CInt(g.MeasureString(Text, font).Height)
End Using
Using g As Graphics = Graphics.FromImage(bitmap)
g.Clear(Color.Blue)
g.DrawString(i.ToString, font, New SolidBrush(Color.White), 0, 0)
End Using
pic.Image = bitmap
pics.Add(pic)
Me.Controls.Add(pic)
Next
End Sub
Private Sub btnShuffle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShuffle.Click
Dim x As Integer = 10, y As Integer = picture_height
Dim counter As Integer = 1
Dim rnd As New Random()
' show controls on form
Console.WriteLine("Show controls on form")
Me.SuspendLayout()
For Each item In pics.OrderBy(Function() rnd.Next)
item.Width = picture_width
item.Height = picture_height
item.Location = New Point(x, y)
item.BorderStyle = BorderStyle.FixedSingle
item.Visible = True
Console.WriteLine("counter: " & counter & " - control name" & item.Name & " - position: " & item.Location.X & "/" & item.Location.Y & " text: " & item.Text)
' reset X position every 3 iterations
If counter Mod 3 = 0 Then
x = 10
y += item.Height
Else
x += item.Width
End If
counter += 1
Next
Me.ResumeLayout()
End Sub
End Class

Changing the backcolor of multiple buttons while holding down the mouse button?

I currently have buttons in panel that is located in every cell of a tablelayoutpanel. I want to be able to hold my left mouse button down and drag across the tablelayoutpanel and highlight all the buttons as my mouse goes over in red.
My code to create the Panel and buttons. The tablelayout panel is already created.
Private Sub LoadHandMatrix(HandMatrix As TableLayoutPanel)
Dim Hands As New List(Of String)()
Dim NRow As Integer = 12
Dim NCol As Integer = 12
Dim HandBtnArray((NRow + 1) * (NCol + 1) - 1) As Button
Dim FrequencyBtnArray((NRow + 1) * (NCol + 1) - 1) As Button
Dim PanelArray((NRow + 1) * (NCol + 1) - 1) As Panel
Hands = MatrixHands()
With HandMatrix
.Height = 1800
.Width = 1800
End With
For i As Integer = 0 To HandBtnArray.Length - 1
PanelArray(i) = New Panel
PanelArray(i).Name = "p" + Str(i)
PanelArray(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
PanelArray(i).BackColor = Color.White
HandMatrix.Controls.Add(PanelArray(i), i Mod (NCol + 1), i \ (NCol + 1))
AddHandler PanelArray(i).Click, AddressOf panel_Click
FrequencyBtnArray(i) = New Button
FrequencyBtnArray(i).Name = "f" + Str(i)
FrequencyBtnArray(i).TextAlign = ContentAlignment.TopLeft
FrequencyBtnArray(i).FlatStyle = FlatStyle.Flat
FrequencyBtnArray(i).FlatAppearance.BorderSize = 0
FrequencyBtnArray(i).FlatAppearance.MouseOverBackColor = Color.White
FrequencyBtnArray(i).FlatAppearance.MouseDownBackColor = Color.White
FrequencyBtnArray(i).Text = 0
FrequencyBtnArray(i).Height = 30
FrequencyBtnArray(i).Width = 45
FrequencyBtnArray(i).Location = New Point(87, 102)
FrequencyBtnArray(i).Font = New Font("Segoe UI", 6)
FrequencyBtnArray(i).BackColor = Color.White
FrequencyBtnArray(i).ForeColor = Color.Black
PanelArray(i).Controls.Add(FrequencyBtnArray(i))
HandBtnArray(i) = New Button()
HandBtnArray(i).Name = "h" + Str(i)
HandBtnArray(i).Text = Hands(i)
HandBtnArray(i).FlatStyle = FlatStyle.Flat
HandBtnArray(i).FlatAppearance.BorderSize = 0
HandBtnArray(i).FlatAppearance.MouseOverBackColor = Color.Yellow
HandBtnArray(i).BackColor = Color.White
HandBtnArray(i).TextAlign = ContentAlignment.TopCenter
HandBtnArray(i).Font = New Font("Segoe UI", 14)
HandBtnArray(i).Dock = DockStyle.Fill
PanelArray(i).Controls.Add(HandBtnArray(i))
AddHandler HandBtnArray(i).MouseDown, AddressOf X_MouseDown
Next
End Sub
I added an event handeler "AddHandler HandBtnArray(i).MouseDown, AddressOf X_MouseDown" for the mouse down event located at the bottom of the code.
Public Sub X_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
sender.backcolor = Color.Red
End Sub
This event only works when I let off the mouse button. I included a picture of my hand matrix
I want to for example click on AA in the top left and hold and drag my mouse over the different hands and change the backcolor as the mouse goes over them while holding the left mouse button. I tried using the on enter event and it worked well but obviously it just changes everything without clicking the mouse. Also tried working with MouseEventArgs but ran into problems with EventArgs.
Each question can contain only one question. I will address the first question only. Ask a new question for the other question.
Notice in the MouseDown event procedure.
sender As Object
An Object does not have a BackColor property. This would be obvious if you had Option Strict On which you should always have it On. You can find this setting in the Project Properties on the Compile tab.
To solve this problem, cast sender to Button, then assign the color.
Private Sub X_MouseDown(sender As Object, e As MouseEventArgs) Handles Button2.MouseDown
Dim b = DirectCast(sender, Button)
b.BackColor = Color.Red
End Sub

Check if Label text created in code contains specific string

I have several labels and textboxes created and the number of them depends on what the user types into a textbox, i.e. 5 typed in so there would be 5 labels and 5 textboxes.
I can recursively search out all labels which name is "myLabel" & var, var = number 1 through 5 in this example.
What i have is a context menu for the labels with 3 items. Item 1 changes the label text to whatever the user wants, numbers, letters, and/or symbols. Item 2 and 3 is what i am having trouble with.
Item 2 needs to change the label text to D1 the first time it is initiated, then it needs to change it to D2 the second time and if a third time is invoked an error should not let them continue.
Item 3 is the same as 2, except the text should be S1 and S2 respectively.
What I have so far is this, however i keep getting stuck in a loop or it doesn't change the label.text to what i want. Any help is appreciated
Private Sub lblMenuItem3_Click()
label = lblContextMenu.SourceControl.Name
For Each control As Control In Me.Controls
If TypeOf control Is Label Then
Dim myLabel As Label = DirectCast(control, Label)
Dim str As String = myLabel.Text
'If myLabel.Text = "S1" Then
If LCase(str).Contains(LCase("S1")) Then
'MessageBox.Show("That string is in here!")
Me.Controls.Item(label).Text = "S2"
Else
'MessageBox.Show("The string is not in here!")
Me.Controls.Item(label).Text = "S1"
End If
Else
Me.Controls.Item(label).Text = "S1"
'End If
End If
Next
'Me.Controls.Item(label).Text = "S1"
'Me.Controls.Item(label).Text = "S2"
End Sub
EDIT1:
This code is partially working as i need it to, but for some reason it runs the sub the number of times equal to the number of labels on the form and I'm not sure why
Private Sub lblMenuItem3_Click()
label = lblContextMenu.SourceControl.Name
Dim s1lbl As Boolean = False
Dim s2lbl As Boolean = False
For Each control As Control In Me.Controls
If TypeOf control Is Label Then
Dim myLabel As Label = DirectCast(control, Label)
Dim str As String = myLabel.Text
If LCase(str).Contains(LCase("S1")) Then
s1lbl = True
Exit For
ElseIf LCase(str).Contains(LCase("S2")) Then
s2lbl = True
Exit For
Else
Continue For
End If
End If
Next
If s1lbl = False AndAlso s2lbl = False Then
Me.Controls.Item(label).Text = "S1"
Exit Sub
End If
If s1lbl = True AndAlso s2lbl = False Then
Me.Controls.Item(label).Text = "S2"
Exit Sub
End If
If s1lbl = False AndAlso s2lbl = True Then
MessageBox.Show("Too many Shallow points, only 2 allowed.")
Exit Sub
End If
End Sub
this is the code:
Private Sub TextBoxABPts_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBoxABPts.KeyPress
For i = 1 To TextBoxABPts.Text
lbl1 = New Label()
lbl1.Location = New Point(2, 165 + 25 * (i - 1))
lbl1.Name = "myLabel" & i
lbl1.Text = i
Me.Controls.Add(lbl1)
AddHandler lblMenuItem1.Click, AddressOf lblMenuItem1_Click
AddHandler lblMenuItem2.Click, AddressOf lblMenuItem2_Click
AddHandler lblMenuItem3.Click, AddressOf lblMenuItem3_Click
lblContextMenu.MenuItems.Add(lblMenuItem1)
lblContextMenu.MenuItems.Add(lblMenuItem2)
lblContextMenu.MenuItems.Add(lblMenuItem3)
lbl1.ContextMenu = lblContextMenu
Next i
TextBoxABDist.Focus()
End If
I think this should help you out:
Add your menu items to the lblContextMenu outside the for loop where ever you create the lblContextMenu
lblContextMenu.MenuItems.Add("click1", New System.EventHandler(AddressOf Me.lblMenuItem1_Click1))
lblContextMenu.MenuItems.Add("click2", New System.EventHandler(AddressOf Me.lblMenuItem1_Click2))
lblContextMenu.MenuItems.Add("click3", New System.EventHandler(AddressOf Me.lblMenuItem1_Click3))
Then in your for loop just add the contextmenu to the labels:
For i As Integer = 1 To TextBoxABPts.Text
Dim lbl1 = New Label()
lbl1.Location = New Point(2, 165 + 25 * (i - 1))
lbl1.Name = "myLabel" & i
lbl1.Text = i
Me.Controls.Add(lbl1)
lbl1.ContextMenu = lblContextMenu
Next i.
Then as an example your menu click sub might look something like this:
Private Sub lblMenuItem1_Click1(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mi As MenuItem = CType(sender, MenuItem)
Dim menu As ContextMenu = mi.GetContextMenu()
Dim lbl As Label = CType(menu.SourceControl, Label)
lbl.Text = mi.Text
End Sub
You'll need to combine this with your current ifelse logic.
This:
If LCase(str).Contains(LCase("S1")) Then
'MessageBox.Show("That string is in here!")
Me.Controls.Item(label).Text = "S2"
Else
'MessageBox.Show("The string is not in here!")
Me.Controls.Item(label).Text = "S1"
End If
Should be this:
If LCase(str).Contains(LCase("S1")) Then
'MessageBox.Show("That string is in here!")
myLabel.Text = "S2"
ElseIf LCase(str).Contains(LCase("S2"))
'MessageBox.Show("Bad Stuff!")
' Do your S3 error things here
Else
'MessageBox.Show("No S Value Found!")
myLabel.Text = "S1"
End If
Without the ElseIf structure, you will alternate between setting it as S1 or S2, and there is no other option. With the structure in place you check for all possibilities and it shouldn't loop.
You need to use the proper signature in your method. It should look like this.
Label1_Click(sender As Object, e As EventArgs)
Then you can cast sender to a Label and set the text.
Ctype(sender, Label).Text = "S1"

Trying to add UserControl to a TabPage inside of another TabPage get NullReferenceException

I have an UserControl and wish to add it to a TabPage Inside another TabPage but get NullReferenceException instead.
My code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyControl As New TimerPanel
Dim Ubicacion As Point
Ubicacion.X = 274
Ubicacion.Y = 100
With MyControl
.Name = "Timer0"
.NombreTimerTxt.Text = "Timer GPS"
.TimerBox.Text = "Timer 00"
.Parent = Me '.TabControl3.TabPages("Timers")
.Location = Ubicacion
.Visible = True
End With
Me.TabControl3.TabPages("Timers").Controls.Add(MyControl) 'Error here
'Me.TabControl1.TabPages("Timers").Controls("Timer0").Location = Ubicacion
End Sub
The IDE say that I must declare it using the word "New", but I already did it on the first code line.
Another thing, If I iterate this code changing the name and coordinates I will get independent controls or when I change one all do the same?
My form looks like this.
Thanks to LarsTech I solve the problem and add several UserControls to my TabPage using the following code
Dim X As Integer = 4
Dim Y As Integer = 0
For XTimer As Integer = 0 To 15
Dim MyControl As New TimerPanel
Dim Ubicacion As Point
Ubicacion.X = X
Ubicacion.Y = Y
With MyControl
.Name = "Timer" & XTimer.ToString
.NombreTimerTxt.Text = "Timer GPS"
.TimerBox.Text = "Timer " & XTimer.ToString
.Parent = Me
.Location = Ubicacion
.Visible = True
End With
TimersTab.Controls.Add(MyControl)
Y += 51
If XTimer = 9 Then 'Start column 2
X = 344
Y = 0
End If
Next
I hope someone finds this useful; bye.

VB.net nearest control

Is there an easy way to get the nearest control to a control of choice?
I have a picture box and some other moving controls. I want to delete the nearest control to my picture box.
So I have to get the position of all controls and delete that with the Location nearest to the Location of my picture box. I'm not sure about how to do that the best way.
If "closest" in your case indeed means "with the Location nearest to the Location of my picture box", then the easiest would be:
Me.Controls.Remove((From c In Me.Controls.Cast(Of Control)() Order By c.Location.Distance(PictureBox1.Location) Select c).Skip(1).Take(1)(0))
, where Distance is defined in a module like this:
<System.Runtime.CompilerServices.Extension()> _
Public Function Distance(ByVal p1 As Point, ByVal p2 As Point) As Integer
Return (p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y)
End Function
Here's an entire sample form that calculates "closest" by checking if the supplied control is in 1 of 8 regions relative to the base control. Half of the code is setup trying to mimic the scenario you described. The MainButton_Click and below is the meat of the work.
Option Explicit On
Option Strict On
Public Class Form1
Private MainPB As PictureBox
Private OtherPB As List(Of PictureBox)
Private WithEvents MainButton As Button
Private Rnd As New Random()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Setup the form with sample data
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.MainButton = New Button()
Me.MainButton.Text = "Update"
Me.MainButton.Left = 1
Me.MainButton.Top = 20
Me.Controls.Add(Me.MainButton)
Me.Width = 1000
Me.Height = 1000
MainPB = New PictureBox()
MainPB.BackColor = Color.Red
MainPB.Width = 100
MainPB.Height = 100
MainPB.Left = (Me.Width \ 2) - (MainPB.Width \ 2)
MainPB.Top = (Me.Height \ 2) - (MainPB.Height \ 2)
Me.Controls.Add(MainPB)
Me.OtherPB = New List(Of PictureBox)
For I = 0 To 50
Me.OtherPB.Add(New PictureBox())
With Me.OtherPB(I)
.BackColor = Color.Transparent
.BorderStyle = BorderStyle.FixedSingle
.Width = 50
.Height = 50
End With
SetRandomPbLocation(Me.OtherPB(I))
Me.Controls.Add(Me.OtherPB(I))
Next
End Sub
Private Sub SetRandomPbLocation(ByVal pb As PictureBox)
'Just sets a random location for the picture boxes and ensures that it doesn't overlap with the center PB
Do While True
pb.Left = Rnd.Next(1, Me.Width - pb.Width)
pb.Top = Rnd.Next(1, Me.Height - pb.Height)
If (pb.Right < Me.MainPB.Left OrElse pb.Left > Me.MainPB.Right) AndAlso (pb.Top > Me.MainPB.Bottom OrElse pb.Bottom < Me.MainPB.Top) Then
Exit Do
End If
Loop
End Sub
Private Sub MainButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MainButton.Click
'Randomizes the location of the picture boxes
For Each PB In Me.OtherPB
SetRandomPbLocation(PB)
Next
'Will hold the closest control after the loop
Dim ClosestPB As Control = Nothing
Dim ClosestD, TempD As Double
For Each PB In Me.OtherPB
'Reset the control's background color
PB.BackColor = Color.Transparent
'Calculate the distance
TempD = GetDistanceBetweenToControls(PB, Me.MainPB)
If ClosestPB Is Nothing Then 'If this is the first time through the loop then just use this control as the closest
ClosestPB = PB
ClosestD = TempD
ElseIf TempD < ClosestD Then 'Otherwise if this control is closer than the current closest
ClosestPB = PB
ClosestD = TempD
End If
Next
'Set the closest controls background color so that we can see it
ClosestPB.BackColor = Color.Blue
End Sub
Private Shared Function GetDistanceBetweenToControls(ByVal controlToCheck As Control, ByVal baseControl As Control) As Double
If controlToCheck.Bottom < baseControl.Top Then
If controlToCheck.Right < baseControl.Left Then 'Above and to the left
Return DistanceBetweenTwoPoints(New Point(controlToCheck.Right, controlToCheck.Bottom), baseControl.Location)
ElseIf controlToCheck.Left > baseControl.Right Then 'above and to the right
Return DistanceBetweenTwoPoints(New Point(controlToCheck.Left, controlToCheck.Bottom), New Point(baseControl.Right, baseControl.Top))
Else 'Above
Return baseControl.Top - baseControl.Bottom
End If
ElseIf controlToCheck.Top > baseControl.Bottom Then
If controlToCheck.Right < baseControl.Left Then 'Below and to the left
Return DistanceBetweenTwoPoints(New Point(controlToCheck.Right, controlToCheck.Top), New Point(baseControl.Left, baseControl.Bottom))
ElseIf controlToCheck.Left > baseControl.Right Then 'Below and to the right
Return DistanceBetweenTwoPoints(controlToCheck.Location, New Point(baseControl.Right, baseControl.Bottom))
Else 'Below
Return controlToCheck.Top - baseControl.Bottom
End If
Else
If controlToCheck.Right < baseControl.Left Then 'Left
Return baseControl.Left - controlToCheck.Right
ElseIf controlToCheck.Left > baseControl.Right Then 'Right
Return controlToCheck.Left - baseControl.Right
End If
End If
End Function
Private Shared Function DistanceBetweenTwoPoints(ByVal point1 As Point, ByVal point2 As Point) As Double
'Standard distance formula
Return Math.Sqrt((Math.Abs(point2.X - point1.X) ^ 2) + (Math.Abs(point2.Y - point1.Y) ^ 2))
End Function
End Class