PC Review


Reply
Thread Tools Rate Thread

combobox binding question

 
 
Adam J. Schaff
Guest
Posts: n/a
 
      27th Oct 2004
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


 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      27th Oct 2004
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
---------------------
"Adam J. Schaff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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



 
Reply With Quote
 
=?Utf-8?B?Q2hhcmxpZQ==?=
Guest
Posts: n/a
 
      27th Oct 2004
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

"Adam J. Schaff" wrote:

> 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
>
>
>

 
Reply With Quote
 
Adam J. Schaff
Guest
Posts: n/a
 
      30th Oct 2004
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.

"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> ---------------------
> "Adam J. Schaff" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Desperate Lookup Combobox or binding combobox sennopati Microsoft VB .NET 0 9th Feb 2008 11:09 AM
ComboBox binding to subset of another ComboBox =?Utf-8?B?V2ViYmVydA==?= Microsoft Dot NET Framework Forms 2 13th Sep 2006 04:44 PM
ComboBox binding to subset of another ComboBox =?Utf-8?B?V2ViYmVydA==?= Microsoft Dot NET Framework 0 13th Sep 2006 02:13 PM
comboBox binding question(with different DisplayMember & ValueMemb =?Utf-8?B?QnJ1Y2U=?= Microsoft VB .NET 1 6th Jan 2005 09:02 PM
Binding data to comboBox question =?Utf-8?B?Sm9uYXRoYW4gTGVl?= Microsoft C# .NET 0 2nd Mar 2004 05:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:40 AM.