How to create a new sequential id (i.e. 1 for first id, 2 for second id, 3 for 3rd id, etc.). You could use this for a customer id, booking id, stock id, etc.
NOTE: For both of these methods to work, you need to manually enter a dummy record into the textfile in NotePad. Give the dummy record an ID of 1 and enter other dummy fields (on the same line) if you wish.
NOTE: The .Length gives you how many records are in the file
For example, in this file, there are two records, so (length)count = 2. Then 1 is added to make count = 3, which is output to a label. A variable could also be used to store the new id (count), before writing out the new record (with the new ID of 3) to the textfile.
Loop through names.txt, storing theUser ID (first 10 chars in textfile).
When you drop out of the loop, you will have stored the last User Id, to which 1 can be added.
In this example, the next record would be created with a UserID of 4
How to access a value across ALL forms in a VB project:
Add a new item (module), which will appear in your Solution Explorer in the top right of your IDE.
Declare a variable in the module. You will be able to access this variable from anywhere in your project. In this example, when the user logged on, they stored txtUserName.txt into the Username variable (in the module), then it can be referenced in other forms.
Sometimes, we don't want to use a button to run code, we may want to input data into a textbox, then press a key on the keyboard, such as the Enter Key, or the Space Bar, to run our code. We can recognise any keyboard key in our code, however, it is important to note that the trigger and type of procedure event we need to use is different.
I want to enter my name, then press the Enter Key (instead of a button) to run my code.
Double click on your textbox, which gives you a txtEnterName_TextChanged procedure.
This procedure will run every time any characters are entered into the textbox. This is not quite right for our EnterKey procedure, so we have to change the procedure/event type.
Change the TextChanged event to a KeyUp event:
It gives you a new KeyUp procedure/event; you can delete the TextChanged procedure, as you don't need it. This is what your procedure should now look like:
Once you have this procedure, you can write code to recognise different keys on the keyboard. The code below recognises the Space Bar (Keys.Space) and the Enter Key (Keys.Enter). Try typing Keys. and you will see you can choose any keyboard key.
You can also recognise key presses using the ASCII values for each individual key on a keyboard. This time, however, we have to change our procedure to a KeyPress event.
The code below recognises when the Enter Key is pressed using it's unique ASCII value of 13. However, we can also recognise letters and other keys pressed when in a textbox. In the example below, if a small letter 'a' is entered, a message is produced. This could be useful if you wanted to count the number of times a particular letter is entered.
This code allows you to count down seconds, minutes and hours
Dim Countdown As Integer = 100 'This is the total number of seconds the timer has to countdown. e.g. 100 secs = 00:01:40
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'set countdown label to max no of hours/minutes/seconds, as set in Countdown variable, above.
lblCountdown.Text = GetTime(Countdown)
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
tmrCountdown.Interval = 1000 'The number of miliseconds in a second
tmrCountdown.Enabled = True 'Start the timer
End Sub
Private Sub tmrCountdown_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrCountdown.Tick
'if time has run out, put out message and delete booking record
If Countdown = 0 Then
tmrCountdown.Enabled = False 'stop the timer
MsgBox("Sorry, you have timed out. Please reselect your tickets and make your purchase within 90 seconds")
'Here is where you work out how to delete a record from your booking file.
Else
'if time left then deduct 1 second and redisplay the timer
Countdown = Countdown - 1
lblCountdown.Text = GetTime(Countdown)
End If
End Sub
Public Function GetTime(ByVal Time As Integer) As String
'this procedure takes the current value of the countdown and converts into hours:minutes:seconds
Dim Hrs As Integer 'number of hours '
Dim Min As Integer 'number of Minutes '
Dim Sec As Integer 'number of Sec '
'Seconds'
Sec = Time Mod 60
'Minutes'
Min = ((Time - Sec) / 60) Mod 60
'Hours'
Hrs = ((Time - (Sec + (Min * 60))) / 3600) Mod 60
Return Format(Hrs, "00") & ":" & Format(Min, "00") & ":" & Format(Sec, "00")
End Function
What you need to do:
1. Draw a CHART from the VB toolbox.
2. This line of code needs to be at the very top of your code.
Imports System.Windows.Forms.DataVisualization.Charting
3. This code needs to be under a button or some other object.
'This will allow the user to view the leaderboard in a graph format.'
Dim s As New Series
Chart1.Series.Clear()
s.ChartType = SeriesChartType.Bar
'This creates the data as a bar graph.'
s.BorderColor = Color.Black
'This will set the graph to a black background.'
s.BorderWidth = 2
s.Name = "Fouls"
s.Color = Color.Green
'This makes the bar on my chart appear in green.'
Chart1.Titles.Add("Top Foulers")
'This gives a title to my graph.'
s.Points.AddXY(LblFirstname0.Text & LblSurname0.Text, LblYellowCard0.Text)
s.Points.AddXY(LblFirstname1.Text & LblSurname1.Text, LblYellowCard1.Text)
s.Points.AddXY(LblFirstname2.Text & LblSurname2.Text, LblYellowCard2.Text)
s.Points.AddXY(LblFirstname3.Text & LblSurname3.Text, LblYellowCard3.Text)
s.Points.AddXY(LblFirstname4.Text & LblSurname4.Text, LblYellowCard4.Text)
s.Points.AddXY(LblFirstname5.Text & LblSurname5.Text, LblYellowCard5.Text)
'This will take the data from my labels and display them in a graph.'
Chart1.Series.Add(s)
This procedure allows the user up to 3 attempts at entering the password "secret".
The user is told which attempt they are currently on (1,2 or 3).
The user is informed their login is successful at the end, if they get it within 3 attempts, or their login is unsuccessful if they don't.
Public Class frmPasswordChecker
Private Sub cmdEnterPassword_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEnterPassword.Click
Dim Password As String
Dim InputPassword As String
Dim attempt As Integer
Password = "secret"
attempt = 0
'This loop will repeat until either the user has tried to enter their password 3 times, or their password is correct.
Do
attempt = attempt + 1
InputPassword = InputBox("Enter password. This is attempt number " & attempt)
'InputPassword = txtpassword.text
'MsgBox("Enter password. This is attempt number " & attempt)
'Application.DoEvents()
Loop Until attempt = 3 Or InputPassword = Password
'This code tells the user if their login has been successful or not.
If InputPassword = Password Then
MsgBox("This password is valid. You have logged on successfully.")
Else
MsgBox("This password is invalid. Please see your administrator.")
End If
End Sub
End Class