I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember
and DisplayMember properties of the Combobox. Is this possible, or do I
need to bind to a DataTable?
Thanks 16 5160
Hi,
Yes you can bind to any property contained in a class stored in the
arraylist.
Ken
----------------------
"Michael C#" <xy*@abcdef.com> wrote in message
news:Xt*****************@fe08.lga...
I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember
and DisplayMember properties of the Combobox. Is this possible, or do I
need to bind to a DataTable?
Thanks
"Michael C#" <xy*@abcdef.com> schrieb: I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember and DisplayMember properties of the Combobox. Is this possible
Yes.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Thank you
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl... Hi,
Yes you can bind to any property contained in a class stored in the arraylist.
Ken ---------------------- "Michael C#" <xy*@abcdef.com> wrote in message news:Xt*****************@fe08.lga... I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember and DisplayMember properties of the Combobox. Is this possible, or do I need to bind to a DataTable?
Thanks
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eq**************@TK2MSFTNGP15.phx.gbl... "Michael C#" <xy*@abcdef.com> schrieb: I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember and DisplayMember properties of the Combobox. Is this possible
Yes.
Well as long as it's possible.
I believe that you have to set the DataSource name to "ArrayList" whereas for
a Datatable, you set this to the DataTable's name. At least this is what you
have to for binding the DataGrid to an ArrayList so I would suppose it's the
same for a combo box. The DataMember has no meaning when binding to an
arraylist.
"Michael C#" wrote: "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:eq**************@TK2MSFTNGP15.phx.gbl... "Michael C#" <xy*@abcdef.com> schrieb: I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember and DisplayMember properties of the Combobox. Is this possible
Yes.
Well as long as it's possible.
So you can't set the Value and a separate distince DisplayMember? For
instance:
1, Yes
2, No
3, Maybe
If I display and choose "No" in the Combobox, there is no way to retrieve
the value 2 which is related to that option? It seems like that's a bit of
important functionality to be missing.
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com... I believe that you have to set the DataSource name to "ArrayList" whereas for a Datatable, you set this to the DataTable's name. At least this is what you have to for binding the DataGrid to an ArrayList so I would suppose it's the same for a combo box. The DataMember has no meaning when binding to an arraylist.
"Michael C#" wrote:
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:eq**************@TK2MSFTNGP15.phx.gbl... > "Michael C#" <xy*@abcdef.com> schrieb: >> I'm binding a Combobox to an Arraylist, and I'd like to set the >> ValueMember and DisplayMember properties of the Combobox. Is this >> possible > > Yes. >
Well as long as it's possible.
"Michael C#" <xy*@abcdef.com> schrieb: I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember and DisplayMember properties of the Combobox. Is this possible
Yes.
Well as long as it's possible.
???
Sample:
\\\
Private Sub Form1_Load( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Load
Dim al As New ArrayList
For i As Integer = 1 To 20
Dim p As New Person
p.Name = "Name " & CStr(i)
p.Age = 70 - i
al.Add(p)
Next i
With Me.ListBox1
.DisplayMember = "Name"
.ValueMember = "Age"
.DataSource = al
End With
End Sub
Private Sub ListBox1_SelectedIndexChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ListBox1.SelectedIndexChanged
MsgBox("Selected value: " & CStr(Me.ListBox1.SelectedValue))
End Sub
..
..
..
Public Class Person
Private m_Name As String
Private m_Age As Integer
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property
Public Property Age() As Integer
Get
Return m_Age
End Get
Set(ByVal Value As Integer)
m_Age = Value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.Name & " (" & Me.Age.ToString() & ")"
End Function
End Class
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Thank you for expounding on your initial 4-character response. I appreciate
it.
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:u3**************@TK2MSFTNGP10.phx.gbl... "Michael C#" <xy*@abcdef.com> schrieb: I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember and DisplayMember properties of the Combobox. Is this possible
Yes.
Well as long as it's possible.
???
Sample:
\\\ Private Sub Form1_Load( _ ByVal sender As Object, _ ByVal e As EventArgs _ ) Handles MyBase.Load Dim al As New ArrayList For i As Integer = 1 To 20 Dim p As New Person p.Name = "Name " & CStr(i) p.Age = 70 - i al.Add(p) Next i With Me.ListBox1 .DisplayMember = "Name" .ValueMember = "Age" .DataSource = al End With End Sub
Private Sub ListBox1_SelectedIndexChanged( _ ByVal sender As Object, _ ByVal e As EventArgs _ ) Handles ListBox1.SelectedIndexChanged MsgBox("Selected value: " & CStr(Me.ListBox1.SelectedValue)) End Sub . . . Public Class Person Private m_Name As String Private m_Age As Integer
Public Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value End Set End Property
Public Property Age() As Integer Get Return m_Age End Get Set(ByVal Value As Integer) m_Age = Value End Set End Property
Public Overrides Function ToString() As String Return Me.Name & " (" & Me.Age.ToString() & ")" End Function End Class ///
-- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/>
Why wont this work with a structure and combo box
Public Structure myStructure
Public Name As String
Public Title As String
End Structure
Dim arList As New ArrayList
Dim i As Integer
For i = 0 To 10
Dim ms As New myStructure
ms.Name = "Name " & i.ToString
ms.Title = "Title " & i.ToString
arList.Add(ms)
Next
'
With cmboMyTest
.DisplayMember = "Name"
.ValueMember = "Title"
.DataSource = arList
End With
When I run this, the combo box is filled with the fully qualified name
of the form and "+myStructure"
That is: "ObjectIntro.FormSecondTest+myStructure" is displayed in the
10 items of the combo box.
Thanks for all the help, always.
John
On Mon, 21 Mar 2005 00:45:28 +0100, "Herfried K. Wagner [MVP]"
<hi***************@gmx.at> wrote: "Michael C#" <xy*@abcdef.com> schrieb: I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember and DisplayMember properties of the Combobox. Is this possible
Yes.
Well as long as it's possible.
???
Sample:
\\\ Private Sub Form1_Load( _ ByVal sender As Object, _ ByVal e As EventArgs _ ) Handles MyBase.Load Dim al As New ArrayList For i As Integer = 1 To 20 Dim p As New Person p.Name = "Name " & CStr(i) p.Age = 70 - i al.Add(p) Next i With Me.ListBox1 .DisplayMember = "Name" .ValueMember = "Age" .DataSource = al End With End Sub
Private Sub ListBox1_SelectedIndexChanged( _ ByVal sender As Object, _ ByVal e As EventArgs _ ) Handles ListBox1.SelectedIndexChanged MsgBox("Selected value: " & CStr(Me.ListBox1.SelectedValue)) End Sub . . . Public Class Person Private m_Name As String Private m_Age As Integer
Public Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value End Set End Property
Public Property Age() As Integer Get Return m_Age End Get Set(ByVal Value As Integer) m_Age = Value End Set End Property
Public Overrides Function ToString() As String Return Me.Name & " (" & Me.Age.ToString() & ")" End Function End Class ///
Follow on question/observation:
When I create a class as you suggest, I can get the binding for the
listbox to work but not for the combobox. Neither work if the
arraylist contains a structure. So I am confused on the issue of
using a class vs structure as a datasource and why the combobox does
not work with either.
Secondly, when I display the results in the SelectedIndexChanged
event, it works as you have posted for the selectedvalue but there is
no simple way to retrieve the string that is displayed. SelectedItem
returns the reference to the person class. I think that makes sense,
but just want to verify.
From all of this, I am sensing that when you fill a listbox (and
combobox?) directly with strings (that is no binding), the display and
value members are the same...that is, the string.
Thanks again for all the help and clarification,
John
On Mon, 21 Mar 2005 00:45:28 +0100, "Herfried K. Wagner [MVP]"
<hi***************@gmx.at> wrote: "Michael C#" <xy*@abcdef.com> schrieb: I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember and DisplayMember properties of the Combobox. Is this possible
Yes.
Well as long as it's possible.
???
Sample:
\\\ Private Sub Form1_Load( _ ByVal sender As Object, _ ByVal e As EventArgs _ ) Handles MyBase.Load Dim al As New ArrayList For i As Integer = 1 To 20 Dim p As New Person p.Name = "Name " & CStr(i) p.Age = 70 - i al.Add(p) Next i With Me.ListBox1 .DisplayMember = "Name" .ValueMember = "Age" .DataSource = al End With End Sub
Private Sub ListBox1_SelectedIndexChanged( _ ByVal sender As Object, _ ByVal e As EventArgs _ ) Handles ListBox1.SelectedIndexChanged MsgBox("Selected value: " & CStr(Me.ListBox1.SelectedValue)) End Sub . . . Public Class Person Private m_Name As String Private m_Age As Integer
Public Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value End Set End Property
Public Property Age() As Integer Get Return m_Age End Get Set(ByVal Value As Integer) m_Age = Value End Set End Property
Public Overrides Function ToString() As String Return Me.Name & " (" & Me.Age.ToString() & ")" End Function End Class ///
OK...now I see what is happening...you need to create properties in
the structure. When I did that, both the class and the structure work
with both the combo box and the list box.
So what are the merits of using a structure vs a class in this case?
John
On Mon, 21 Mar 2005 00:45:28 +0100, "Herfried K. Wagner [MVP]"
<hi***************@gmx.at> wrote: "Michael C#" <xy*@abcdef.com> schrieb: I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember and DisplayMember properties of the Combobox. Is this possible
Yes.
Well as long as it's possible.
???
Sample:
\\\ Private Sub Form1_Load( _ ByVal sender As Object, _ ByVal e As EventArgs _ ) Handles MyBase.Load Dim al As New ArrayList For i As Integer = 1 To 20 Dim p As New Person p.Name = "Name " & CStr(i) p.Age = 70 - i al.Add(p) Next i With Me.ListBox1 .DisplayMember = "Name" .ValueMember = "Age" .DataSource = al End With End Sub
Private Sub ListBox1_SelectedIndexChanged( _ ByVal sender As Object, _ ByVal e As EventArgs _ ) Handles ListBox1.SelectedIndexChanged MsgBox("Selected value: " & CStr(Me.ListBox1.SelectedValue)) End Sub . . . Public Class Person Private m_Name As String Private m_Age As Integer
Public Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value End Set End Property
Public Property Age() As Integer Get Return m_Age End Get Set(ByVal Value As Integer) m_Age = Value End Set End Property
Public Overrides Function ToString() As String Return Me.Name & " (" & Me.Age.ToString() & ")" End Function End Class ///
Well, while I am talking to myself LOL...
Very interesting...if I put both a listbox and combobox on the form
and bind them to the same arraylist, they become synchronized! Not
sure how I would use that...but it is interesting.
John
On Sun, 20 Mar 2005 19:05:17 -0500, "Michael C#" <xy*@abcdef.com>
wrote: Thank you for expounding on your initial 4-character response. I appreciate it.
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:u3**************@TK2MSFTNGP10.phx.gbl... "Michael C#" <xy*@abcdef.com> schrieb:> I'm binding a Combobox to an Arraylist, and I'd like to set the > ValueMember and DisplayMember properties of the Combobox. Is this > possible
Yes.
Well as long as it's possible.
???
Sample:
\\\ Private Sub Form1_Load( _ ByVal sender As Object, _ ByVal e As EventArgs _ ) Handles MyBase.Load Dim al As New ArrayList For i As Integer = 1 To 20 Dim p As New Person p.Name = "Name " & CStr(i) p.Age = 70 - i al.Add(p) Next i With Me.ListBox1 .DisplayMember = "Name" .ValueMember = "Age" .DataSource = al End With End Sub
Private Sub ListBox1_SelectedIndexChanged( _ ByVal sender As Object, _ ByVal e As EventArgs _ ) Handles ListBox1.SelectedIndexChanged MsgBox("Selected value: " & CStr(Me.ListBox1.SelectedValue)) End Sub . . . Public Class Person Private m_Name As String Private m_Age As Integer
Public Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value End Set End Property
Public Property Age() As Integer Get Return m_Age End Get Set(ByVal Value As Integer) m_Age = Value End Set End Property
Public Overrides Function ToString() As String Return Me.Name & " (" & Me.Age.ToString() & ")" End Function End Class ///
-- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/>
JL,
I never saw Herfried spontanisly use structures in his samples, and he does
that in this case as well not.
Cor
"J L" <jo**@marymonte.com> schrieb: OK...now I see what is happening...you need to create properties in the structure. When I did that, both the class and the structure work with both the combo box and the list box.
So what are the merits of using a structure vs a class in this case?
The decision whether or not to use a structure should not be based on
setting a data source. You will find some information on when to choose
structures here:
<URL:http://groups.google.de/groups?threadm=8z%25Kb.2347%24d27.1979%40news-binary.blueyonder.co.uk>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
I mistyped, I meant to say that you can't set the DataMember, not Display
Member. You should be able to set the Display member OK.
"Michael C#" wrote: So you can't set the Value and a separate distince DisplayMember? For instance:
1, Yes 2, No 3, Maybe
If I display and choose "No" in the Combobox, there is no way to retrieve the value 2 which is related to that option? It seems like that's a bit of important functionality to be missing.
"Dennis" <De****@discussions.microsoft.com> wrote in message news:BC**********************************@microsof t.com...I believe that you have to set the DataSource name to "ArrayList" whereas for a Datatable, you set this to the DataTable's name. At least this is what you have to for binding the DataGrid to an ArrayList so I would suppose it's the same for a combo box. The DataMember has no meaning when binding to an arraylist.
"Michael C#" wrote:
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:eq**************@TK2MSFTNGP15.phx.gbl... > "Michael C#" <xy*@abcdef.com> schrieb: >> I'm binding a Combobox to an Arraylist, and I'd like to set the >> ValueMember and DisplayMember properties of the Combobox. Is this >> possible > > Yes. >
Well as long as it's possible.
OK, that does make a little more sense taken with Herfried K's code. Thanks
for the clarification.
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com... I mistyped, I meant to say that you can't set the DataMember, not Display Member. You should be able to set the Display member OK.
"Michael C#" wrote:
So you can't set the Value and a separate distince DisplayMember? For instance:
1, Yes 2, No 3, Maybe
If I display and choose "No" in the Combobox, there is no way to retrieve the value 2 which is related to that option? It seems like that's a bit of important functionality to be missing.
"Dennis" <De****@discussions.microsoft.com> wrote in message news:BC**********************************@microsof t.com... >I believe that you have to set the DataSource name to "ArrayList" >whereas >for > a Datatable, you set this to the DataTable's name. At least this is > what > you > have to for binding the DataGrid to an ArrayList so I would suppose > it's > the > same for a combo box. The DataMember has no meaning when binding to an > arraylist. > > "Michael C#" wrote: > >> >> "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message >> news:eq**************@TK2MSFTNGP15.phx.gbl... >> > "Michael C#" <xy*@abcdef.com> schrieb: >> >> I'm binding a Combobox to an Arraylist, and I'd like to set the >> >> ValueMember and DisplayMember properties of the Combobox. Is this >> >> possible >> > >> > Yes. >> > >> >> Well as long as it's possible. >> >> >> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jason |
last post by:
Here is an odd issue. I am trying to shed some light on why this is causing
a problem.
I have an ArrayList. I am binding it to a ListBox control...
|
by: Jim Bancroft |
last post by:
Hi everyone,
I'm binding an ArrayList to a DataGrid for the first time (I'm used to
binding DataSets and DataTables) and I was wondering if I...
|
by: Alan Silver |
last post by:
Hello,
I'm trying to use an ArrayList to do data binding, but am getting an
error I don't understand. I posted this in another thread, but that...
|
by: guy |
last post by:
I have a winforms datagrid bound to an arraylist which works correctly,
however if I add a row to the arraylist i cannot get the datagrid to display...
|
by: lgbjr |
last post by:
hi All,
I've decided to use Options Strict ON in one of my apps and now I'm trying
to fix a late binding issue. I have 5 integer arrays:
dim...
|
by: Dean Slindee |
last post by:
Can someone explain how to remedy the two late binding errors below when
Option Strict On:
Public Function ControlDataRowArrayListPaint(ByRef frm...
|
by: Dean Slindee |
last post by:
Can someone explain how to remedy the two late binding errors below when
Option Strict On:
Public Function ControlDataRowArrayListPaint(ByRef frm...
|
by: Miro |
last post by:
VB 2003 and Im still new to vb, so i hope i can explain this as best I can.
I have a variable defined as such: ( simple example )
Dim...
|
by: kevinwolfe |
last post by:
Hi all.
I'd like any suggestions on how I can get my data set (not a DataSet)
bound to a couple of controls on a form. Let me start by...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
| |