dynamic controls databinding
Hi guys,
I'm having a tough one here (i think).
The problem is the following: on my form there are dynamically created comboboxes and textboxes. Each combobox has a corresponding textbox. I wrote the following code to accomplish this:
VB Code:
'in load event of form
Dim enumerator As IEnumerator = strArrCmbDossier.GetEnumerator
While enumerator.MoveNext
Me.AddCmbDossier(enumerator.Current)
aantal = aantal + 1
End While
'creation of hashtable
hashtable()
Private Sub AddCmbDossier(ByVal CmbDossierName As String)
Dim CmbDossier As New ComboBox
Dim CmbNaam As String
CmbNaam = CmbDossierName
'each combobox has it's own bindingcontext
CmbDossier.BindingContext = New BindingContext
CmbDossier.Name = CmbNaam
CmbDossier.Left = Me.LblM1.Left + 10
CmbDossier.Top = Me.LblM1.Top + number * 4
CmbDossier.DataSource = Me.DSDossier
CmbDossier.DisplayMember = "tblDossier.DOSSNR"
Me.Controls.Add(CmbDossier)
AddHandler CmbDossier.Click, AddressOf CmbDossierM1_Click
'function to create the corresponding textboxes
Me.AddtxtDossier()
End Sub
Private Sub AddtxtDossier()
Dim txtDossier As New TextBox
Dim txtNaam As String
Dim target As Control
For Each target In Controls
If (TypeOf target Is ComboBox = True) Then
txtDossier.Name = "txtbox" & target.Name
txtDossier.Left = Me.LblM1.Left + target.Width + 15
txtDossier.Top = Me.LblM1.Top + number * 4
Me.Controls.Add(txtDossier)
txtDossier.BindingContext = target.BindingContext
txtDossier.DataBindings.Add("text", target, "Name")
End If
Next
End Sub
Each combobox is binded to the same datasource (DSDossier) but has a separate bindingcontext to avoid the synchronisation between the comboboxes when the user selects a value in one combobox.
Now comes the problem: As I said the comboboxes have a corresponding textbox in which the corresponding text for the selected value in the corresponding combobox has to be placed after the user clicked the combobox. THis is my problem because I don't know how to do this. Obviously I have to set the databindings of the combobox and textbox to the same, but how. I tried to put this functionality in the click event handler :
VB Code:
Private Sub CmbDossierM1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmbDossierM1.Click
'Me.Controls.Item(e.item).DataBindings.Add("text", DSdossier, "tblDossier.DOSSIERNAAM")
Dim b As ComboBox = DirectCast(sender, ComboBox)
Dim naamtxtbox As String
If (myControls.ContainsKey(b.Name)) Then
naamtxtbox = "txt" & b.Name
myControls.Item(naamtxtbox).BindingContext = b.BindingContext
End If
End Sub
Where myControls is a Hashtable, so I can search on the name
VB Code:
Private Sub hashtable()
myControls = New Hashtable(Controls.Count)
Dim c As Control
For Each c In Controls
myControls.Add(c.Name, c.Name)
Next
End Sub
Anybody an idea how to accomplish this??
many thanks