Jump to content

Combo Box Problem

CripDawg
Go to solution Solved by madknight3,

I think what you want is to clear, or reset, the text of the unused boxes.

 

So in your first function where you selected an item you want to do

Model.Items.Clear()Model.Text = "" // or "Select Model"Series.Items.Clear()Series.Text = "" // or "Select Series"

Then when your model is selected, you just reset the series.

 

That way you always make sure the next combo box being selected is always reset.

 

You can also make that into a method. I've used an optional parameter so you can choose to provide the method with a string, like "Select Model". But if you don't want to, it'll just default to the empty string.

Public Sub ResetComboBox(ByVal box As ComboBox, Optional ByVal text As String = "")    box.Items.Clear()    box.Text = textEnd Sub

Then

// Instead ofMake.Text = "Select Make"Model.Items.Clear()Model.Text = "Select Model"Series.Items.Clear()Series.Text = "Select Series"// You can useResetComboBox(Make, "Select Make")ResetComboBox(Model, "Select Model")ResetComboBox(Series, "Select Series")// or if you want to clear the text instead of set itResetComboBox(Make)ResetComboBox(Model)ResetComboBox(Series)

my car buyer work by using 3 combo boxes and changing the items that are selectable based on the value of the previous combo box e.g. if ford is selected only ford models will appear in the model combo box. however the selected model (Not the entire list of items) remain so say i choose a Toyota as a make and one of the Toyota models i then change the make to ford the Toyota model will stay in the combo box until i select a ford model from the list.

 

Hopefully this is not to confusing....

 

Public Class Home       Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Make.SelectedIndexChanged        Dim comboBox As ComboBox = CType(sender, ComboBox)        Dim index As Integer = comboBox.SelectedIndex        Dim item As String = comboBox.Items(index)         Model.Items.Clear()         If item.Equals("Nissan") Then            Model.Items.Add("Skyline")            Model.Items.Add("Silvia")        ElseIf item.Equals("Toyota") Then            Model.Items.Add("Supra")            Model.Items.Add("86")        ElseIf item.Equals("Mitsubishi") Then            Model.Items.Add("Evolution")        ElseIf item.Equals("Subaru") Then            Model.Items.Add("Impreza")            Model.Items.Add("Liberty")        End If    End Sub     Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Model.SelectedIndexChanged        Dim comboBox As ComboBox = CType(sender, ComboBox)        Dim index As Integer = comboBox.SelectedIndex        Dim item As String = comboBox.Items(index)         Series.Items.Clear()        If item.Equals("Skyline") Then            Series.Items.Add("R32")            Series.Items.Add("R33")            Series.Items.Add("R34")        ElseIf item.Equals("Silvia") Then            Series.Items.Add("S13")            Series.Items.Add("S14")            Series.Items.Add("S15")        ElseIf item.Equals("Supra") Then            Series.Items.Add("JZA80")            Series.Items.Add("JZA70")        ElseIf item.Equals("Evolution") Then            Series.Items.Add("IX")            Series.Items.Add("X")        ElseIf item.Equals("Impreza") Then            Series.Items.Add("WRX STi")            Series.Items.Add("RS")            End If    End Sub      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        If Make.Text = ("Select Make") Then            MessageBox.Show("Please Select A Make, Model And Series", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)        ElseIf Model.Text = ("Select Model") Then            MessageBox.Show("Please Select A Model And Series", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)        ElseIf Series.Text = ("Select Series") Then            MessageBox.Show("Please Select A Series", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)        Else : Results.Show()        End If    End Sub      Private Sub AboutThisSoftwareToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutThisSoftwareToolStripMenuItem1.Click        About.Show()    End Sub     Private Sub NewSearchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewSearchToolStripMenuItem.Click        Make.Text = "Select Make"        Model.Items.Clear()        Model.Text = "Select Model"        Series.Items.Clear()        Series.Text = "Select Series"     End Sub   End Class
Link to comment
Share on other sites

Link to post
Share on other sites

I think what you want is to clear, or reset, the text of the unused boxes.

 

So in your first function where you selected an item you want to do

Model.Items.Clear()Model.Text = "" // or "Select Model"Series.Items.Clear()Series.Text = "" // or "Select Series"

Then when your model is selected, you just reset the series.

 

That way you always make sure the next combo box being selected is always reset.

 

You can also make that into a method. I've used an optional parameter so you can choose to provide the method with a string, like "Select Model". But if you don't want to, it'll just default to the empty string.

Public Sub ResetComboBox(ByVal box As ComboBox, Optional ByVal text As String = "")    box.Items.Clear()    box.Text = textEnd Sub

Then

// Instead ofMake.Text = "Select Make"Model.Items.Clear()Model.Text = "Select Model"Series.Items.Clear()Series.Text = "Select Series"// You can useResetComboBox(Make, "Select Make")ResetComboBox(Model, "Select Model")ResetComboBox(Series, "Select Series")// or if you want to clear the text instead of set itResetComboBox(Make)ResetComboBox(Model)ResetComboBox(Series)
Link to comment
Share on other sites

Link to post
Share on other sites

 

I think what you want is to clear, or reset, the text of the unused boxes.

 

So in your first function where you selected an item you want to do

Model.Items.Clear()Model.Text = "" // or "Select Model"Series.Items.Clear()Series.Text = "" // or "Select Series"

Then when your model is selected, you just reset the series.

 

That way you always make sure the next combo box being selected is always reset.

 

You can also make that into a method. I've used an optional parameter so you can choose to provide the method with a string, like "Select Model". But if you don't want to, it'll just default to the empty string.

Public Sub ResetComboBox(ByVal box As ComboBox, Optional ByVal text As String = "")    box.Items.Clear()    box.Text = textEnd Sub

Then

// Instead ofMake.Text = "Select Make"Model.Items.Clear()Model.Text = "Select Model"Series.Items.Clear()Series.Text = "Select Series"// You can useResetComboBox(Make, "Select Make")ResetComboBox(Model, "Select Model")ResetComboBox(Series, "Select Series")// or if you want to clear the text instead of set itResetComboBox(Make)ResetComboBox(Model)ResetComboBox(Series)

so on the model sub i put this

  Model.Items.Clear()        Model.Text = "Select Model"        Series.Text = "Select Series"
Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, here's what it would look like in your code. I've put ... for the code I've skipped over.


Public Class Home    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Make.SelectedIndexChanged        ...         // A Make item is selected so make sure the model and series         // combo boxes are cleared and ready for selection        Model.Items.Clear()        Model.Text = "Select Model"        Series.Items.Clear()        Series.Text = "Select Series"         ...    End Sub     Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Model.SelectedIndexChanged        ...         // A Model item is selected so make sure the series combo box        // is cleared and ready for selection        Series.Items.Clear()        Series.Text = "Select Series"        ...    End Sub     ...End Class
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×