Having trouble with the messagebox.show - vb.net

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.

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

VB - Click Same Button Twice

Im new to VB and have been searching the internet for answer.
Im trying to create a launcher that allows you to simply locate your exe and run the exe.
Problem is that i can't figure out how to have those 2 actions to happen with just 1 button.
e.g: Click " Play " will open a folder where you have to locate your exe, once its located the folder closes and then when you press " Play " again, it launches the allready located exe.
What i got so far is:
Private Property TextBox As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()
TextBox = OpenFileDialog1.FileName("/Wow.exe")
Process.Start(TextBox)
End Sub
End Class
It works " almost " as i want it to.
As of now, when i press the " Play " it simply opens a folder where i can select the .exe and then when the folder closes, it opens the .exe automatically. When i press the " Play button again it repeats the process. It even launches the .exe if i press the "exit button (top right)" on the folder.
Could it be possible to make it come with an error if its not the correct file that has been selected ??
Hope you can help me.
Thanks in advance.
How about something like this:
Private filePath As String = String.Empty
Private Sub PlayButton_Click(sender As System.Object, e As System.EventArgs) Handles PlayButton.Click
Try
If filePath.Length = 0 Then
Dim diagResult As DialogResult = OpenFileDialog1.ShowDialog()
If diagResult = Windows.Forms.DialogResult.OK Then
filePath = OpenFileDialog1.FileName
If filePath.ToUpper.EndsWith("WOW.EXE") Then
Process.Start(filePath)
Else
MessageBox.Show("Wrong file selected!")
filePath = String.Empty
End If
End If
Else
Process.Start(filePath)
End If
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred in the play button click:", ex.Message))
End Try
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

Change date format in code only

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/

How to automatically append to RichTextBox in Visual Basic

I tried Google for an answer but didn't find what I was looking for.
I created a very simple app that lets users use a barcode scanner to scan barcodes into a text file. Everything works fine, I just want to simplify it a bit more.
Originally, I have a textbox1.text field where the scanned barcode would appear, then the user had to click the 'Add' button (Button1.Click) I placed next to the textbox field to append the barcode serial into a RichTextBox right below. Well the user found it tedious to have to click the 'Add' button every time they scanned an individual barcode.
My Question
Is there a way I can have the text in textbox1.text automatically append to RichTextBox as soon as a barcode is scanned? I want to eliminate having to click the 'Add' button.
Here is my current code (code for the Button1.Click button):
Dim scanData As String = TextBox1.Text
RichTextBox1.AppendText(scanData + " " + Format(TimeOfDay, "HH:mm:ss") + vbNewLine)
TextBox1.Clear()
TextBox1.Focus()
First off, I would make sure the user cannot input text themselves by disabling the TextBox control (TextBox1.Enabled=False), then add your code to the TextChanged event:
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
Dim scanData As String = TextBox1.Text
RichTextBox1.AppendText(scanData + " " + Format(TimeOfDay, "HH:mm:ss") + vbNewLine)
TextBox1.Clear()
TextBox1.Focus()
End Sub
Before appending to the RTB, I would check to make sure the BarCode is valid.