Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Using SelectedItem to have pre select item in combo box

Using SelectedItem to have pre select item in combo box

Scheduled Pinned Locked Moved Visual Basic
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AAGTHosting
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • A AAGTHosting

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups