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:
  1. 'in load event of form
  2. Dim enumerator As IEnumerator = strArrCmbDossier.GetEnumerator
  3.         While enumerator.MoveNext
  4.             Me.AddCmbDossier(enumerator.Current)
  5.             aantal = aantal + 1
  6.         End While
  7.  
  8. 'creation of hashtable
  9.         hashtable()
  10.  
  11.  
  12. Private Sub AddCmbDossier(ByVal CmbDossierName As String)
  13.         Dim CmbDossier As New ComboBox
  14.         Dim CmbNaam As String
  15.        
  16.         CmbNaam = CmbDossierName
  17.         'each combobox has it's own bindingcontext
  18.         CmbDossier.BindingContext = New BindingContext
  19.         CmbDossier.Name = CmbNaam
  20.         CmbDossier.Left = Me.LblM1.Left + 10
  21.         CmbDossier.Top = Me.LblM1.Top + number * 4
  22.         CmbDossier.DataSource = Me.DSDossier
  23.         CmbDossier.DisplayMember = "tblDossier.DOSSNR"
  24.         Me.Controls.Add(CmbDossier)
  25.  
  26.         AddHandler CmbDossier.Click, AddressOf CmbDossierM1_Click
  27.  
  28.         'function to create the corresponding textboxes
  29.         Me.AddtxtDossier()
  30.     End Sub
  31.  
  32.  Private Sub AddtxtDossier()
  33.         Dim txtDossier As New TextBox
  34.         Dim txtNaam As String
  35.         Dim target As Control
  36.        
  37.         For Each target In Controls
  38.             If (TypeOf target Is ComboBox = True) Then
  39.                 txtDossier.Name = "txtbox" & target.Name
  40.                 txtDossier.Left = Me.LblM1.Left + target.Width + 15
  41.                 txtDossier.Top = Me.LblM1.Top + number * 4
  42.                 Me.Controls.Add(txtDossier)
  43.                 txtDossier.BindingContext = target.BindingContext
  44.                 txtDossier.DataBindings.Add("text", target, "Name")
  45.              End If
  46.         Next
  47.     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:
  1. Private Sub CmbDossierM1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmbDossierM1.Click
  2.         'Me.Controls.Item(e.item).DataBindings.Add("text", DSdossier, "tblDossier.DOSSIERNAAM")
  3.  
  4.         Dim b As ComboBox = DirectCast(sender, ComboBox)
  5.  
  6.         Dim naamtxtbox As String
  7.        
  8.         If (myControls.ContainsKey(b.Name)) Then
  9.             naamtxtbox = "txt" & b.Name
  10.            myControls.Item(naamtxtbox).BindingContext = b.BindingContext
  11.                    End If
  12.     End Sub

Where myControls is a Hashtable, so I can search on the name

VB Code:
  1. Private Sub hashtable()
  2.         myControls = New Hashtable(Controls.Count)
  3.         Dim c As Control
  4.         For Each c In Controls
  5.             myControls.Add(c.Name, c.Name)
  6.         Next
  7.     End Sub
Anybody an idea how to accomplish this??

many thanks