By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,326 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,326 IT Pros & Developers. It's quick & easy.

ComboBox refuses to display blank on setting selectedindex=-1

100+
P: 106
Hi,

I am Designing a Vehicle Parts Database.
I have a first combobox 'cboVehiclemake' that displays list of all Vehicles. The second combobox 'cboVehicleModel' relies on the first cbo display all the related models.

when I clear all fields to add new data, second combo refuses to blank out. I am not sure why.

There are other controls and combos that behave as desired.
I have filtered and shown below, the code related to the two combos.

Expand|Select|Wrap|Line Numbers
  1. Private m_MakeID As Decimal
  2.  
Expand|Select|Wrap|Line Numbers
  1. 'Form_Load
  2. InitcboMake()
  3. InitcboModel()
  4.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub InitcboMake()
  2. 'First Combobox
  3. Dim dc As ACCDataContext = New ACCDataContext(g_strConnectionString)
  4.  
  5. Dim MakesList = From ml In dc.tblVehicleMakes _
  6.                 Order By ml.Descr Ascending _
  7.                 Select ml
  8.  
  9.  If MakesList IsNot Nothing Then
  10.     cboVehicleMake.DataSource = MakesList
  11.     cboVehicleMake.DisplayMember = "Descr"
  12.     cboVehicleMake.ValueMember = "ID"
  13.  End If
  14.  
  15. 'set to blank
  16.  cboVehicleMake.SelectedIndex = -1
  17. End Sub
  18.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboVehicleMake_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboVehicleMake.SelectedIndexChanged
  2.  
  3.   m_MakeID = cboVehicleMakeID.SelectedValue
  4.   InitcboModel()
  5.  
  6. End Sub
  7.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub InitcboModel()
  2.  
  3. 'Second combobox
  4. Dim dc As ACCDataContext = New ACCDataContext(g_strConnectionString)
  5.  
  6. Dim ModelsList = From ml In dc.tblVehicleModels _
  7.                  Where ml.VehicleMakeID = m_MakeID _
  8.                  Select ml
  9.  
  10.   If ModelsList IsNot Nothing Then
  11.      cboVehicleModelID.DataSource = ModelsList
  12.      cboVehicleModelID.DisplayMember = "Descr"
  13.      cboVehicleModelID.ValueMember = "ID"
  14.   End If
  15.  
  16. 'set to blank
  17.   cboVehicleModelID.SelectedIndex = -1
  18.  
  19. End sub
  20.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub ClearControls()
  2.  
  3. cboVehicleMakeID.SelectedIndex = -1
  4. m_MakeID = -1
  5. InitcboModel()
  6.  
  7. End Sub
  8.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  2.  
  3.         ClearControls()
  4.  
  5. End Sub
  6.  
Dec 21 '13 #1
Share this Question
Share on Google+
1 Reply


100+
P: 299
This may be a very vague answer, but I'm not going to make it complicated since it looks like you know what you're doing and you asked this 3 weeks ago... but it looks like you just need to say: "If combobox1 selected index is changed, combobox2.items.clear()"
Jan 14 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.