Using SelectedItem to have pre select item in combo box
-
I have a list view and when the user selects an item in the list view there is a combo box that has a list of teacher names in it. I am trying to pre select a teacher name when the user click on the item in the list view, but the teacher name is not changing when the user clicks on the item in the list view. I hard coded a selectedValue which is the teacher id to see if the teacher name would change in the combo box and it did. Here is my code. Why is this happening?
Private Sub lvwSelectLess_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles lvwSelectLess.MouseDown grpBxLessonInfo.Visible = True Dim dt As New DataTable() Dim lvwItems As New ListViewItem() Dim selection As ListViewItem = lvwSelectLess.GetItemAt(e.X, e.Y) If (selection IsNot Nothing) Then MsgBox(selection.SubItems(4).Text) cboSelectTeacher.SelectedItem = selection.SubItems(4).Text ' this si the line of code to set a pre selected item in the combo box. txtInstrument.Text = selection.SubItems(3).Text lblStartDate.Text = selection.SubItems(5).Text dtpEndDate.Text = selection.SubItems(6).Text End If End Sub
-
I have a list view and when the user selects an item in the list view there is a combo box that has a list of teacher names in it. I am trying to pre select a teacher name when the user click on the item in the list view, but the teacher name is not changing when the user clicks on the item in the list view. I hard coded a selectedValue which is the teacher id to see if the teacher name would change in the combo box and it did. Here is my code. Why is this happening?
Private Sub lvwSelectLess_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles lvwSelectLess.MouseDown grpBxLessonInfo.Visible = True Dim dt As New DataTable() Dim lvwItems As New ListViewItem() Dim selection As ListViewItem = lvwSelectLess.GetItemAt(e.X, e.Y) If (selection IsNot Nothing) Then MsgBox(selection.SubItems(4).Text) cboSelectTeacher.SelectedItem = selection.SubItems(4).Text ' this si the line of code to set a pre selected item in the combo box. txtInstrument.Text = selection.SubItems(3).Text lblStartDate.Text = selection.SubItems(5).Text dtpEndDate.Text = selection.SubItems(6).Text End If End Sub
AAGTHosting wrote:
cboSelectTeacher.SelectedItem = selection.SubItems(4).Text ' this si the line of code to set a pre selected item in the combo box.
By setting the Text property of the ComboBox, you're not actually selecting anything. All you're doing is putting text in a TextBox control. To select the item, you have to call the FindString method of the ComboBox to find the text of the item you're looking for. This will return the index of the first item that matches that string. It'll return -1 if the item is not found. Then you can set the SelectedIndex property of the ComboBox to select the item.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008