-
May 29th, 2008, 10:35 AM
#1
Thread Starter
Hyperactive Member
[2005] Binding controls to an array property
Here's what I want to do; I have defined an instance of a class that has a property Arguments, which is an array. I have several comboboxes on my form that I want to bind to individual string values in this array. I tried:
Code:
cboControl1.DataBindings.Add(New Binding("Text", OrderSheet.CurrentOrder, "Arguments(1)"))
This does not generate any errors until I want to make the combobox visible. Then I get:
Cannot bind to the property or column Arguments(1) on the DataSource.
Parameter name: dataMember
Any thoughts as to what I'm doing wrong?
-
May 29th, 2008, 09:27 PM
#2
Lively Member
Re: [2005] Binding controls to an array property
Is this what you want?
To bind the "Text" property of a combobox, textbox, label etc. to a specific element in an array......
Code:
Dim myArray() As String = {"Zero", "One", "Two", "Three"}
' Bind to a specific element in an array
Me.ComboBox1.DataBindings.Add(New Binding("Text", myArray, ""))
Me.BindingContext(myArray).Position += 2
'or
Me.Label1.DataBindings.Add(New Binding("Text", myArray, ""))
Me.BindingContext(myArray).Position += 1
Code:
'
Dim test As New TestClass
Me.Label3.DataBindings.Add(New Binding("Text", test.StrArray, ""))
Me.BindingContext(test.StrArray).Position += 1
'
Public Class TestClass
Private _strArr() As String = {"A", "B", "C"}
Public Property StrArray() As String()
Get
Return _strArr
End Get
Set(ByVal value As String())
_strArr = value
End Set
End Property
End Class
Be aware of the behaviour of the BindingContext.
Hope this helps
Last edited by Sunglasses Ron; May 30th, 2008 at 01:02 AM.
Microsoft Visual Basic 2008
-
May 30th, 2008, 02:20 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] Binding controls to an array property
Thanks Ron, it's not exactly what I wanted, but it's a good start I think.
The problem with your code for my application is that I have multiple comboboxes on my form, and I want each to bind to another item in the array. So cboControl1 should be bound to OrderSheet.CurrentOrder.Arguments(1) and cboControl2 should be bound to OrderSheet.CurrentOrder.Arguments(2).
What I want to achieve is the following:
The OrderSheet object contains an array of Order objects, which each contain an array of strings called Arguments. I want my comboboxes to bind to the individual items in the Arguments array, so that when I change the current order (by simply stating CurrentOrder = Orders(16) or similar) the comboboxes will show the values of the Orders(16).Arguments array.
Hope this makes sense!
-
May 30th, 2008, 04:47 AM
#4
Lively Member
Re: [2005] Binding controls to an array property
Hopefully this is a step closer......
Code:
Dim myArray() As String = {"Zero", "One", "Two", "Three"}
Me.ComboBox1.BindingContext = New BindingContext()
Me.ComboBox1.DataBindings.Add(New Binding("Text", myArray, ""))
Me.ComboBox1.BindingContext(myArray).Position = 1
Me.ComboBox2.BindingContext = New BindingContext()
Me.ComboBox2.DataBindings.Add(New Binding("Text", myArray, ""))
Me.ComboBox2.BindingContext(myArray).Position = 2
Me.ComboBox3.BindingContext = New BindingContext()
Me.ComboBox3.DataBindings.Add(New Binding("Text", myArray, ""))
Me.ComboBox3.BindingContext(myArray).Position = 3
The comboboxes are now bound to the same data source, but with a different binding manager.
cheers
Microsoft Visual Basic 2008
-
May 30th, 2008, 06:03 AM
#5
Thread Starter
Hyperactive Member
Re: [2005] Binding controls to an array property
Thanks Ron,
That does the trick! One more question though, I create these bindings from my frmForm_Load event. At the end of that sub, the positions for the BindingContexts are nicely set to the values I want. When I click a menu item however, they are all reset to 0. I put a breakpoint on the first line of the event handler for the menu item, and at that point they have already been changed.
Any ideas?
-
May 30th, 2008, 08:51 PM
#6
Lively Member
Re: [2005] Binding controls to an array property
Not sure what you are trying to do.
Can you post a bit more detail?
Cheers
Microsoft Visual Basic 2008
-
May 31st, 2008, 10:10 AM
#7
Thread Starter
Hyperactive Member
Re: [2005] Binding controls to an array property
Well, what I'm trying to do hasn't changed. I still want each of my comboboxes to be bound to a single value in the array. I create the bindings in the Form_Load event, because the object I want to bind to is created there as well. When I pause the program at the end of the Form_Load event, all bindings have been correctly created, and the position property is correctly set to 1 for combobox 1, 2 for combobox 2, etc.
If I then resume the program and click a (random) menu item, and pause the program again at the first line of the event handler for that menu item, all position properties have a value of 0.
-
Jun 1st, 2008, 12:50 AM
#8
Lively Member
Re: [2005] Binding controls to an array property
I can't seem to duplicate the problem that you are having.
The .Position properties remain unchanged (as they should).
All I can suggest is create a simple project with a couple of comboboxes, menustrip etc & prove that code works correctly in it's most basic form & work from there.
There must be something happening in the background that is not immediately obvious.
Microsoft Visual Basic 2008
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|