Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

combobox binding question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Adam J. Schaff - 27 Oct 2004 04:08 GMT
I am forgetting something obvious, I think. I have a form with a combobox on
it. I place the code (below) in the form. Instead of populating the dropdown
with 0 and 1, as I expected, it populates it with two rows, each containing
the string "MyProject.Form1+test"

What am I missing (besides sleep)? Do I need to implement some kind of
interface in test?

  Public Class test
     Public Sub New(ByVal v As Integer)
        Me.Value = v
     End Sub
     Public Value As Integer
  End Class

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
     Dim x As New ArrayList
     x.Add(New test(0))
     x.Add(New test(1))
     cbo.DataSource = x
End Sub

-AJ
Ken Tucker [MVP] - 27 Oct 2004 04:21 GMT
Hi,

       You can bind to an arraylist and show any property in the class.
Change you class to this.
Public Class test

Dim mVal As Integer

Public Sub New(ByVal v As Integer)

Me.Value = v

End Sub

Public Property Value() As Integer

Get

Return mVal

End Get

Set(ByVal Value As Integer)

mVal = Value

End Set

End Property

End Class

Dont forget to set the display member

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
     Dim x As New ArrayList
     x.Add(New test(0))
     x.Add(New test(1))
     cbo.DataSource = x

     cbo.displaymember = "Value"
End Sub

Ken
---------------------
I am forgetting something obvious, I think. I have a form with a combobox on
it. I place the code (below) in the form. Instead of populating the dropdown
with 0 and 1, as I expected, it populates it with two rows, each containing
the string "MyProject.Form1+test"

What am I missing (besides sleep)? Do I need to implement some kind of
interface in test?

  Public Class test
     Public Sub New(ByVal v As Integer)
        Me.Value = v
     End Sub
     Public Value As Integer
  End Class

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
     Dim x As New ArrayList
     x.Add(New test(0))
     x.Add(New test(1))
     cbo.DataSource = x
End Sub

-AJ
Adam J. Schaff - 30 Oct 2004 14:55 GMT
Thanks!  I had tried setting the DisplayMember, but I hadn't tried using
true properties instead of public variables. I had no idea there was such a
difference. Ironically, I always use full properties in my classes for
professional work. I was "cheating" here because I was working on a quick,
throwaway application to test a concept.

Anyway, thanks again for the help.

> Hi,
>
[quoted text clipped - 69 lines]
>
> -AJ
Charlie - 27 Oct 2004 07:09 GMT
For the datasource, you can use any object that implements iList.  

If you want to use an ArrayList, just populate it with strings and assign
the ComboBox.datasource to the ArrayList.  The ComboBox.SelectedIndex
property will return the zero-based index value of the entry.

You could use a DataTable, or a DataView as the datasource. You can assign
the ComboBox.DisplayMember and ValueMember properties to the corresponding
fields of the DataTable.  In that case the SelectedValue property will return
the field value of ValueMember, and DisplayMember provides the list of
visible items.

www.charlesfarriersoftware.com

> I am forgetting something obvious, I think. I have a form with a combobox on
> it. I place the code (below) in the form. Instead of populating the dropdown
[quoted text clipped - 20 lines]
>
> -AJ

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.