473,499 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

combobox binding question

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
Nov 21 '05 #1
3 969
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

Nov 21 '05 #2
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" <ab****@adelphia.net> wrote in message
news:ur**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #3
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]" <vb***@bellsouth.net> wrote in message
news:ea**************@TK2MSFTNGP09.phx.gbl...
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" <ab****@adelphia.net> wrote in message
news:ur**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2611
by: Bruce | last post by:
Hi, there, I meet a problem about comboBox binding. -------------------- Database: Northwind Tables: 1) Products 2) Categories I create a form (named "form1") to edit the record from...
4
4600
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
5
9147
by: Eric A. Johnson | last post by:
Hi Everyone, I am at my wit's end here. I have a combobox (combyQueryTitle) that I need to use in order to select a query for my database project. Therefore, I am using the...
3
4554
by: amber | last post by:
Hello, I have 6 comboboxes, that I thought I could bind to the same datasource, and have them display different 'selectedvalue's', but apparently I can't... My code is as follows: dim drv as...
6
2840
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
2
8642
by: shumaker | last post by:
I have a combobox that is very much like the one found in the RSS project here: http://msdn.microsoft.com/vstudio/express/visualCSharp/learning/ My projectNameComboBox basically is filled with a...
5
4633
by: Joe | last post by:
Hi I am adding a class to a ComboBox - and all is fine except that I fill the combobox on the Form_Load Method and it causes the method private void comboBox2_SelectedIndexChanged(object...
0
1989
by: Frnak McKenney | last post by:
Can I use a bound ComboBox for both browsing and editing? I'm working on a small, standalone database application using Visual C#.NET 2003 and an Access data file. In order to keep the number...
3
7487
by: Simon Tamman | last post by:
I've come across an interesting bug. I have workarounds but i'd like to know the root of the problem. I've stripped it down into a short file and hope someone might have an idea about what's going...
2
1810
by: gleadams | last post by:
I have databound a Windows form ComboBox control to a DataSource and DataMember and it is properly showing the data as I navigate through the records. My question concerns the choices in the...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.