Visual Studio 2019 (vb) - issue with Reading/Writing My.Settings - vb.net

I'm just starting to develop and so there are some concepts that are not clear to me, I need you to try to understand what I'm doing wrong.
My goal is to have a series of ComboBoxes that contain some strings, for example a string is this one, the ComboBox is called TYPE:
I am storing these strings in My.Settings for my convenience and to edit them directly from the app.exe.config file (these information are stored there right?).
I'm using this code
Private Sub AdminPanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In My.Settings.TYPE
ComboBoxType.Items.Add(item)
Next
End Sub
My issue is that, I'm unable to read from My.Settings.TYPE and when I try to write into it with the following code I doesn't find any strings added into My.Settings menu neither into app.exe.config.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonTYPEAddValue.Click
ComboBoxType.Items.Add(TextBoxType.Text)
ComboBoxType.Text = TextBoxType.Text
TextBoxType.Clear()
MsgBox("Item added!")
End Sub
what is wrong?

In your code, it seems like you've used String to add/remove the ComboBoxType items. Follow the steps to achieve your requirement:
In this screenshot, you can see I've set the Type as System.Collection.Specialized.StringCollection to separate each time for future use.
Now, you may use the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Settings.tests.Add(TextBox1.Text)
My.Settings.Save()
ComboBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub AdminPanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In My.Settings.tests
ComboBox1.Items.Add(item)
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
My.Settings.tests.Remove(TextBox1.Text)
My.Settings.Save()
ComboBox1.Items.Remove(TextBox1.Text)
End
It's noticeable that you can change the variable names, they're differ a bit than yours in my code.
Form structure for the code:
On the MyBase.Load event, all the items containing in My.Settings.tests will be added using For Each loop (benefit of StringCollection).
Working example of the form [adding - removing using My.Settings]:
I hope you've got your answer.

Related

Visual Basic FolderBrowser returning no value

I was looking to memorizing a folder path to my.settings indefinitely.
I tried
Private Sub CartellaTessutiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CartellaTessutiToolStripMenuItem.Click
FolderBrowserDialog1.ShowDialog()
End Sub
Private Sub FolderBrowserDialog1_Disposed(sender As Object, e As EventArgs) Handles FolderBrowserDialog1.Disposed
My.Settings.CartellaTex = FolderBrowserDialog1.SelectedPath
My.Settings.Save()
MsgBox(My.Settings.CartellaTex.ToString)
End Sub
My.settings.CartellaTex setting is set to String.
I am sure no value is returned as by setting up a msg box at the end of the text such message does not show.
All the functions in my app that rely on My.settings.CartellaTex go in error.
I have read somewhere that FolderBrowser struggles when something called "threads" are involved, but this code ran perfectly yesterday night. Keep in mind I am using Visual Studio on Parallels for Mac but again this used to work hours ago. I am really new to vb.net and this is as much knowledge I have. Please be patient.
Try this:
Private Sub CartellaTessutiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CartellaTessutiToolStripMenuItem.Click
If FolderBrowserDialog1.ShowDialog = DailogResult.OK Then
My.Settings.CartellaTex = FolderBrowserDialog1.SelectedPath
My.Settings.Save()
MsgBox(My.Settings.CartellaTex.ToString)
End If
End Sub

New form "is never disposed" message

I've recently moved over to Visual Studio 2019 V16.2 and it's now showing me a new "message" whenever I move between forms in my Windows Forms App.
IDE0067 Disposable object created by 'New FindFile' is never disposed
I've always moved to the "next" form in my project with code snippets like:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim frmFindFile As New FindFile
frmFindFile.Show()
Me.Close()
End Sub
What am I doing wrong? Should I be disposing of the form variable once the new one is showing? The below gets rid of the warning, but my second form never shows up!
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim frmFindFile As New FindFile
frmFindFile.Show()
Me.Close()
frmFindFile.Dispose()
End Sub
VB.NET has default instances of forms, so if you just use FindFile.Show() it will not give a warning.
For more information, please see the answers at Why is there a default instance of every form in VB.Net but not in C#?
I haven't seen the proper answer to this question yet, but there is one.
Original code as listed above is:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim frmFindFile As New FindFile
frmFindFile.Show()
Me.Close()
End Sub
Yet, the proper way to do this is:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Using frmFindFile As FindFile = New FindFile()
frmFindFile.Show()
End Using
Me.Close()
End Sub
This will automatically dispose of anything within the Using structure that requires disposal.
Pretty much anything that can be instantiated using the Dim/As class structure can be placed within a Using structure.
Another example would be using streamreader/writer. Instantiate it with the Using, then instead of using instance.Close(), just use End Using to close it out and dispose of it.

VB.Net - data does not show up in database grid and does not update the ms access database

I am making a database program where users can add their data such as lrn, first name, middle name, and last name. My data sources are fine and the table loads in data grid view but when I run the program and enter new data, they do not show up in the data grid nor the access file
Here is the code :
Public Class Form1
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim poi As DialogResult
poi = MessageBox.Show("Do You Want To Exit?", "Exit Program?", MessageBoxButtons.OKCancel)
If poi = DialogResult.OK Then
Application.Exit()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'BoboDataSet.Table1' table. You can move, or remove it, as needed.
Me.Table1TableAdapter.Fill(Me.BoboDataSet.Table1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Table1BindingSource.AddNew()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'this button users click that will transfer the user's inputted data to the data grid viewer and updates the database
Table1BindingSource.EndEdit()
Table1TableAdapter.Update(BoboDataSet.Table1)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Table1BindingSource.RemoveCurrent()
End Sub
End Class
I am a bit stumped! I think there is a wrong configuration in my data source but I followed steps to enter it and I already changed the database property to Copy if Newer but the new data entered does not show up in the database grid viewer and the access file
Thanks for the response :)
*Edited to better reflect my problem, Hopefully
At last! I have fixed the problem. The textboxes were not binded to the table in the data bindings. I just changed in the data bindings section in the textbox properties. sorry for the inconvenience

How do you get to the .enter code automatically in visual basic?

Okay I realize the question is ambiguous, but I didn't know what else to name it. As all of you know if I double click a textbox in visual basic it gives me this code automatically.
Private Sub textBox1_Click(sender As Object, e As EventArgs) Handles textBox1.Click
'do stuff here
End Sub
What do you click or otherwise have to do to get this to show up automatically?
Private Sub textBox1_Enter(sender As Object, e As EventArgs) Handles textBox1.Enter
'do stuff here
End Sub
What about this one as well
Private Sub textBox1_TextChanged(sender As Object, e As EventArgs) Handles textBox1.TextChanged
'do stuff here
End Sub
Select the textbox. In the Properties pane/window (press F4 if can't see it), click the icon which looks a bit ilke a lightning bolt to get a list of the available handlers. Double-click the one you want and it will create the template for you.

Windows Forms: Keeping Check Beside Checked Box list item and displaying separate message for each

When I run my program currently, Within any checked box list I am unable to actually select an option (i.e. the box beside it, to tick).
I would also like each individual option to display a messagebox when ticked (i.e. "User story one added"), But currently my program only displays a general messagebox ("User Story selected") once the checked box list is clicked on. Any help would be much appreciated!
Current Code:
Public Class Form2
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
MessageBox.Show("User Story Selected")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form3.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
You are responding to the wrong event and might be likely to examine the wrong property.
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ItemCheckEventArgs)
MessageBox.Show("User Story Selected")
End Sub
Checks fire the ItemCheck event, and checked items are in the CheckedItems Collection. The SelectedItems collection is literally those selected (highlighted) which might not also be checked. It is not really a list of checkboxes, but a list of items drawn as checks - thats why they look different than regular checks. To see which item:
For n as Integer = 0 to CheckedListBox1.CheckedItems.Count-1
userWants = CheckedListBox1.CheckedItems(n)
Next n
Like a ListBox you can put anything in there, not just strings, so it was a list of stories, you might be able to do:
userWants = CheckedListBox1.CheckedItems(n).StoryName