Change date format in code only - vb.net

I've a code that simply creates a folder in a directory and gives it a name based on the values of a datetimepicker and a text box.
The date picker is displayed on the form as "16 October 2013" (how I want to keep it) but when I generate the file name I would lime the date to read in the format "161013"
The code I'm using is below if that helps
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lMailbox As String
lMailbox = t2.Text & "-" & d1.Text
' Check if folder exists, if not: create it
If Not Directory.Exists(nMailbox & lMailbox) Then
Directory.CreateDirectory(nMailbox & lMailbox)
' Folder created message
MessageBox.Show("Mailbox created!", "Lynx Control Panel", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
' Folder already exists
MessageBox.Show("Mailbox already exists!", "Lynx Control Panel", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End If
End Sub
The nMailbox & lMailbox are declared at the top of the code page
d1 is the name of the datepicker
I'm very new to VB.net and would appreciate any help
Thanks

try this:
lMailbox = t2.Text & "-" & d1.Value.ToString("ddMMyy")
The format options are the same as for DateTime string formatting. Use Value of the DTP rather than the Text which is formatted otherwise/

Related

Why is it that the LastWriteTime of a file and the stored Date&Time of that same file don't equal, even if are the same?

I'm trying to notify users there has been a change to a file, by making a picturebox turn lime when the date in the application's settings is not equal to the LastWriteTime of a file. However, when i run the program and the two are equal (to my knowledge) it still says they are not equal. I've added a textbox in order to visualize it, but I don't know what I'm doing wrong...
My code:
fsw_wervelSTX_file = My.Computer.FileSystem.GetFileInfo("G:\divi\RATH\Applicaties\RSM\Program\Settings\IGART\WervelSTX\log_wervelSTX.txt")
If Not My.Settings.fsw_wevelSTX_lastwrite = fsw_wervelSTX_file.LastWriteTime Then
PictureBox_wervelSTX.BackColor = Color.Lime
MsgBox("Time in My.Settings: " & My.Settings.fsw_wevelSTX_lastwrite & " LastWriteTime: " & fsw_wervelSTX_file.LastWriteTime, MsgBoxStyle.Information)
Else
PictureBox_wervelSTX.BackColor = Color.Maroon
MsgBox("It is the same", MsgBoxStyle.Information)
End If
There's a screenshot of the MsgBox in the link:
MsgBox
The LastWriteTime is stored to My.Settings when the user clicks the button to open the specified file:
Code:
Private Sub Button11_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click
Try
Process.Start("G:\divi\RATH\Applicaties\RSM\Program\Settings\IGART\WervelSTX\log_wervelSTX.txt")
PictureBox_wervelSTX.BackColor = Color.Maroon
fsw_wervelSTX_file = My.Computer.FileSystem.GetFileInfo("G:\divi\RATH\Applicaties\RSM\Program\Settings\IGART\WervelSTX\log_wervelSTX.txt")
MsgBox(fsw_wervelSTX_file.LastWriteTime, MsgBoxStyle.Information)
My.Settings.fsw_wevelSTX_lastwrite = fsw_wervelSTX_file.LastWriteTime
Catch ex As Exception
MessageBox.Show("Error opening file: " & ex.Message)
End Try
End Sub
Thanks in advance.
Going from the code you posted I guess it's a serialization thing. The datetime is probably stored in your settings as something like 2016-01-06 12:08:11 (aside the actual date formatting) whereas the actual datetime probably has more/better 'resolution' and contains something like 2016-01-06 12:08:11.123. I'd suggest one of:
Storing it in a specific (specified) format and make sure you compare no more than the actual stored 'resolution'
Storing the value as ticks or some other specific long/integer value (e.g. UNIX timestamp for example)
Allow for some 'margin' when comparing
Which is best is up to you and the requirements / usecases.
There's all sorts of weird things with file datetimes like Why does the timestamp of a file increase by up to 2 seconds when I copy it to a USB thumb drive? but my best guess currently is that you're just comparing two slightly different values.
Added to load code:
Dim timecompare As Date
Private Sub IGART_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
fsw_wervelSTX_file = My.Computer.FileSystem.GetFileInfo("G:\divi\RATH\Applicaties\RSM\Program\Settings\IGART\WervelSTX\log_wervelSTX.txt")
timecompare = fsw_wervelSTX_file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
If Not My.Settings.fsw_wevelSTX_lastwrite = timecompare Then
PictureBox_wervelSTX.BackColor = Color.Lime
MsgBox("Time in My.Settings: " & My.Settings.fsw_wevelSTX_lastwrite & " LastWriteTime: " & fsw_wervelSTX_file.LastWriteTime, MsgBoxStyle.Information)
Else
PictureBox_wervelSTX.BackColor = Color.Maroon
MsgBox("It is the same", MsgBoxStyle.Information)
End If
Added to button code:
Dim time As Date
Private Sub Button11_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click
Try
Process.Start("G:\divi\RATH\Applicaties\RSM\Program\Settings\IGART\WervelSTX\log_wervelSTX.txt")
PictureBox_wervelSTX.BackColor = Color.Maroon
fsw_wervelSTX_file = My.Computer.FileSystem.GetFileInfo("G:\divi\RATH\Applicaties\RSM\Program\Settings\IGART\WervelSTX\log_wervelSTX.txt")
MsgBox(fsw_wervelSTX_file.LastWriteTime, MsgBoxStyle.Information)
My.Settings.fsw_wevelSTX_lastwrite = fsw_wervelSTX_file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
Catch ex As Exception
MessageBox.Show("Error opening file: " & ex.Message)
End Try
End Sub

Selected item in listbox, to form part of a directory

I'm having a hard time trying to check if a file exist when the user selects an item from the listbox.
The selected item is supposed to be converted into a part of a directory to open a file. I know for sure I have the right event and that the problem is within the "if" statement because without the "if" statement a message box will show the item name.
Its hard to explain in words so heres the code, and if someone finds an answer to this issue and I'm not on, Thanks.
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim item As String = ListBox1.SelectedItem.ToString
If System.IO.File.Exists(Application.StartupPath & "\" & item & "\" & "config.yml") Then
Button1.Enabled = True
End If
End Sub

How to display value from TextBox into a MessageBox in VB.NET?

I honestly don't know what I'm doing wrong. I have a textbox named "txtNumSticks" where the user enters a number. After the user hits start, I want a message box to pop up that says "Okay! We'll play with (x) sticks!" But I can't get it to work. First day learning VB.net. Thanks in advance!
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Dim NumSticks As String
txtNumSticks.Text = NumSticks
Game.Show()
Me.Close()
MessageBox.Show("Okay! We'll play with " & NumSticks & "sticks!")
End Sub
You are setting the variable the wrong way around you should be assigning NumSticks to the value in the text box so:
NumSticks = txtNumSticks.Text
or alternatively without the use of a variable
MessageBox.Show("Okay! We'll play with " & txtNumSticks.Text & "sticks!")
You may want to add a little bit of error checking in your program to make sure your value entered is Numeric.
Dim NumSticks As String
NumSticks = txtNumSticks.Text.ToString
If IsNumeric(NumSticks) Then
Game.Show()
MessageBox.Show("Okay! We'll play with " & NumSticks & " sticks!")
Me.Close()
Else
' Let user know the value is non-numeric
MessageBox.Show("Non Numeric Value entered", "Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If

Having trouble with the messagebox.show

I'm having trouble with the messagebox.show. I want the answer and this line "would you like to try another temp conversion?" to appear on a message box with yesno buttons.
Public Class Form1
Dim intFah As Windows.Forms.DialogResult
Private Sub BtnFah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFah.Click
Try
Dim intFah As Integer
intFah = CInt(TxtBoxTemp.Text)
intFah = (intFah * 9) / 5 - 32
MessageBox.Show(intFah.ToString & ControlChars.CrLf & "Would you like to start another temp conversion?", MessageBoxButtons.YesNo)
Catch
MessageBox.Show("Would you like to start another temp conversion?", "System Error", MessageBoxButtons.YesNo)
End Try
End Sub
End Class
Your code didn't build for me - it's probably because in your MessageBox.Show:
MessageBox.Show(intFah.ToString & ControlChars.CrLf & "Would you like to start another temp conversion?", MessageBoxButtons.YesNo)
You aren't passing the right number of parameters. As per the link, it needs the text, a caption (title) and then the button options
If I change it to:
MessageBox.Show(intFah.ToString & ControlChars.CrLf & "Would you like to start another temp conversion?", "A Caption", MessageBoxButtons.YesNo)
Then it builds and when I run the app I can get the answer appearing in one line and the text on a second line as per what you're looking for.

Can't get VBA to write "http://"as text?

I wrote the code bellow and need the asociated .PGP file to have the text http:// included. the PGP file is namely read by Autocad which requers the "http://" in its text to be able to launch the desierd webpage. problem is , is that VBA is Auto formating the http:// as a code entatie and not writting it to the text based PGP file.
Can any one tell me how to achive what im after?
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim FILE_NAME As String = "C:\test.pgp"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.WriteLine((TextBox5.Text) + "," + " " + "START http://" + (TextBox6.Text) + ", 1,,")
objWriter.Close()
MsgBox("The acad.pgp file was successfully appended…")
Else
MsgBox("File missing reinstall or contact vendor…")
End If
End Sub
Hm, I'tried your code above in VisualStudio 2010. An empty file named C:\test.pgp was appended with the following text:
textbox5, START http://textbox6, 1,,
The text http... is right there. Sometimes, when I open the file in a viewer, this viewer automatically detects the http string and marks it as a hyperlink. But only in this viewer!
So the error seems to be somewhere else, not in the code?