0

I'm using VB.NET and Visual Studio 2010

I've got a windows form with a combobox.

I fill the combobox using the following

Dim objSizes As List(Of ASME_Hub_Sizes) = ASME_Hub_Sizes.GetAllHubSizes()

        If Not objSizes Is Nothing Then
            With Me.cboSize
                .DisplayMember = "Size"
                .ValueMember = "ID"
                .DataSource = objSizes
            End With
        End If

This works fine, but i would like to add a "Select Size..." option but i'm unsure how to do this.

It seems so much easier to do this in asp.net, but this has me baffled

Thanks Mick

user551353
  • 79
  • 1
  • 6
  • Can you insert "Select Size..." into your objSizes collection right after your call to `GetAllHubSizes`? – Brad Rem Jun 22 '13 at 13:48

2 Answers2

0

You could try adding a custom objSize object to the objSizes collection that has ID = 0 and value = "Select size..." as it's ID 0 it should be at the top (I think) and won't clash with any values in your database, upon saving the record you can validate the combobox to avoid writing the "Select size..." object to your database. I'll have a bit of a code and see if this will work...

Ok, I've had another look. You can do as I suggested but you must sort the list before passing it to the combobox. Here is my example:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        With Me.ComboBox1
            .DisplayMember = "Description"
            .ValueMember = "ID"
            .DataSource = GetListOfObjects.returnListOFObjects
        End With
    End Sub
End Class

Public Class dummyObjectForCombo

    Public Property ID As Integer
    Public Property Description As String

    Public Sub New(ByVal id As Integer,
                   ByVal description As String)
        _ID = id
        _Description = description
    End Sub
End Class

Public Class GetListOfObjects

    Public Shared Function returnListOFObjects() As List(Of dummyObjectForCombo)

        Dim col As New List(Of dummyObjectForCombo)

        Dim obj0 As New dummyObjectForCombo(-1, "Herp")
        Dim obj1 As New dummyObjectForCombo(1, "Jamie")
        Dim obj2 As New dummyObjectForCombo(2, "Bob")

        col.Add(obj1)
        col.Add(obj2)
        col.Add(obj0)

        'using a lambda to sort by ID as per http://stackoverflow.com/questions/3309188/c-net-how-to-sort-a-list-t-by-a-property-in-the-object
        Return col.OrderBy(Function(x) x.ID).ToList

    End Function

End Class

I've used -1 as the topmost record instead of 0.

So you'd get your list as usual, add in the extra dummy record, then sort it as per the above code before assigning it as your combo boxes datasource.

majjam
  • 1,286
  • 2
  • 15
  • 32
0

simply add the item before setting the datasource property

C# : cboSize.items.add(...);

  • You can't modify the item collection after you bind it to the ComboBox. It has to be done before binding. – Tim Jun 22 '13 at 10:47
  • i just forgot about this because i did it with asp.net , however it can be done Before binding , also setting Text property will do the job too. – Eslam Gamal Jun 22 '13 at 11:04