VB 2013 Persistent User Settings - vb.net

On my form I have a menu with "file-save". When I click save I want to save particular settings to restore when the form is closed and re-opened. I've done this successfully for text in text-boxes and the checked states of check-boxes, but I'm failing when trying to loop through the items in a list-box. Please see below for what I've tried...
When I click save:
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs)
Handles SaveToolStripMenuItem.Click
For Each i In ListBox1.Items()
My.Settings.ListBox1.Add(i)
Next
My.Settings.Save()
End Sub
When my form loads:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
For Each i In My.Settings.ListBox1()
ListBox1.Items.Add(i)
Next
End Sub
I've only been using VB for three days, so apologies if I am missing something simple ha! Thanks for any help!!!

There is one small glitch with the StringCollection in settings. If you do not seed it with a fake variable then it starts out as Nothing and you cannot add to Nothing. in your form load add this:
' if the collection has not been initialized, do so
If My.Settings.ListBox1 Is Nothing Then
My.Settings.ListBox1= New System.Collections.Specialized.StringCollection
End If
' now it is safe to use: load strings from Setting -> form listbox
For Each s As String In My.Settings.ListBox1()
ListBox1.Items.Add(s)
Next
The very first time it runs, there are likely no saved settings, so we have to create the container for them, basically.
Option Strict can be implemented by file, by adding this at the top:
Option Strict On
Or for the project: Project => Properties => Compile: Option Strict is likely to the right (I have 2012). You can also set it as a permanent option (recommended).
Among other things, this will prevent you from plucking variables out of the air and use them without declaring a type (which will lead to errors). For instance:
For Each i In My.Settings.ListBox1()
becomes
For Each s As String In My.Settings.ListBox1() ' tell the compiler the Type

Related

Save/Load full form

I am writing a program for a friend in my free time to use to document students for physical therapy and occupational therapy. I'm using visual basic and have created a basic form so far, but realized I'm not sure how to save/load the information that will be input.
This is what the form looks like so far (basic I know)
Form
I do not have access to sql or ms access at the moment so I'm hoping to be able to save all the information to a txt document or xml file and be able to read and update it at a future time. Eventually I will have multiple forms all associated with one student and would like to save them all together.
Any help would be GREATLY appreciated.
You can use this method, it will work perfect but the data will be stored in a xml file and it's editable :
First you go to your project properties :
Then you go the the settings tab :
And there you add all the variables that you need to store data in :
So to save the data you can use the Form_Closing event :
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.StudentName = StudentNameTextBox.Text
'.......
'....
'And you do that for all the information that you need to store.
End Sub
To Load the data when the form loads, just revers the storing way :
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
StudentNameTextBox.Text = My.Settings.StudentName
'.......
'....
'And you do that for all the information that you need to load.
End Sub
Hope that will be useful to you :)

VB.Net 2013 How do I programatically update a bindingsource without getting late binding errors/warnings?

I created a DataSet with a DataTable & TableAdapter that contains Select/Update/Insert/Delete commands that relate to my MSSQL database. The DataTable selects all the fields from a Person table (two fields in particular a LastName string and an UpdateUserID int).
Then I made a simple test form with a textbox and a button. From the Data Sources tab, I dragged the LastName field onto the form. This creates MyDataset, MyTableAdapter, and MyBindingSource on the form. It also databinds the textbox to the LastName field of the BindingSource.
Here's the complete code for the form:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
loadData()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Validate()
Me.MyBindingSource.EndEdit()
' Me.MyBindingSource.Current("updateUserID") = 123
Me.MyTableAdapter.Update(Me.MyDataset.myDatatable)
loadData()
End Sub
Private Sub loadData()
Me.MyTableAdapter.Fill(Me.MyDataset.myDatatable)
End Sub
End Class
This all works completely fine. The problem occurs when I uncomment the line: "Me.MyBindingSource.Current("updateID") = 123". I'm trying to set the UpdateID to save the ID of the user that most recently made a change.
In Visual Studio 2008, this worked fine. But I just upgraded to 2013 and now I'm getting an error that says "Option Strict On disallows late binding."
(PS, this is a test program. In the actual program - converted from 2008 - I only get warnings saying "Late bound resolution; runtime errors could occur.")
I understand why I'm getting these errors/warnings: because the IDE doesn't know what DataType the "updateUserID" DataColumn is.
But what can I do to manually update the BindingSource before saving? I don't think turning Option Strict off is a good idea.
The DataColumns in the DataTable have their DataTypes specified. Is there a way to reference the DataColumn directly instead of through the string of its name?
I'm looking for something like:
Me.MyBindingSource.Current.DataColumns.UpdateUserID.value = 123
Thanks for any help!

How to save a changed label when you exit

In my program I have a preview, and edit side.
When you edit using the text boxes on the edit side(right side) and click "save", It should change the label on the right side (preview side). Although when you exit the program and re-open, all the data you entered has disappeared!,
I have tried the below code and had no luck as my result.
Public Class Form1
Private Shared NameBasic As Integer
Public Sub New()
InitializeComponent()
lblNameBasic.Text = Convert.ToString(NameBasic)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
NameBasic = txtFirstBasic.Text
lblNameBasic.Text = Convert.ToString(NameBasic)
End Sub
End Class
Also my goal is to have it be able to take it on a flashdrive onto any computer and have that data still saved within the exe. Is this even manageable? (I am more of a web based programmer so I am a bit new to this)
You need to write values to a text file at the same location of the exe. and read it. you will need both exe and the textfile.
OR
You will need to write a dll on run-time and write some values while closing the application. this will be more secure then the text file. here you will need both exe and dll to be copied into flash drive
OR
the application setting as spoken by others..

How can I keep items in ComboBox after close the application

Public Class Form1
Private Sub btnAddCat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCat.Click
If txtAdd.Text <> "" Then
comboBox1.Items.Add(txtAdd.Text)
txtAdd.Clear()
Else
MessageBox.Show("Fill the blanket")
End If
End Sub
End Class
If user close application should see the items but there is no items
Can anyone help?
thanks
If you want the application to remember the value the next time it is run, you will need to save the value to disk. There are many different options for how to do that (e.g. text file, XML, database, registry), but for simple tasks, I'd recommend just using the built-in Settings feature.
To use the Settings feature, first you need to open your project properties screen. Then select the Settings tab. Add a new setting by typing in the name and selecting a data type. For instance, you could type MyItems for the name, and then select System.Collections.Specialized.StringCollection as the data type. Then, in your code, you can read the current value of the setting like this (perhaps in your form's Load event handler):
For Each i As String In My.Settings.MyItems
ComboBox1.Items.Add(i)
Next
And then you could save the list to the setting, like this (perhaps in your form's FormClosed event handler):
My.Settings.MyItems.Clear()
For Each i As String In ComboBox1.Items
My.Settings.MyItems.Add(i)
Next
You need to persist the data to a data store (either the database or the file system) so that the next time the application runs, then it can check the data store and display the items to the user.

Converting C# to VB.net is not working

I have translated some code from C# to VB.net for the purpose of getting Folder Browser functionality. The link to the code is here.....
http://www.codeproject.com/KB/aspnet/DirectoryBrowsing.aspx
My issue is that I have not been able to correcly translate these two lines of code to VB.net.
TreeView1.TreeNodeExpanded +=new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
TreeView1.SelectedNodeChanged += new EventHandler(TreeView1_SelectedNodeChanged);
Every translator I have used has simply dropped the semicolon from the end of each line. But the editor still does not like them.
I could some help with this as it seems this effects the refresh of the selected folder in the tree view control.
I don't get to see the C drive folder unless I type the path in the text box, and the folder will still not expand.
thank you,
Use this:
AddHandler TreeView1.TreeNodeExpanded, AddressOf TreeView1_TreeNodeExpanded
AddHandler TreeView1.SelectedNodeChanged, AddressOf TreeView1_SelectedNodeChanged
Edit:
A different way to do this would be to apply it at the method level:
Protected Sub TreeView1_TreeNodeExpanded(ByVal sender as Object, ByVal e as TreeNodeEventArgs) Handles TreeView1.TreeNodeExpanded
' Some code
End Sub
Protected Sub TreeView1_SelectedNodeChanged(ByVal sender as Object, ByVal e as EventArgs) Handles TreeView1.SelectedNodeChanged
' Some code
End Sub
You should run this in debug to find out what exactly is going on. I find a lot of times when events of this nature are run in asp.net, you have a conflicting event that "resets" the controls you are attempting to change.