vb.net combobox hide item - vb.net

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.

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

Related

Replacing listview items with another list using combo box

Pardon for being novice at vb.net. I have a combo box and a list view. What I needed is when I change the category in the combo box and pressed 'OK', the old list added before will be replaced by a new list.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As ListViewItem
If ComboBox1.Text = "Terrestrial Ecotoxicity (Freshwater)" Then
i = ListView1.Items.Add("Water")
i.SubItems.Add("2068.030567")
i.SubItems.Add("0")
i.SubItems.Add("0")
ElseIf ComboBox1.Text = "Terrestrial Ecotoxicity (Seawater)" Then
i = ListView1.Items.Add("Dimethylamine")
i.SubItems.Add("1229.539887")
i.SubItems.Add("0.000122731")
i.SubItems.Add("0.15090266")
End if
End Sub
What should I need to add?
What you are doing there is adding a different row on each case. What you ask is to fill the listview with different items depending on the item you pick at the combobox.
Basically, you need to clear the listview on each case. I'd do something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As ListViewItem
ListView1.Items.Clear
If ComboBox1.Text = "Terrestrial Ecotoxicity (Freshwater)" Then
i = ListView1.Items.Add("Water")
i.SubItems.Add("2068.030567")
i.SubItems.Add("0")
i.SubItems.Add("0")
ElseIf ComboBox1.Text = "Terrestrial Ecotoxicity (Seawater)" Then
i = ListView1.Items.Add("Dimethylamine")
i.SubItems.Add("1229.539887")
i.SubItems.Add("0.000122731")
i.SubItems.Add("0.15090266")
End if
End Sub

How to make a listbox automatically update when the source (list) is changed

So I have a listbox with a List as a datasource. What I want is that when I add and remove items from the List, the listbox updates itself.
Right now im able to do it but in a really ugly way. What I do is remove and add the datasource in all the places that I modify the list:
For example:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
formaciones.Add(New ForDias(Formacion, NumericUpDown1.Value))
ListBox2.DataSource = Nothing
ListBox2.DataSource = formaciones
End Sub
This works, but is there any way of telling the Listbox to check again the datasource without resetting it?
Edit:
How I filter:
On the textBox text changed event:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
ListBox2.DataSource = New BindingList(Of Object)((formaciones.Where(Function(i As ForDias) i.Formacion.ToString().Contains(TextBox1.Text))).ToList())
End Sub
You need to bind a "BindingList(of ForDias)"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim formaciones As New System.ComponentModel.BindingList(Of ForDias)
formaciones.Add(New ForDias(Formacion, NumericUpDown1.Value))
ListBox2.DataSource = Nothing
ListBox2.DataSource = formaciones
End Sub

VB.NET Save Combo Box to My.Settings

Looking to save combobox items to my.settings collection. I developed a webbrowser and a combobox will be my address bar. I am attempting to save a history of visited sites.
I tried the below code and it doesnt work. It errors out with "Object reference not set to an instance of an object":
Went into settings added MyItems for the name, and then select System.Collections.Specialized.StringCollection as the data type. Then onload is the below:
For Each i As String In My.Settings.MyItems
ComboBox1.Items.Add(i)
Next
FormClosing and ive tried FormClosed: For now i put it in a button event to save it for testing
My.Settings.MyItems.Clear()
For Each i As String In ComboBox1.Items
My.Settings.MyItems.Add(i)
Next
I love this site very much! So I came back to post the correct code that will correctly save and load combobox entries to my.setting! This has been tested as working!!!
Private Sub Form1_FormClosing(
sender As Object,
e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.Categories.Clear()
For Each item In ComboBox1.Items
My.Settings.Categories.Add(item.ToString)
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In My.Settings.Categories
ComboBox1.Items.Add(item)
Next
ComboBox1.SelectedIndex = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ComboBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If ComboBox1.Items.Count > 0 Then
Dim Index As Int32 = ComboBox1.SelectedIndex
ComboBox1.Items.RemoveAt(Index)
If Index - 1 <> -1 Then
ComboBox1.SelectedIndex = Index - 1
End If
End If
End Sub

How to populate listbox with a list of all open forms

I have a form with a listbox, and I want to be able to populate it with all open forms of the same application. However, I want to be able to select an Item from the listbox, and be able to close the form associated with that item in the list box. Is this possible to do?
I found the answer to the issue. The following code works:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim myForms As FormCollection = Application.OpenForms
For Each frmName As Form In myForms
ListBox1.Items.Add(frmName.Name.ToString)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If Not ListBox1.SelectedIndex = -1 Then
Dim myForm As Form = Application.OpenForms(ListBox1.Text)
myForm.Close()
End If
End Sub
Where the code under ListBox1_SelectedIndexChanged can very easily be placed in a button.
My.Application.OpenForms is a collection of the open forms in your project. So something like:
For Each f As Form In My.Application.OpenForms
Me.SomeListBox.Items.Add(f)
Next
Then to close the selected item, it’s
DirectCast(Me.SomeListBox.SelectedItem, Form).Close()

Values are not populating in the Form

Using VB.Net (Windows Application)
In the form (called as FirstForm), i am using textbox, add-form Button, search button.
When i click the add-form button, it will give the new form (same as FirstForm)
Code for adding new form
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Dim SecondForm As New FirstForm
SecondForm.Show()
End Sub
Search Button Code
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
If FirstForm.Focus = True Then
FirstForm.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
Else
Dim SecondForm As New FirstForm
SecondForm.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End If
End Sub
The above code is working, but If i am in second Form when i click the search button and selected the value, then the value is appearing in the FirstForm textbox, it is not appearing in the SecondForm textbox.
If SecondForm is showing, the selected Value should appear in the SecondForm textbox not in the FirstForm Textbox.
How to solve this issue.
Need Vb.net code Help
Use Me - Reference variable which hold ref. of current form.
Dim frm As FirstForm
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If IsNothing(frm) OrElse frm.IsDisposed Then
frm = New FirstForm
End If
frm.Show()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End Sub
Using "me" will not solve the problem?? why are you referring to the form in a static way?
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End Sub