Results 1 to 7 of 7

Thread: Bind Enumeration to ComboBox

  1. #1

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Bind Enumeration to ComboBox

    How can this be accomplished, setting the display and value members of the Combobox?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Can you explain more of what you are trying to do?

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Not through databinding, b/c an enum does not implement IList and the other interfaces necessary for databinding.

  4. #4

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    But to create a bindable name value collection that implements IBindingList you have to implement a whole shlew of functions and properties that I don't need. Is there any other way?

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can't bind directly but you can use the Parse and GetNames methods to link an Enum to a combo.

  6. #6

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Can you post sample code? Are you using cbo.Datasource and then Value/Display member or cbo.DataBindings...

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I'm not really 'binding' except to the array of enum names. To get the value you can just parse the text though.
    VB Code:
    1. Public Enum Testers
    2.         Test1
    3.         Test2
    4.         Test3
    5.     End Enum
    6.  
    7.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    8.         'fill combo with names of enum
    9.         ComboBox1.DataSource = [Enum].GetNames(GetType(Testers))
    10.     End Sub
    11.  
    12.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    13.         'convert the combo text back to an enum
    14.         MessageBox.Show([Enum].Parse(GetType(Testers), ComboBox1.Text))
    15.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width