How to make a listbox automatically update when the source (list) is changed - vb.net

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

Related

How to remove the last item in the list(Of) in vb.net

I have a windows form application which added string into list of collection. This can be done by input the string into the textbox then click 'add' button, and the list will display in a listbox.
Now, I want to delete the last item in the list collection & the listbox.
Below are the code snippett that I have done
Public strList As List(Of String) = New List(Of String)
'add string to list
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TxtBox.Text <> "" Then
strList.Add(TxtBox.Text)
TxtBox.Clear()
End If
lstItem.Items.Clear()
strList.ForEach(AddressOf ListItem)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
lstItem.Items.Clear()
strList.ForEach(AddressOf ListItem)
End Sub
'Add item into list
Public Sub ListItem(s As String)
lstItem.Items.Add(s)
'lstItem.Sorted = True
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
strList.ToList.ForEach(AddressOf DeleteItem)
End Sub
'Delete item
Public Sub DeleteItem(s As String)
For i = 0 To strList.Count
lstItem.Items.RemoveAt(strList.Count - 1)
i = i + 1
Next
End Sub
as you can see, in the sub DeleteItem, i try to delete the last item of the list collection by clicking 'delete button'. but the error says Additional information: InvalidArgument=Value of '1' is not valid for 'index'.
can anyone help me on this? thank you.
What you really ought to do is use a BindingList(Of String) and bind it to the ListBox. That way, you only have to deal with one list. Adding and removing against the underlying BindingList will automatically affect the ListBox:
Private items As New BindingList(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DataSource = items
End Sub
Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
items.Add(TextBox1.Text)
End Sub
Private Sub deleteSelectedButton_Click(sender As Object, e As EventArgs) Handles deleteSelectedButton.Click
items.RemoveAt(ListBox1.SelectedIndex)
End Sub
Private Sub deleteLastButton_Click(sender As Object, e As EventArgs) Handles deleteLastButton.Click
items.RemoveAt(items.Count - 1)
End Sub

How to make it simple

My code:
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
PictureBox1.Enabled = False
End Sub
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
PictureBox2.Left = "90"
End Sub
How can I simply change the name of PictureBox1 and PictureBox2?. Example I just need to change their name like:
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
I.Enabled = False
End Sub
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
I.Left = "90"
End Sub
'I change their name to "I"
You can do that from the properties window. All you need to do is:
have the object that you want to change its name changed active by clicking on it.
on the properties window search for (Name) and change it to the name that you want your object to have i.e I but remember that your names should be clear and easy to understand.
Remember that you can not have two different objects the same name in one form you will therefore have to give them different names

vb.net Find and REMOVE a line in a textbox

I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class

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.
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

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