longest field value for each col in a generic collection

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,

i have a generic collection and was wondering if there was an easy way to
get the longest field value in each column?

thanks,
rodchar
 
rodchar said:
hey all,

i have a generic collection and was wondering if there was an easy
way to get the longest field value in each column?

What field value and what columns?
 
rodchar said:
hey all,

i have a generic collection and was wondering if there was an easy way to
get the longest field value in each column?

thanks,
rodchar

If you mean the displayed width of each item in each row, you would need
to loop and keep track of the results of calls to Graphics.MeasureString().
 
You can try this.

You can replace MyStringCollection with its base (List<string>).
However, I coded it because that is what I do.
And you might have an object (<T>) which is not a string.
So the code below makes it updatable with a different entity. (aka,
substitute an Employee object for the string(s) below.


public class MyStringCollection : List<string>

{

}

class Program

{

static void Main(string[] args)

{

MyStringCollection sc = new MyStringCollection();

sc.Add("One");

sc.Add("Five");

sc.Add("Ten");

sc.Add("Twelve");

sc.Add("Thirteen");

sc.Add("Twenty");



//http://www.cubiczone.com/Articles/tabid/65/EntryID/23/Default.aspx

sc.Sort(delegate(string a, string b)

{

if(String.IsNullOrEmpty (a)) return 0;

if (String.IsNullOrEmpty(b)) return 0;

if (a.Length > b.Length) return 1;

if (b.Length < a.Length) return -1;

return 0;

});

//sc.Sort(new CustomStringComparer());

string longestString = string.Empty;

if (null != sc)

{

if (sc.Count > 0)

{

longestString = sc[0];

}

}

Console.WriteLine(longestString);



Console.WriteLine("\n\rPress ENTER To Continue");

Console.ReadLine();

}



}
 
thanks all for the help,
rod.

sloan said:
You can try this.

You can replace MyStringCollection with its base (List<string>).
However, I coded it because that is what I do.
And you might have an object (<T>) which is not a string.
So the code below makes it updatable with a different entity. (aka,
substitute an Employee object for the string(s) below.


public class MyStringCollection : List<string>

{

}

class Program

{

static void Main(string[] args)

{

MyStringCollection sc = new MyStringCollection();

sc.Add("One");

sc.Add("Five");

sc.Add("Ten");

sc.Add("Twelve");

sc.Add("Thirteen");

sc.Add("Twenty");



//http://www.cubiczone.com/Articles/tabid/65/EntryID/23/Default.aspx

sc.Sort(delegate(string a, string b)

{

if(String.IsNullOrEmpty (a)) return 0;

if (String.IsNullOrEmpty(b)) return 0;

if (a.Length > b.Length) return 1;

if (b.Length < a.Length) return -1;

return 0;

});

//sc.Sort(new CustomStringComparer());

string longestString = string.Empty;

if (null != sc)

{

if (sc.Count > 0)

{

longestString = sc[0];

}

}

Console.WriteLine(longestString);



Console.WriteLine("\n\rPress ENTER To Continue");

Console.ReadLine();

}



}

rodchar said:
hey all,

i have a generic collection and was wondering if there was an easy way to
get the longest field value in each column?

thanks,
rodchar
 
Back
Top