Visual basic error message for blank field, password and data retrieval system - vb.net

I am writing a program in visual basic that, when a user enters their email and password (from a list of email and passwords in a CSV file) it will give them their name, address, password etc. I have successfully achieved this however when I input nothing into the field the program simply enters 5 blank lines, how do I fix this? I need an error message to say that nothing has been input and for it to reject it. How do I do this? My code is below:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Password.Click
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles EmailAddress.Click
End Sub
Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
Dim currentRow As String()
Dim foundmatch As Boolean
Using parserDetails As New Microsoft.VisualBasic.FileIO.TextFieldParser("CSV_File.csv")
parserDetails.SetDelimiters(",")
While Not parserDetails.EndOfData
currentRow = parserDetails.ReadFields()
If EmailAddress.Text = currentRow(0) And Password.Text = currentRow(1) Then
Me.DataGridView1.Rows.Add(currentRow)
foundmatch = True
End If
End While
End Using
If Not foundmatch Then
MsgBox("The email and/or password entered cannot be found or is incorrect.", MsgBoxStyle.Critical, "Invalid Email")
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Input(p1 As String)
Throw New NotImplementedException
End Sub
End Class

You'd use the If construct to check for conditions. For example...
Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
If String.IsNullOrWhiteSpace(EmailAddress.Text) Then
MsgBox('Email Address is required!')
Return
End If
' The rest of your logic
End Sub
You can check multiple conditions at once, sequentially, etc.
A couple of notes:
(I realize this is just a learning exercise but...) If you can load the file into a database-like structure (DataTable?) then you may be able to perform more effective searches than just scanning line-by-line. (Or if you could use an actual database, even better.) For small amounts of data it may not be a big deal, but scaling to larger amounts will make a difference.
FYI - Storing user passwords in plain text is a famously bad idea. While you're learning and practicing, it would be good to familiarize yourself with password hashing. Store the hash of the password (not encrypted, hashed) and when a user enters their password you hash their input and compare the hashes. It should be impossible for you or anyone else to be able to recover and see the original password.

You would want to make use of something like this:
If(Not String.IsNullOrWhitespace(EmailAddress.Text)) Then
'put
Else 'null/empty or white space
Msgbox("You cannot leave the email address empty...")
End If
I.e. you could change your while block to this:
While Not parserDetails.EndOfData
currentRow = parserDetails.ReadFields()
If (Not String.IsNullOrWhiteSpace(EmailAddress.Text)) Then
If EmailAddress.Text = currentRow(0) And Password.Text = currentRow(1) Then
Me.DataGridView1.Rows.Add(currentRow)
foundmatch = True
Exit While
End If
Else 'null/empty or white space
MsgBox("You cannot leave the email address empty...")
foundmatch = False
Exit While
End If
End While
Note the addition of the Exit Whiles, which makes it more efficient because it no longer needs to loop after the correct values are found.

Related

Barcode scanning to a listbox checking for duplicates

Good day!
I want to add some strings from a barcode scanner, captured in a text box, to a list box, and, before adding it, to check if the specific string hasn't been already added. So I have a text box called txtWO which captures what the reader scans and a list box called lstScanBOM to which I add the text box string if the item is not already added. The problem is, that whatever I do, only after the specific string is added twice the checking for duplicate entry starts to work. In other words I scan the same string twice, it added it, and then when I scan the third time only it throws the message with the error saying it is a duplicate. I don't understand why is doing this. The code is below:
Private Sub frmValidareFIP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If txtWO.Focused = False Then
txtWO.Select()
End If
End Sub
Private Sub AddUnique(StringToAdd As String)
If lstScanBom.Items.Contains(StringToAdd) = True Then
MsgBox("Articol duplicat!", vbOKOnly)
Else
'it does not exist, so add it..
lstScanBom.Items.Add(StringToAdd)
End If
End Sub
Private Sub txtWO_KeyDown(ByVal sender As Object,ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWO.KeyDown
If e.KeyCode = Keys.Enter Then
Dim barcode As String
barcode = txtWO.Text
AddUnique(barcode)
txtWO.Clear()
txtWO.Focus()
End If
End Sub
IMO Try listing the data outside of the ListBox. I can't see why it isn't working, maybe we need a third pair of eyes to see it!?
Try adding a list (of string) as a Private within the form, populate this as your user scans, and check the duplicate there..
This is definately not the best solution, but I'm sure it will help!
Private List_Barcodes As List(Of String)
Private Sub frmValidareFIP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
List_Barcodes = New List(Of String)
'You can also populate this list on load, if you have a stored cahce of previous scanned barcodes?
'List_Barcodes.Add("0123456")
'List_Barcodes.Add("4567890")
'...etc
If txtWO.Focused = False Then
txtWO.Select()
End If
End Sub
Private Sub AddUnique(StringToAdd As String)
If List_Barcodes.Contains(StringToAdd) Then
MsgBox("Articol duplicat!", vbOKOnly)
Else
'Place into dynamic list
List_Barcodes.Add(StringToAdd)
'and Place into your listbox
lstScanBom.Items.Add(StringToAdd)
End If
End Sub
Private Sub txtWO_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWO.KeyDown
If e.KeyCode = Keys.Enter Then
Dim barcode As String
barcode = txtWO.Text
AddUnique(barcode)
txtWO.Clear()
txtWO.Focus()
End If
End Sub
Your barcode reader is returning <carriage return><line feed> as enter. Your code catches the enter key (carriage return = 13), but leaves the line feed (10) character. So the next time you scan something it will start with a line feed. The two strings in your example are different because the first is "58335001" and the second is "<line feed>58335001". The third one is "<line feed>58335001", which is a duplicate of the second.
One way to fix this is to trim your string.
Private Sub txtWO_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWO.KeyDown
If e.KeyCode = Keys.Enter Then
Dim barcode As String
'Add the .Trim() to remove the leading <line feed> character
barcode = txtWO.Text.Trim()
AddUnique(barcode)
txtWO.Clear()
txtWO.Focus()
End If
End Sub
The most simple decision is ONLY to make your textBox control txtWO NOT multiline
And that's enough! Your code will work correctly!

Matching cards by using .tag

In this game I am creating, I have a set of "cards" as pictureboxes set to a random lot of images. When the game starts, the images in the pictureboxes are hidden and the user has to guess which image was in each card. I am having trouble with finding if the user's guess matches what was in the actual card.
In the code below, I am using a listbox to score the names of images in which the user can guess from.
https://imgur.com/a/xCg8X
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ClickedCard Is Nothing Then 'Make sure that a card has been clicked, otherwise the below code will fail.
MsgBox("You must select a card.")
Return 'Do not continue execution of this code.
End If
btnSubmit.Visible = True
ClickedCard.Image = imglist1.Images(ListBox1.SelectedIndex)
If ClickedCard.Tag = ListBox1.SelectedIndex Then
roundscore += 1
If roundscore = Cards.Count Then
MsgBox("All right")
End If
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Static AlreadySelected As New List(Of Integer)
If AlreadySelected.Contains(ListBox1.SelectedIndex) Then
MessageBox.Show("Already select once")
Exit Sub
End If
AlreadySelected.Add(ListBox1.SelectedIndex)
'Your other code here
End Sub
The static list will persist between calls to this sub. You will have to clear this list when you go to a new round. I hope this helps with the problem you mentioned in your comments. :-)

How to replace a word in a textbox with another word

I'm writing an article rewriter in VB.NET and I am having a problem in replacing some words with another word.
is there a way i can replace the words directly while the user is typing.
while texting it , i typed "what is love, we always look at it"
and it displayed what is love we frequently look at it
instead of
what is affection we frequently see it
Here is my code:
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
If RichTextBox1.Text.Contains("always") Then
RichTextBox2.Text = RichTextBox1.Text.Replace("always", "frequently")
End If
If RichTextBox1.Text.Contains("love") Then
RichTextBox2.Text = RichTextBox1.Text.Replace("love", "affection")
End If
If RichTextBox1.Text.Contains("look") Then
RichTextBox2.Text = RichTextBox1.Text.Replace("look", "see")
End If 'RichTextBox2.Text = RichTextBox1.Text
End Sub
If I understand the problem correctly, you want to change the text as it's being typed in. You don't want to use the text changed event as it won't occur immediately on typing. Use the keyup event instead.
Private Sub RichTextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyUp
Dim wordToFind As String = "findword"
Dim replaceWord As String = "replaceword"
Richtextbox2.rtf = RichTextBox1.replace(wordToFind, replaceWord)
End If
End Sub

vb.net string lookup in listbox with output as different part of the string

Last Name Finder
what I'm trying to do is create a list of all peoples surnames where the 1st name = #name
although this isn't the exact purpose its easier to try and explain the actual content. i'm aware it may be easier to use a data source, but I want to try and avoid that where possible.
Form_Load
ListBox1.Items.Add("Dean Smith")
ListBox1.Items.Add("John Jones")
ListBox1.Items.Add("David Johnson")
ListBox1.Items.Add("Samantha Thompson")
ListBox1.Items.Add("Claire Frost")
ListBox1.Items.Add("John Brown")
and then some sort of string manipulation to do the following on button_click
if textbox1.text contains "John" then
listbox2.items.add(Jones)
listbox2.items.add(Brown)
else messagebox.show("No matches found")
end if
Thanks for any input.
What I've understood is that you are willing to use your textbox as a filter to the listbox. And this can be achieved by running the query to your db on every TextChanged().
So to give you some guidelines, you can proceed like this;
private Names As List(Of String)
Private Sub Form2_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Names = New List(Of String)
With Names
.Add("Dean Smith")
.Add("John Jones")
.Add("John Brown")
End With
For Each Name As String In Names
ListBox1.Items.Add(Name)
Next
End sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ListBox1.Items.Clear()
For Each s As String In Names
If s.Substring(0, TextBox1.Text.Length).ToLower = TextBox1.Text.ToLower Then
ListBox1.Items.Add(s)
End If
Next
End Sub
I hope this solution might help you achieve what you wanted. You can also apply direct filter to the List by using a delegate function, which will avoid you from looping again on TextChange().

Passing arguments to methods in VB

I'm hoping you guys can help with a problem that should be simple to solve, I've just had issues finding a solution. In the program that I'm writing some of the textbox's have to be numeric between 1 and 10, and others just have to be numeric. Instead of coding each textbox to verify these parameters I decided to write methods for each of them. I'm having problems passing the arguments and getting it to function correctly. Included is some of my code that shows what I'm trying to accomplish.
Public Shared Sub checkforonetoten(ByVal onetoten As Double)
If (onetoten > 1 & onetoten < 10) Then
Else
MessageBox.Show("Please enter a Number between 1-10", "Error")
End If
End Sub
Public Shared Sub checkfornumber(numCheck As Double)
Dim numericCheck As Boolean
numericCheck = IsNumeric(numCheck)
If (numericCheck = False) Then
MessageBox.Show("Please enter a number", "Error")
End If
End Sub
Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) Handles textboxS.TextChanged
Dim S As Double
S = textboxS.Text
checkfornumber(S)
checkforonetoten(S)
End Sub
One of your main problems is you're converting your text without validating it. You're also programming without the Options On to warn you of bad conversion techniques like you're using in the event handler.
The TryParse method would come in handy here:
Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) Handles textboxS.TextChanged
Dim S As Double
If Double.TryParse(textboxS.Text, S) Then
checkforonetoten(S)
End If
End Sub
Since the TryParse method validates your text and sets the value to 'S', you only need to check the range.
Of course using NumericUpDown controls would make all this moot, since the values will always only be numbers and you can set the range on each one.
one way to structure it is to have one event procedure process the similar TB types:
Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) _
Handles textbox1.TextChanged, textbox12.TextChanged, _
Handles textbox16.TextChanged
Dim S As Double
If Double.TryParse(Ctype(sender, TextBox).Text, S) Then
' or place the Check code here for all the TextBoxes listed above
checkforonetoten(S)
End If
End Sub
The plain numeric kind:
Private Sub textboxQ_TextChanged(sender As Object, e As EventArgs) _
Handles textbox2.TextChanged, textbox6.TextChanged
Dim S As Double
If Double.TryParse(Ctype(sender, TextBox).Text, S) = False Then
MessageBox.Show("Please enter a number", "Error")
End If
End Sub
Rather than calling a function and passing the current TextBox from events (which is fine), have 2 or 3 events process them all. The point is adding/moving the Handles clause to a common event procedure (be sure to delete the old ones).
If you do decide to call a common function, dont do anything in the events (you still have one per TB) and do it all in the common proc:
Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) _
Handles textboxS.TextChanged
checkforonetoten(Sender)
End Sub
private Sub checkforonetoten(tb As Textbox)
Dim S As Double
If Double.TryParse(tb.Text, S) Then
' your check for 1 - 10 on var S
else
' error: not a valid number
End If
end sub
Also:
If (onetoten > 1 & onetoten < 10) Then
should be:
If (onetoten > 1) AndAlso (onetoten < 10) Then