Problem with programmatically adding combo boxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Y2RtdW5veg==?=

    #1

    Problem with programmatically adding combo boxes

    I have created a sub routine to programmaticall y add an indefinite number of
    combo boxes to a form. The combo boxes are bound in code to a view for
    displaying the available options. This routine works good until you add the
    second combo box. At that point it changes the first combo box's selected
    item. A third changes the second and first and so on. I've tried setting
    the combo box name and tag to a unique name, but that doesn't help. How can
    I make each iteration behave separately?

    Here's the code for the routine:
    Private Sub SetNewCombo(ByV al CControl As Control, ByVal ControlName As
    String, ByVal Datafield As String, ByVal DisplayField As String, Optional
    ByVal SourceField As String = "", Optional ByVal DataSource As DataView =
    Nothing, Optional ByVal DefaultValue As String = "")

    Try
    Dim lCombo As New ComboBox

    With lCombo
    .Name = ControlName 'Predefined as "Filter1Com bo" and the
    number of the added control
    .Font = gFont
    .Size = New Size(140, 20)
    .Location = New Point(6, mintHeight)
    .DisplayMember = DisplayField
    .ValueMember = Datafield
    .Tag = ControlName
    .Visible = True

    Dim lCol1, lCol2 As New DataColumn

    lCol1.DefaultVa lue = DisplayField
    lCol2.DefaultVa lue = Datafield

    .Items.Add(lCol 1)
    .Items.Add(lCol 2)

    'Set Data
    .DataSource = DataSource

    'Set default value
    If DefaultValue.Le ngth 0 Then
    .SelectedValue = DefaultValue
    End If

    End With

    CControl.Contro ls.Add(lCombo)

    Catch ex As Exception
    MsgBox("SetNewC ombo: " & ex.Message)

    End Try

    End Sub

    As I stated, the creation of the controls works great, but after selecting
    the value and you add another combo box, it resets the previous box's value
    to the new box value. What am I missing?

    --
    Cindy
  • Cor Ligthert[MVP]

    #2
    Re: Problem with programmaticall y adding combo boxes

    Hi,

    Most probably are you using all the same the build in dataview from the
    datatable (the defaultview).

    This is a common asked question, just create for every combobox a seperate
    New dataview and you will se it working as you want.

    Cor


    "cdmunoz" <cdmunoz@discus sions.microsoft .comschreef in bericht
    news:164BE588-3CA4-4BAF-861C-41C374391728@mi crosoft.com...
    >I have created a sub routine to programmaticall y add an indefinite number
    >of
    combo boxes to a form. The combo boxes are bound in code to a view for
    displaying the available options. This routine works good until you add
    the
    second combo box. At that point it changes the first combo box's selected
    item. A third changes the second and first and so on. I've tried setting
    the combo box name and tag to a unique name, but that doesn't help. How
    can
    I make each iteration behave separately?
    >
    Here's the code for the routine:
    Private Sub SetNewCombo(ByV al CControl As Control, ByVal ControlName As
    String, ByVal Datafield As String, ByVal DisplayField As String, Optional
    ByVal SourceField As String = "", Optional ByVal DataSource As DataView =
    Nothing, Optional ByVal DefaultValue As String = "")
    >
    Try
    Dim lCombo As New ComboBox
    >
    With lCombo
    .Name = ControlName 'Predefined as "Filter1Com bo" and
    the
    number of the added control
    .Font = gFont
    .Size = New Size(140, 20)
    .Location = New Point(6, mintHeight)
    .DisplayMember = DisplayField
    .ValueMember = Datafield
    .Tag = ControlName
    .Visible = True
    >
    Dim lCol1, lCol2 As New DataColumn
    >
    lCol1.DefaultVa lue = DisplayField
    lCol2.DefaultVa lue = Datafield
    >
    .Items.Add(lCol 1)
    .Items.Add(lCol 2)
    >
    'Set Data
    .DataSource = DataSource
    >
    'Set default value
    If DefaultValue.Le ngth 0 Then
    .SelectedValue = DefaultValue
    End If
    >
    End With
    >
    CControl.Contro ls.Add(lCombo)
    >
    Catch ex As Exception
    MsgBox("SetNewC ombo: " & ex.Message)
    >
    End Try
    >
    End Sub
    >
    As I stated, the creation of the controls works great, but after selecting
    the value and you add another combo box, it resets the previous box's
    value
    to the new box value. What am I missing?
    >
    --
    Cindy

    Comment

    • =?Utf-8?B?Y2RtdW5veg==?=

      #3
      Re: Problem with programmaticall y adding combo boxes

      Yes, I am. I had wondered if that was the problem. I will try that. Thanks.
      I'll let you know if it works!
      --
      Cindy


      "Cor Ligthert[MVP]" wrote:
      Hi,
      >
      Most probably are you using all the same the build in dataview from the
      datatable (the defaultview).
      >
      This is a common asked question, just create for every combobox a seperate
      New dataview and you will se it working as you want.
      >
      Cor
      >
      >
      "cdmunoz" <cdmunoz@discus sions.microsoft .comschreef in bericht
      news:164BE588-3CA4-4BAF-861C-41C374391728@mi crosoft.com...
      I have created a sub routine to programmaticall y add an indefinite number
      of
      combo boxes to a form. The combo boxes are bound in code to a view for
      displaying the available options. This routine works good until you add
      the
      second combo box. At that point it changes the first combo box's selected
      item. A third changes the second and first and so on. I've tried setting
      the combo box name and tag to a unique name, but that doesn't help. How
      can
      I make each iteration behave separately?

      Here's the code for the routine:
      Private Sub SetNewCombo(ByV al CControl As Control, ByVal ControlName As
      String, ByVal Datafield As String, ByVal DisplayField As String, Optional
      ByVal SourceField As String = "", Optional ByVal DataSource As DataView =
      Nothing, Optional ByVal DefaultValue As String = "")

      Try
      Dim lCombo As New ComboBox

      With lCombo
      .Name = ControlName 'Predefined as "Filter1Com bo" and
      the
      number of the added control
      .Font = gFont
      .Size = New Size(140, 20)
      .Location = New Point(6, mintHeight)
      .DisplayMember = DisplayField
      .ValueMember = Datafield
      .Tag = ControlName
      .Visible = True

      Dim lCol1, lCol2 As New DataColumn

      lCol1.DefaultVa lue = DisplayField
      lCol2.DefaultVa lue = Datafield

      .Items.Add(lCol 1)
      .Items.Add(lCol 2)

      'Set Data
      .DataSource = DataSource

      'Set default value
      If DefaultValue.Le ngth 0 Then
      .SelectedValue = DefaultValue
      End If

      End With

      CControl.Contro ls.Add(lCombo)

      Catch ex As Exception
      MsgBox("SetNewC ombo: " & ex.Message)

      End Try

      End Sub

      As I stated, the creation of the controls works great, but after selecting
      the value and you add another combo box, it resets the previous box's
      value
      to the new box value. What am I missing?

      --
      Cindy
      >

      Comment

      • =?Utf-8?B?Y2RtdW5veg==?=

        #4
        Re: Problem with programmaticall y adding combo boxes

        That worked. Thank you so much.
        --
        Cindy


        "cdmunoz" wrote:
        Yes, I am. I had wondered if that was the problem. I will try that. Thanks.
        I'll let you know if it works!
        --
        Cindy
        >
        >
        "Cor Ligthert[MVP]" wrote:
        >
        Hi,

        Most probably are you using all the same the build in dataview from the
        datatable (the defaultview).

        This is a common asked question, just create for every combobox a seperate
        New dataview and you will se it working as you want.

        Cor


        "cdmunoz" <cdmunoz@discus sions.microsoft .comschreef in bericht
        news:164BE588-3CA4-4BAF-861C-41C374391728@mi crosoft.com...
        >I have created a sub routine to programmaticall y add an indefinite number
        >of
        combo boxes to a form. The combo boxes are bound in code to a view for
        displaying the available options. This routine works good until you add
        the
        second combo box. At that point it changes the first combo box's selected
        item. A third changes the second and first and so on. I've tried setting
        the combo box name and tag to a unique name, but that doesn't help. How
        can
        I make each iteration behave separately?
        >
        Here's the code for the routine:
        Private Sub SetNewCombo(ByV al CControl As Control, ByVal ControlName As
        String, ByVal Datafield As String, ByVal DisplayField As String, Optional
        ByVal SourceField As String = "", Optional ByVal DataSource As DataView =
        Nothing, Optional ByVal DefaultValue As String = "")
        >
        Try
        Dim lCombo As New ComboBox
        >
        With lCombo
        .Name = ControlName 'Predefined as "Filter1Com bo" and
        the
        number of the added control
        .Font = gFont
        .Size = New Size(140, 20)
        .Location = New Point(6, mintHeight)
        .DisplayMember = DisplayField
        .ValueMember = Datafield
        .Tag = ControlName
        .Visible = True
        >
        Dim lCol1, lCol2 As New DataColumn
        >
        lCol1.DefaultVa lue = DisplayField
        lCol2.DefaultVa lue = Datafield
        >
        .Items.Add(lCol 1)
        .Items.Add(lCol 2)
        >
        'Set Data
        .DataSource = DataSource
        >
        'Set default value
        If DefaultValue.Le ngth 0 Then
        .SelectedValue = DefaultValue
        End If
        >
        End With
        >
        CControl.Contro ls.Add(lCombo)
        >
        Catch ex As Exception
        MsgBox("SetNewC ombo: " & ex.Message)
        >
        End Try
        >
        End Sub
        >
        As I stated, the creation of the controls works great, but after selecting
        the value and you add another combo box, it resets the previous box's
        value
        to the new box value. What am I missing?
        >
        --
        Cindy

        Comment

        Working...