Results 1 to 8 of 8

Thread: Confused ComboBox

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question Confused ComboBox

    This is one weird glitch. I've searched the forums and the web, and still i have not found anyone else that has encountered this.

    On a WinForm, drop on 2 ComboBoxes, and then place this code in behind it:
    VB Code:
    1. Dim bFormLoading As Boolean = True
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim arText() As String = {"", "Red", "White", "Blue"}
    5.         ComboBox1.DataSource = arText
    6.         ComboBox2.DataSource = arText
    7.         'MsgBox("form_load")
    8.         bFormLoading = False
    9.     End Sub
    10.  
    11.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    12.         If bFormLoading = False Then MsgBox("ComboBox1")
    13.     End Sub
    14.  
    15.     Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
    16.         If bFormLoading = False Then MsgBox("ComboBox2")
    17.     End Sub
    When you run the program and select something from either combo box, the code for both fires!

    I think it's some kind of weird situation where .NET is re-drawing the form, and thus it is re-selecting an item in each combo box. But i'm not sure.

    It's the weirdest thing i've ever seen.
    ~Peter


  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    It is not related to form painting event. As obvious in your code, you have bound tow comboboxes to one datasource and when you change one combobox the underlying binding manager position changes, so it fires the other combobox.

  3. #3

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question

    I accept your answer, but logically it doesn't make sense. At least from a programming aspect.

    In this case you want to have the user select two different colours. So you populate the combo boxes with the colours available in the array.

    Having to create a second array with the same values defeats the purpose of having the array, since you lose the single place to update all available elements.

    Personally i think this is a bug in the .NET code.

    I created another array and it works, so thanks a lot for your help!
    ~Peter


  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Well, am not that experienced to judge if it is a bug or not, but IMHO its not, its just the nature of binding i guess. Why dont you just populate your comboboxes with the array instead of binding to that array?

  5. #5

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question

    You say i should populate instead of binding.... Can you give me an example of what you are thinking?
    ~Peter


  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Too easy
    VB Code:
    1. Dim arText() As String = {"", "Red", "White", "Blue"}
    2.         ComboBox1.Items.AddRange(arText)
    3.         ComboBox2.Items.AddRange(arText)

  7. #7

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Talking

    Well what do you know..... It worked!

    I had no idea you could do that. All the examples i'd seen used the DataSource property.

    I have learned some very important today. Thanks Lunatic3!
    ~Peter


  8. #8
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    You're most welcome!

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