vb.net datetimepicker not taking value from my.setting - vb.net

I have this code to save value from datetimepicker1:
Private Sub DateTimePicker1_Validating(sender As Object, e As EventArgs) _
Handles DateTimePicker1.Validating
My.Settings.dt1value= DateTimePicker1.Value.ToString
MsgBox("before save")
My.Settings.Save()
MsgBox("after save")
End Sub
It look that it saves value in My.Settings (From Message box 1,2)
Then when closing the app and running it again; it is not loading the My.Settings.dt1value into DateTimePicker1
The code for loading is:
Private Sub main_Shown(sender As Object, e As EventArgs) Handles Me.Shown
DateTimePicker1.Value = Convert.ToDateTime(My.Settings.dt1value)
End Sub
Other controls like Textbox1 is saving and loading properly but only for DateTimePicker is not working.
I tried to change from Handles Me.Shown to Handles Me.Load but same problem.
I have another problem,
When I deploy the application and setup in windows, My.Setting.Save() not working for all controls.
I had read other similar posts and try to follow them but nothing helps.
Any tip appreciated,
Thanks in advance.

First check whether the value is getting saved. Use
MsgBox(My.Settings.dt1value)
instead of
MsgBox("after save").
This will ensure the value is getting saved.
MsgBox("before save") & MsgBox("after save") does NOTHING useful here
But as from your code snippet, It seems the value is getting saved.
In the form Load event write the below mentioned code and check for the output:
string DatePattern = "dd/MM/yyyy HH:mm:ss";
DateTime ConvertedDateTime;
DateTimePicker1.Value = DateTime.TryParseExact(My.Settings.dt1value, DatePattern , null, DateTimeStyles.None, out ConvertedDateTime))
Edit : About your second problem
That is because whenever you update or modify the application in any way, the My.Settings(built-in settings file) gets flushed and a new one is generated. I would suggest you to save your config file in a separate external file. NOT in My.Settings

Related

Why Is A FilePath Item With ~$ Added To ListBox

I am adding file paths from a folder to a List Box which are then opened as text in a Rich Text Box. I have used the same syntax as the code below for achieving the same purpose in another List Box and it works just fine. But, in the current example, I have two files in the default MyProjects folder (i.e. default folder is created by my app), but when I add the file paths from the folder as items to the List Box, I get a third item with ~$ in the file path? This item is obviously some kind of repetition of the first file path in the list? The two files in the default folder are also created by my app so, if this is a file access issue, I don't understand why I wouldn't have access to a file created by my app? Can anyone give me a clue what's happening here?
What I have Tried:
I have tried debugging to check where the extra file path is coming from. As far as I can tell, it is being created when I add the file paths to the List Box? i.e. commenting out the code for adding the items to the List Box stops all items being added, but doesn't tell me where this extra item is coming from?
The "Extra Item" Issue:
System.Windows.Forms.ListBox+ObjectCollectionC:\Users\username\Documents\MySolution\MyProjects\RTFdoc.rtf
C:\Users\username\Documents\MySolution\MyProjects\Testdoc.rtf
C:\Users\username\Documents\MySolution\MyProjects~$Fdoc.rtf
The Code:
lbxName.Items.AddRange(Directory.GetFiles("C:\Users\" + username + "\Documents\MySolution\MyProjects"))
lbxName.SelectedIndex = 0
Code For Loading:
For Each item In lbxName.SelectedItems
RTB.LoadFile(lbxName.SelectedItem, RichTextBoxStreamType.RichText)
Next
I cannot reproduce the error in the following code.
Private Sub FillListBox()
ListBox1.Items.AddRange(Directory.GetFiles("C:\Users\" & username & "\Documents\MySolution\MyProjects"))
ListBox1.SelectedIndex = 0
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillListBox()
End Sub
Please read the comments in the following code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each item In ListBox1.SelectedItems
'The following will overwrite the contents of the RichTextBox on each iteration
'This overload of LoadFile will only handle .rtf files
RichTextBox1.LoadFile(item.ToString)
Next
End Sub
I suggest you set the SelectionMode property to One in the designer and do the following.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.LoadFile(ListBox1.SelectedItem.ToString)
End Sub
Try to use as follows:
Directory.GetFiles("C:\Users\" & username & "\Documents\MySolution\MyProjects").Where(Function(f)
Return New IO.FileInfo(f).Attributes & IO.FileAttributes.Hidden & IO.FileAttributes.System = 0

Saving a setting to MySettings and exiting the program isn't working

I'm having a problem with saving a setting to My Project and then exiting my program with an 'End' statement. If I save the setting but don't execute the end statement, everything works. If I save the setting and then execute the 'End', the setting doesn't get saved. Here's some code that illustrates the problem:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'reads the last setting correctly
TextBox1.Text = My.Settings.MySetting
End Sub
Private Sub btnWrite_Click(sender As Object, e As EventArgs) Handles btnWrite.Click
'write value, don't exit; works
My.Settings.MySetting = TextBox1.Text
End Sub
Private Sub btnWriteEnd_Click(sender As Object, e As EventArgs) Handles btnWriteEnd.Click
'write value and end; fails
My.Settings.MySetting = TextBox1.Text
End
End Sub
End Class
When I execute the code, whatever was last in My.Settings.MySetting appears in TextBox1. If I change the text in the textbox and click on the 'Write' button and manually exit the program by clicking on the 'X', the new text appears properly changed when I execute the program again. If I change the text and exit programmatically by clicking on 'WriteEnd', the changed setting text doesn't get written to 'MySetting'.
What am I doing wrong?
Thanks
Settings will be saved automatically at shutdown by default, so there's generally no need to call Save. End is definitely the issue. NEVER use End. Call Close on the startup form or call Application.Exit. I compare End with a bouncer grabbing someone by the scruff of the neck and throwing them out, spilling their drink on everyone and leaving their jacket behind, rather than asking them to leave of their own accord.

How do i link Visual Studio applications over LAN

I have created a VS application , I have installed a copy on another computer and I wish to link them via LAN so that if settings are canged in one , the others setings will also be saved .
for example this setting
i created a new name in the sttings are and call it "AdminIn" and set its type it integer , its scope to user and set its value to 0
Dim AI As New My .MySettings
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AI.AdminIn = AI.AdminIn + 1
Ai.SAve()
End Sub
now how can AI also be updated in the other application on the other computer .
How do I connect via LAN and accomplish this ?
I found this link that provides some example code to modify application-scoped variables from My.Settings that might be useful. I've tested it out with a simple form with a timer and a label showing me the current value of the AdminIn setting, and it seems to work. The timer updates the label on every instance of the form by checking the reloaded My.Settings value. The variable would need to be application scoped in order to be accessible to all users on any machine that may run the executable.
http://www.codeproject.com/Articles/19211/Changing-application-scoped-settings-at-run-time
Here's the form code that I put together to keep the current admin count up-to-date. Very simplistic, but it seems to do the job neatly.
Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'Decrement the AdminIn count when the current instance of the form is closed.
Me.tmrAdminCheck.Stop()
ChangeMyAppScopedSetting((My.Settings.AdminIn - 1).ToString)
'Reload the .exe.config file to synchronize the current AdminIn count.
My.Settings.Reload()
My.Settings.Save()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Increment the current AdminIn count when a new instance of the form is loaded
ChangeMyAppScopedSetting((My.Settings.AdminIn + 1).ToString)
'Reload the .exe.config file to synchronize the current AdminIn count.
My.Settings.Reload()
My.Settings.Save()
Me.lblAdminsIn.Text = "Current Admins In: " & My.Settings.AdminIn.ToString
'Start the timer to periodically check the AdminIn count from My.Settings
Me.tmrAdminCheck.Enabled = True
Me.tmrAdminCheck.Interval = 100
Me.tmrAdminCheck.Start()
Me.Refresh()
Application.DoEvents()
End Sub
Private Sub tmrAdminCheck_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrAdminCheck.Tick
'Reload the .exe.config file to synchronize the current AdminIn count.
My.Settings.Reload()
Me.lblAdminsIn.Text = "Current Admins In: " & My.Settings.AdminIn.ToString
Me.Refresh()
Application.DoEvents()
End Sub
End Class
I've found a couple of things with this method, and they relate to what others have already mentioned in their comments:
The application's .exe.config file must be in an accessible location (the CodeProject example defaults to the application's executable directory). Of course, you could save the settings to an INI file or some other configuration file in another shared directory and accomplish a similar thing, but this method uses the My.Settings.
You'll may want to do some additional checking for the possibility
of two people attempting to get in at exactly the same time. If
that happens, the configuration file will still be open and locked,
and the new AdminIn value won't be saved. The CodeProject example
doesn't have any exception handling, but you could easily work this
functionality into the exception handling by making a recursive call
to the Sub.
Otherwise, this seems to be a totally viable method of accomplishing what you're talking about.

How to save settings made in VB?

Is there any way I can save changes made by a user , so that at the next run they would still be there? I have seen something about My.Settings , is that how it is done?
Yes you have multiple options, you can create a file and load it on startup, or you can use the built in settings object. Just go to your Project Property's --> settings set your variable there, the you can access it through My.Settings
Then load it like this:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim greeting As String = My.Settings.mySetting
End Sub
you would change the value and save it like this
My.Settings.mySetting = "GoodBye"
My.Settings.Save()

Error with code?

Hi can anyone tell me why the following dose not work:
(p.s I dont want the file to append upon clicking abutton just upon clicking the checkbox.
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Dim FILE_NAME As String = "C:\RXF\log.txt"
'Adding items for AutoCAD 2006...
If CheckBox1.CheckState = CheckState.Checked Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.WriteLine("module: 4FNV-67-5H")
objWriter.Close()
End If
End Sub
End Class
Not reproducible, even with your exact code as posted. This works perfectly fine for me, creating a text file in the specified location if one does not exist and appending the specified text to the end of the file.
The only thing I suggest is wrapping your StreamWriter object in a Using statement to ensure that its Dispose method always gets called, even if an exception is thrown (which is all the more likely when you're doing disk I/O). So, your existing code would simply change to:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Dim FILE_NAME As String = "C:\RXF\log.txt"
''#Adding items for AutoCAD 2006...
If CheckBox1.CheckState = CheckState.Checked Then
Using objWriter as New System.IO.StreamWriter(FILE_NAME, True)
objWriter.WriteLine("module: 4FNV-67-5H")
objWriter.Close()
End Using
End If
End Sub
Also, if you anticipate this method getting called a lot (i.e., the user clicking and unclicking and clicking the checkbox repeatedly), you might consider creating the StreamWriter object once and saving it as a private class-level variable, instead of creating and disposing of it each time the method is called. Then you just have to make sure that you dispose of it whenever your class (presumably the containing form) is disposed.