Results 1 to 4 of 4

Thread: [RESOLVED] array to combobox

  1. #1

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Resolved [RESOLVED] array to combobox

    hey all...

    a quick question regarding binding an array to comboboxes.

    i have mutilple comboboxes which will contain the hours and minutes

    so i've create 2 arrays, one containing the hours and one containing the minutes

    Code:

    Dim l_hours As String() = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"}



    so i want to assign the hours to several comboboxes. I do this by:
    Code:

    cbo1.DataSource = l_hours
    cbo1.SelectedIndex = -1
    cbo2.DataSource = l_hours
    cbo2.SelectedIndex = -1


    however whatever value i select in cbo1 it is set in cbo2 aswell.
    eg: i select 10 from the dropdown in cbo1, when this is selected 10 becomes the selected value in cbo2.

    can someone explain this to me... there something im not doing right here
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: array to combobox

    I realy don't know why it does that but i guess the reason is that you bind the same object to the two comboboxes. If you don't want it hapen then you need to have two diferent array objects bind to the controls.
    vb Code:
    1. Public Class Form1
    2.     Dim l_hours1() As String = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"}
    3.     Dim l_hours2(l_hours1.Length - 1) As String
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Me.ComboBox1.DataSource = l_hours1
    7.         Me.ComboBox1.SelectedIndex = -1
    8.         Array.Copy(l_hours1, l_hours2, l_hours1.Length)
    9.         Me.ComboBox2.DataSource = l_hours2
    10.         Me.ComboBox2.SelectedIndex = -1
    11.     End Sub
    12. End Class

  3. #3
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: array to combobox

    You will need to 'reset' the binding context or better said, assign a different binding manager to each combobox:
    Code:
            cbo1.BindingContext = New BindingContext()
            cbo1.DataSource = l_hours
    
            cbo2.BindingContext = New BindingContext()
            cbo2.DataSource = l_hours
    Or you can simply use AddRange.
    Code:
            cbo1.Items.AddRange(l_hours)
            cbo1.SelectedIndex = -1
    
            cbo2.Items.AddRange(l_hours)
            cbo2.SelectedIndex = -1
    VB 2005, Win Xp Pro sp2

  4. #4

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: array to combobox

    the first one works perfectly as im doing this on the compact framework

    cheers half
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width