user3740783
user3740783

Reputation: 27

vb.net combobox hide item

I have a combobox that when the item is selected and it's the same as a button.text in other form the button.text changes for the name that the user typed in a textbox. But if the item is different of the button.text I want to hide it, so the user can't select it or see it.

cafetariacombo is the combobox
Form3.cafetaria2.Text is the button I'm changing

  If cafetariacombo.SelectedItem = "cafetaria2" Then
                Form3.cafetaria2.Text = TextBox1.Text
                My.Settings.cafetaria2guardar = Form3.cafetaria2.Text
                My.Settings.Save()
end if

I use this to name the button, I just need to know if I can hide the combobox item.

Help me please :)

Update with some code

I inserted the list of items myself in the combobox. I solved my earlier problem, but now i need to save the state of the combobox items when I leave the form.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If cafetariacombo.SelectedItem <> Form3.cafetaria1.Text Then
        cafetariacombo.Items.Remove("cafetaria1")
    End If

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If ComboBox2.SelectedItem = Form3.cafetaria1.Text Then
        Form3.cafetaria1.Text = "cafetaria1"
        My.Settings.cafetaria1guardar = Form3.cafetaria1.Text
        My.Settings.Save()
        adicionarproduto.cafetariacombo.Items.Add("cafetaria1")
    end if
  end sub

When I remove the item from the combobox I'm in form1 and when I add the item again I'm in form2.
Just need to save the combobox with the deleted item when I leave the form1.

Upvotes: 1

Views: 4352

Answers (1)

Victor Zakharov
Victor Zakharov

Reputation: 26454

You can use a DataTable as data source, then it's as easy as changing DefaultView.RowFilter, please study this example and let me know if you have any questions:

Public Class Form1

  Private Sub Button1_Click(sender As System.Object,
                            e As System.EventArgs) Handles Button1.Click
    Dim dt As New DataTable
    dt.Columns.Add("id")
    dt.Rows.Add("1")
    dt.Rows.Add("2")
    dt.Rows.Add("3")
    ComboBox1.DisplayMember = "id"
    ComboBox1.DataSource = dt 'show all items by default
  End Sub

  Private Sub Button2_Click(sender As System.Object,
                            e As System.EventArgs) Handles Button2.Click
    DirectCast(ComboBox1.DataSource, DataTable).DefaultView.
      RowFilter = "id <> 2" 'hide item=2 from the view
  End Sub

End Class

Upvotes: 2

Related Questions