Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get lowest location of controls in array using linq

i have array of controls say
dim oClipboard() as control


i needed the control which has lowest location or minimum location value from tha oclipboard

What I have tried:

so far i have tried linq with min function

Dim p = c.Select(Function(g) g.Location).Min.ToString
Dim x = c.Select(Function(g) g).Min(Function(h) h.Location)

both of above give me error stated below

System.ArgumentException was unhandled
HResult=-2147024809
Message=At least one object must implement IComparable.
Source=mscorlib
StackTrace:
at System.Collections.Comparer.Compare(Object a, Object b)
at System.Collections.Generic.ObjectComparer`1.Compare(T x, T y)
at System.Linq.Enumerable.Min[TSource](IEnumerable`1 source)
at System.Linq.Enumerable.Min[TSource,TResult](IEnumerable`1 source, Func`2 selector)

is there any way i could get control whhich has lowest location
Posted
Updated 15-May-16 7:16am
Comments
BillWoodruff 7-May-16 0:24am    
Note that if your collection of Controls includes Controls that are nested in other Controls, or Controls from more than one Form, then you will need to translate the Location value for each Control into screen co-ordinates before comparing.

Try something like:
C#
Control lowest = myControls.OrderByDescending(c => c.Location.Y).First();

VB (untested):
C#
Dim lowest As Control = myControls.OrderByDescending(Function(c) c.Location.Y).First()
 
Share this answer
 
Comments
Omkaara 6-May-16 6:33am    
@OriginalGriff how many times i have to tell you that you are genius!!!!
OriginalGriff 6-May-16 6:42am    
One is probably too many... :blush:
What's wrong with Enumerable.Min()[^] and Enumerable.Max()[^] functions?

C#
var lowest = c.Min(x=>x.Location.Y);
var highest = c.Max(x=>x.Location.Y);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900