Results 1 to 3 of 3

Thread: [RESOLVED] Getting bound ComboBox selected item text

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Resolved [RESOLVED] Getting bound ComboBox selected item text

    Hi Folks

    I have a Combobox populated with MAC address strings from a SQL server DB (using Entity Framwork)

    VB Code:
    1. Public Class Form1
    2.     Dim myContext As New MySqlDataEntities
    3.     Dim burnRackBay As New BurnRackBay
    4.  
    5.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    6.         ComboBox1.DataSource = myContext.BurnRackBays
    7.         ComboBox1.DisplayMember = "Mac"
    8.         ComboBox1.SelectedIndex = -1
    9.     End Sub

    However when getting the text of the selected item to use in a new L2E query the only way that seems to work is to use ComboBox.Text

    VB Code:
    1. Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    2.         If ComboBox1.SelectedIndex >= 0 Then
    3.             Label4.Text = ComboBox1.Text
    4.         End If
    5.     End Sub

    But when I do this, when the form loads the label text is immediately set to the first item in the list instead of just being left blank, as the ComboBox is (as I set its index to -1 in Form1.Load)

    Is there a better way of getting the selected string, that is the *actual* string *currently* selected

    EDIT: Just noted that this does not occur if I remove the If from the selectedIndexChanged event... does this mean that the event is firing on form load but before the form.load event sub runs?
    Thanks

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Getting bound ComboBox selected item text

    Just noted that this does not occur if I remove the If from the selectedIndexChanged event... does this mean that the event is firing on form load but before the form.load event sub runs?
    Er, not quite. If it ran before the form load it wouldn't have anything to select, would it. The default on binding is to select item 0. The event fires and the label gets the text. You then change the selected index to -1 and the event fires again but as it's -1 no change is effected in the label. I'm not really sure why you need the label to say what the combobox already says, to be honest, but the simplest solution is an Else Label4.Text = ""
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Re: Getting bound ComboBox selected item text

    Hmm, OK. I think I need to make this test projet more accurately represent my use case before I go any further...

    Thanks dunfiddlin
    Thanks

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