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
Related
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
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
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
I know from textbox to listbox you can use something like:
listBox1.Items.Add(textBox1.Text)
But how would this work between two listboxes? Thanks.
Assuming the SelectionMode is set to One, and you want to Copy the selected item:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ListBox1.SelectedIndex <> -1 Then
ListBox2.Items.Add(ListBox1.SelectedItem)
End If
End Sub
If you want to Move the selected item, then:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ListBox1.SelectedIndex <> -1 Then
ListBox2.Items.Add(ListBox1.SelectedItem)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End If
End Sub
when i try to show dialog of one of my forms it displays this error all other forms work perfectly i have tried to copy the code into another form happened the same
here is the error:
A first chance exception of type 'System.InvalidOperationException'
occurred in hyper market system.exe
Additional information: An error occurred creating the form. See
Exception.InnerException for details. The error is: Object reference
not set to an instance of an object.
If there is a handler for this exception, the program may be safely continued.
here is all my code
Public Class farm
Dim inifile As New IniFile(myfiles & "\system.ini")
Dim myfiles As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\HMsystem"
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Dim Count As Integer = 0
Dim total As Long = 0
Dim productnum As String = TextBox1.Text
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim productnum As String = TextBox1.Text
Dim num As String = TextBox2.Text
Dim itemname As String = inifile.GetString("productname", productnum, "غير موجود")
Dim price As String = inifile.GetString("productprice", productnum, "غير موجود")
ListView1.Items.Add(productnum)
ListView1.Items(Count).SubItems.Add(itemname)
ListView1.Items(Count).SubItems.Add(num)
ListView1.Items(Count).SubItems.Add(price)
total += price
Count += 1
Dim a As String = inifile.GetString("productquan", productnum, "0")
Dim itemquannow As String = inifile.GetString("productquan", productnum, "0")
If itemquannow <= 5 Then
Else
MsgBox("لم يبق الا 5 من هذا المنتج")
End If
inifile.WriteInteger("productquan", productnum, a - num)
MsgBox("تم الاضافة")
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label4.Text = total & " السعر النهائي"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListView1.Clear()
TextBox1.Clear()
TextBox2.Clear()
Count = 0
total = 0
MsgBox("تم الشراء بنجاح")
End Sub
Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
End Class
When you get an error message like that, i.e. "An error occurred creating the form", it almost always means the same basic issue: you have an event handler that is being raised because of a property value set in the designer and that event handler assumes that the user has made that change after the form has been displayed. For instance, if you set the Text property of a TextBox in the designer then that's going to raise the TextChanged event. If you have handled that event then your event handler is going to be executed during the initialisation of the form, before it has been displayed to the user. If you assume that, for instance, an item is selected in a ComboBox then you're in trouble because there will be no such selection.
As the error message states, look at the InnerException, which will tell you exactly where the original exception was thrown. That will tell you which event handler is the issue and you can then look at the code in that method and determine what would cause an issue if the form had not yet been displayed. If in doubt, update your question with the code of that event handler and tell us where the exception was thrown, which the stack trace will tell you.