How to use timer in vb.net - vb.net

I have this code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim num As String
Dim message As String
Dim name As String
message = txtMessage.Text
Dim count As Integer = Me.TblContactsBindingSource.Count
If i < TblContactsDataGridView.Rows.Count - 1 Then 'stay within bounds
i = i + 1 ' for all rows except Row0
TblContactsDataGridView.Rows(i - 1).DefaultCellStyle.BackColor = Color.White ' restore previous highlight
TblContactsDataGridView.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'new highlight
num = Me.TblContactsDataGridView.Rows(i).Cells(1).Value.ToString()
name = Me.TblContactsDataGridView.Rows(i).Cells(0).Value.ToString()
If SerialPort1.IsOpen() Then
SerialPort1.Write("AT" & vbCrLf)
SerialPort1.Write("AT+CMGF=1" & vbCrLf)
SerialPort1.Write("AT+CMGS=" & Chr(34) & num & Chr(34) & vbCrLf)
SerialPort1.Write(message & Chr(26))
MessageBox.Show("Message has been successfully sent to " & vbNewLine & name & " (" & num & ") ", "Message Sent", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else 'next row is off the bottom so
'i = 0 'reset index
'TblSmsDataGridView.Rows(TblSmsDataGridView.Rows.Count - 1).DefaultCellStyle.BackColor = Color.White 'restore bottom row
'TblSmsDataGridView.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'highlight top row
End If
In a command button I have this:
Timer1.Interval = 2000
Timer1.Enabled = True 'no need to enable it and start it; one or t'other
What happen is, the message box appears over and over. How can i trigger message box to automatically close once it is finished? I commented the code in the "else" because the it repeats over and over.

You have to use a custom message box. Normal message box wont do the thing you wanted. It will pop up every 2 second. best choice is to make a new form and show it as a message box. :)

You need to set timer1.enabled = false in the timer1.tick handler.

Related

Display selected radio button and check box on a msgbox when button1 is clicked VB

So what I'm trying to do is create a MessageBox when Button1 is clicked with the selected Radiobuttons and Checkboxes on it.
Here's the design:
And I want the output to be something like this:
Thank you
Better way of doing this is to loop through the groupbox controls and check if the checkboxes are checked and if yes append that to some string.And after all your checks are complete display the string using messagebox.Simple as it is.To provide some more light to the solutions please go through the below code
Looping through controls
Dim strfinal As String
For Each gb As Control In Me.Controls
If gb.GetType() Is GetType(GroupBox) Then
Dim str As String
str = gb.Text
For Each c As CheckBox In gb.Controls
If c.Checked = True Then
str = str + vbNewLine + c.Text
End If
Next
If str <> "" Then
strfinal = strfinal + vbNewLine + str
End If
End If
Next
and displaying in messagebox
If strfinal <> "" Then
MessageBox.Show(strfinal, "somecaption", MessageBoxButtons.OK)
End If
hope this helps.
This code is going to give you the result exactly as in picture2 above :
Dim Toppings As String = "Toppings:" & VbCrlf
Dim TSize As String = "Size:"
Dim CrustType As String = "Crust Type:"
Here when you press on Button1, when the name of the groubBox that contains the toppings is ' ToppingsGroupBox' and the same for the other groupBoxes:
For Each CB As CheckBox In ToppingsGroupBox.Controls
If CB.Checked Then
Toppings &= "-" & CB.Text & VbCrlf
End If
End Each
For Each RB As RadioButton In SizesGroupBox.Controls
If RB.Checked Then
TSize &= RB.Text
End If
End Each
For Each RB As RadioButton In CurstTypeGroupBox.Controls
If RB.Checked Then
CurstType &= RB.Text
End If
End Each
If DineInRadioBox.Checked Then
MsgBox(TSize & VbCrlf & CurstType & Toppings & "*Dine In")
Else
MsgBox(TSize & VbCrlf & CurstType & Toppings & "*Take Out")
End If
Hope that will help you :)

This date time literal was not understood

I get an error saying "This date time literal was not understood" with this formula:
Private Sub btnOPrint_Click(sender As Object, e As EventArgs) Handles btnOPrint.Click
If MsgBox("Print Offertory Record?", MsgBoxStyle.OkCancel, "Print Record") = MsgBoxResult.Ok Then
Dim report As New ReportDocument
report.Load("C:\Users\Paolo\Documents\Visual Studio 2015\Projects\NewMonitoringSystem\NewMonitoringSystem\OffertoryReport.rpt")
docprint.CrystalReportViewer1.ReportSource = report
docprint.CrystalReportViewer1.SelectionFormula = "{tblOffertory.Date}=""" & dtpOffertory.Text & """ AND {tblOffertory.Weekly}=#" & txtOffertory.Text & "#"
docprint.CrystalReportViewer1.Refresh()
docprint.Show()
End If
End Sub
If I remove this line...
AND {tblOffertory.Weekly}=#" & txtOffertory.Text & "#"
...and instead use this line...
docprint.CrystalReportViewer1.SelectionFormula = "{tblOffertory.Date}=""" & dtpOffertory.Text & """"
...it shows the report with no data, just the columns. What formula should I use?

VB Saving Listview Items Error

Well the code itself works. The problem occurs when there is a sub items without text, the program will crash. I'm looking for a method that will bypass this annoying error.
My Code:
If ComboBox1.Text = "Everything" Then
Dim SetSave As SaveFileDialog = New SaveFileDialog
SetSave.Title = ".txt"
SetSave.Filter = ".txt File (*.txt)|*.txt"
If SetSave.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim s As New IO.StreamWriter(SetSave.FileName, False)
For Each myItem As ListViewItem In Form1.ListView1.Items
s.WriteLine(myItem.Text & TextBox1.Text & myItem.SubItems(1).Text & TextBox1.Text & myItem.SubItems(2).Text & TextBox1.Text & myItem.SubItems(3).Text & TextBox1.Text & myItem.SubItems(4).Text & TextBox1.Text & myItem.SubItems(5).Text & TextBox1.Text & myItem.SubItems(6).Text & TextBox1.Text & myItem.SubItems(7).Text) '// write Item and SubItem.
Next
s.Close()
End If
Error:(this indicates the the listview item without text it can range from number 1 up to 7, the one below is 5)
InvalidArgument=Value of '5' is not valid for 'index'.
Parameter name: index
Your indexing is starting at 1. VB indexing starts at 0 so for 5 items you whould have index values of 0 to 4

Login with textboxes in visual basic, 3 tries

I'm making a login script in VB using textboxes.
My problem is that the msgbox which inform the user about attempts left keep looping itself as well and using up all the (3) tries.
Not sure what's wrong.
here is my code:
Dim cor_pw As String = "1234"
Dim cor_us As String = "abcd"
Dim tries1 As Integer = 3
tries1 -= 1
Do
MsgBox("wrong combination, " & tries1 & " chances left to make it right.")
Loop Until textbox1.Text = cor_us And textbox2.Text = cor_pw Or tries1<= 0
If textbox1.Text = cor_us And textbox2.Text = cor_pw Then
MsgBox("Congratulations! That's Correct!")
Else
MsgBox("You've used all tries! " & tries1 & " chances left, good bye!")
Me.Close()
End If
You need an OK button that indicates the user has finished entering text.
In the event for the OK button you would
validate the text
If valid then you are good
otherwise increment the retry1 variable -- which must be declared at the Form (module) level!
now test retry1
if retry 1 is > 3 then display failure message and disable the OK button
otherwise display retry message
exit the sub
At this point the user can reenter the values and then hit OK button again.
No loop.
Your loop event is in the wrong area(s)
Dim cor_pw As String = "1234"
Dim cor_us As String = "abcd"
Dim tries1 As Integer = 3
Do Until (textbox1.Text = cor_us And textbox2.Text = cor_pw) Or tries1<= 0
If textbox1.Text = cor_us And textbox2.Text = cor_pw Then
MsgBox("Congratulations! That's Correct!")
Else
MsgBox("You've used all tries! " & tries1 & " chances left, good bye!")
Me.Close()
End If
MsgBox("wrong combination, " & tries1 & " chances left to make it right.")
tries1 -= 1
Loop

Vb.net Journal Program Issue

Okay so for an internship project i'm making a Journal with streamwriters and streamreaders.
I have to to where you can create an account with a name, Username, and Password. I also have it to where it creates a txt file in that persons name when you create the account. Now, they login and it brings them to the journal page. The Journal Page for the most part has a Date for your journal Entry, the title of the journal and the journal entry text itself.
The problem that I am having is that when you click the button to create/edit a journal entry, it goes through a sub routine that checks if that journal exists (Meaning that there is already one for that date) or not. If it doesn't exist, then it should create a new one at the bottom of the text file. If it does exist then it should edit the lines in which that journal are stationed in the text file.
Code:
Private Sub CreateBtn_Click(sender As System.Object, e As System.EventArgs) Handles CreateBtn.Click
Errors = ""
Dim TempCounter As Integer = 0
If TitleTxt.Text = "" Then
Errors = "You must enter a title." & vbCrLf
End If
If JournalTextRtxt.Text = "" Then
Errors &= "You must enter an entry for the journal."
End If
If Errors <> "" Then
MessageBox.Show("There's an error in creating/editing your journal." & vbCrLf & "Error(s):" & vbCrLf & Errors, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
JournalDate = DateTimePicker1.Value
JournalTitle = TitleTxt.Text
JournalText = JournalTextRtxt.Text
arrJournalEntries(TempCounter).TheDate = JournalDate
arrJournalEntries(TempCounter).Title = JournalTitle
arrJournalEntries(TempCounter).JournalEntry = JournalText
CheckAndWrite()
End If
End Sub
Private Sub CheckAndWrite()
Dim Reader As New StreamReader(MyName & ".txt", False)
Dim Sline As String = Reader.ReadLine
Counter = 0
Do Until (Sline Is Nothing) 'Perform the code until the line in the text file is blank
If Not Sline Is Nothing Then 'If the line in the text file is NOT blank then
For i As Integer = 1 To 3
Select Case i
Case 1
arrJournalEntries(Counter).TheDate = Sline
Sline = Reader.ReadLine
Case 2
arrJournalEntries(Counter).Title = Sline
Sline = Reader.ReadLine
Case 3
arrJournalEntries(Counter).JournalEntry = Sline
Sline = Reader.ReadLine
End Select
Next
End If
JournalDate = arrJournalEntries(Counter).TheDate
Time = DateTimePicker1.Value
MsgBox("Journal Date = " & JournalDate & vbCrLf & "Today's Date = " & Time)
If Time = JournalDate Then
JournalFound = True
Else
Counter += 1
JournalFound = False
End If
Loop
Reader.Close()
Try
If Sline Is Nothing Or JournalFound = False Then
MsgBox("Your journal is now going to be created.")
JournalDate = DateTimePicker1.Value
JournalTitle = TitleTxt.Text
JournalText = JournalTextRtxt.Text
arrJournalEntries(Counter).TheDate = JournalDate
arrJournalEntries(Counter).Title = JournalTitle
arrJournalEntries(Counter).JournalEntry = JournalText
Dim Writer As New StreamWriter(MyName & ".txt", True)
Do Until (arrJournalEntries(Counter).TheDate = Nothing)
Writer.WriteLine(arrJournalEntries(Counter).TheDate)
Writer.WriteLine(arrJournalEntries(Counter).Title)
Writer.WriteLine(arrJournalEntries(Counter).JournalEntry)
Counter += 1
Loop
Writer.Close()
End If
If JournalFound = True Then
MsgBox("Your journal is now going to be edited.")
JournalDate = DateTimePicker1.Value
JournalTitle = TitleTxt.Text
JournalText = JournalTextRtxt.Text
arrJournalEntries(Counter).TheDate = JournalDate
arrJournalEntries(Counter).Title = JournalTitle
arrJournalEntries(Counter).JournalEntry = JournalText
Dim Writer As New StreamWriter(MyName & ".txt", True)
Do Until (arrJournalEntries(Counter).TheDate = Nothing)
Writer.WriteLine(arrJournalEntries(Counter).TheDate)
Writer.WriteLine(arrJournalEntries(Counter).Title)
Writer.WriteLine(arrJournalEntries(Counter).JournalEntry)
Counter += 1
Loop
Writer.Close()
End If
Catch ex As Exception
MessageBox.Show("An error has occured" & vbCrLf & vbCrLf & "Original Error:" & vbCrLf & ex.ToString)
End Try
End Sub`
The problem that's occuring is that it's not only writing in the first time wrong. When it's supposed to say it's going to edit, it doesn't, it just says creating. But it just adds on to the file. After pressing the button 3 times with the current date. and the Title being "Test title", and the journal entry text being "Test text". This is what occured.
It should just be
7/10/2012 3:52:08 PM
Test title
Test text
7/10/2012 3:52:08 PM
Test title
Test text
the whole way through. but of course if it's the same date then it just overwrites it. So can anybody please help me?
You are only filtering your array by the date, so it looks like you have an object with a date but no title or text:
Do Until (arrJournalEntries(Counter).TheDate = Nothing)
The "quick" fix:
Do Until (arrJournalEntries(Counter).TheDate = Nothing)
If arrJournalEntries(Counter).Title <> String.Empty Then
Writer.WriteLine(arrJournalEntries(Counter).TheDate)
Writer.WriteLine(arrJournalEntries(Counter).Title)
Writer.WriteLine(arrJournalEntries(Counter).JournalEntry)
End If
Counter += 1
Loop
Do consider getting rid of the array and using a List(of JournalEntry) instead. Your code looks difficult to maintain in its current state.