1

I am trying to change combobox's DropDownWidth based on maximum string in Combobox's items. The code below returns the maximum string length from all the items.

 Dim maxStringLength As Integer = cboDt.AsEnumerable().
                  SelectMany(Function(row) row.ItemArray.OfType(Of String)()).
                  Max(Function(str) str.Length)

cboDt is the datatable attached to combobox.
I want to return the actual string. For example if combobox items are:
"aaa"
"bbbb"
"ccccc"
My code returns maxStringLength = 5 (because 5 is the maximum number of characters of all items-here is ccccc) I want code to retun "ccccc" (of course in a string variable)

Nianios
  • 1,391
  • 3
  • 20
  • 45
  • How many columns does your `DataTable` have, which are you using in the ComboBox? The rest is not relevant for your `maxStringLength`. – Tim Schmelter Jan 31 '13 at 13:04
  • Just to answer that. My datatable always have two (the display and the value column) – Nianios Jan 31 '13 at 13:40

3 Answers3

3

Order the list by string-length descending, and then take the first result.

Dim maxStringLength As Integer = 
    cboDt.AsEnumerable().
    SelectMany(Function(row) row.ItemArray.OfType(Of String)()).
    OrderByDescending(Function(str) str.Length).
    First()  ' You can use FirstOrDefault here, if you are
             ' not certain there will be a result.
RB.
  • 36,301
  • 12
  • 91
  • 131
2

Assuming that the first column of the DataTable is displayed in the ComboBox:

Dim maxStringLength As Integer = cboDt.AsEnumerable().
        Max(Function(r) r.Field(Of String)(0).Length)

Note that this assumes that this requires that this column is never null.

( I don't see a reason why you would measure the length of the (possibly available) other columns of the table when they aren't shown in the ComboBox at all. )

Update

Find the maximum string in Combobox

Now i got it, you want the string not the length:

Dim longestString = cboDt.AsEnumerable().
        OrderByDescending(Function(r) r.Field(Of String)(0).Length).
        First().Field(Of String)(0)
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • the reason is the length of the string and he want to modify the `DropDownWidth` base of the length `:)` – spajce Jan 31 '13 at 13:17
  • sir, i guess the OP using a one column only with the [ComboBox and DataTable](http://stackoverflow.com/a/256871/1080742) – spajce Jan 31 '13 at 13:27
  • @TimSchmelter: Your code returns the .Length, but I want the string. Your note for the columns is correct – Nianios Jan 31 '13 at 13:27
  • @Nianios, the answer of Mr. Tim is already updated. +1 for you sir.. you have great mind :) – spajce Jan 31 '13 at 13:31
0

You can achieve this using linq and Finding the Index of maxStringLength = 5

Dim ls = comboBox4.Items.Cast(Of String)().ToList()
Dim index = ls.FindIndex(Function(c) c.ToString().Count() >= 5)
comboBox4.SelectedIndex = index

or using Max() Method

Dim ls = comboBox4.Items.Cast(Of String)().ToList()
Dim index = ls.Max()
comboBox4.Text = index
spajce
  • 7,044
  • 5
  • 29
  • 44
  • I like your idea, unfortunately my Combobox is a third party and it doesn't have the .Items – Nianios Jan 31 '13 at 13:30
  • ..hows it look like? could you name your _comboBox third party_? – spajce Jan 31 '13 at 13:32
  • Sorry my combobox is a component not from Visual Studio. Sorry for any inconvience – Nianios Jan 31 '13 at 13:36
  • i don't believe that your third party comboBox had no.. or something like [ObjectionCollection](http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection(v=vs.71).aspx), but its okay `:)` – spajce Jan 31 '13 at 13:40