Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27206

Getting bound ComboBox selected item text

$
0
0
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?

Viewing all articles
Browse latest Browse all 27206

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>