Visual Basic find and display - vb.net

I have made a program in Visual Basic. It's a contact book where the user can enter a friend's name as the key and contact which is stored as a value. Both are entered via TextBoxes and the information is then displayed in a ListBox.
The following code is for my search address button where I enter the contact name in TextBox4 and it displays the address in TextBox3. The problem I'm having with my code is that after I enter more contacts into my ListBox and I click the find button it does display the address, but also the error message which is intended to be used if the contact does not exist.
I'm a beginner to programming and looking to see what is wrong with my code.
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox3.Clear()
For Each x In MyAddressBook
If TextBox4.Text.Contains(x.Key) Then
TextBox3.Text = x.Value
Else
If TextBox4.Text <> x.Key Then
MessageBox.Show("error")
End If
End If
Next
End Sub

You probably only need to stop the For Each loop when you were successful in your search. Add a Exit For after TextBox3.Text = x.Value. Since you found the address there's no need to look further.

Related

MessageBox appears at wrong time

I have a form which contains a PctureBox. When the user clicks this PictureBox, they will proceed to another form.
In the other form, I have a DataGridView and I have a search TextBox for the user to search content in the DataGridView. However I want to display "no record found" when the user enters something that is not in the database.
The MessageBox displays "no record found" however it also does this when the user first clicks on the PictureBox and the form loads.
Code:
Private Sub TextBox3_TextChanged_1(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
If Me.MasterlistTableAdapter1.FillBySearchProject(Me.DocumentDataSet.masterlist, TextBox3.Text)=0 Then
System.Windows.Forms.MessageBox.Show("No Record has been Found")
End If
End Sub
When I created a Button this code works perfectly. The prompt only displays when no records are found on the search.
I don't want to use a Button to do the search. My search functions when a user only types several words, data found.
Try this
Private Sub TextBox3_TextChanged_1(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
If Not TextBox3.Text = "" Then
If Me.MasterlistTableAdapter1.FillBySearchProject(Me.DocumentDataSet.masterlist, TextBox3.Text)=0 Then
System.Windows.Forms.MessageBox.Show("No Record has been Found")
End If
End If
End Sub

displaying label in different form with if else, vb.net

i have 2 forms. let's say form2 has a label that is not visible but with a particular "if" statement in form1, it will show up? here's my current code in form1 that isn't working:
Private Sub btnEnterPromoCode_Click(sender As Object, e As EventArgs) Handles btnEnterPromoCode.Click
Dim pcode As String
pcode = InputBox("Please enter any promo code below.", "Promo Code")
If pcode = "05567" Then
Dim resultz As Integer = MessageBox.Show("You have entered the 'Promo of the Week'.", "Promo Code Successful", MessageBoxButtons.OK)
frmCheckOut.lblFree.Show()
ElseIf pcode = "66795" Then
Dim resultx As Integer = MessageBox.Show("You have entered the 'Christmas Promo'.", "Promo Code Successful", MessageBoxButtons.OK)
frmCheckOut.lblFree.Show()
ElseIf pcode = "" Then
MessageBox.Show("You have entered a wrong code", "Promo Code Unsuccessful", MessageBoxButtons.OK)
Else
MessageBox.Show("You have entered a wrong code", "Promo Code Unsuccessful", MessageBoxButtons.OK)
End If
End Sub
sorry, kinda new with vb.net. thanks!! and anyway, in my code, checkout is the form2.
I suspect that frmCheckOut is a class name, not a reference to an instance of the class. When loading the second form, save a member-level or global reference to it. then use that reference in the call to Show.
This works for me:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim f As New Form2
f.Show()
f.Label1.BackColor = Drawing.Color.PeachPuff
f.Label1.Show()
End Sub
Having said that, I would have never thought to use f.Label1.Show() I would have used: f.Label1.Visible = True (I also normally don't use PeachPuff as a color, but that's a story for a different day).
As #JerryM already said, it looks like you are calling the form directly and not an instance of it (which is what I did in my example). That makes me think you may have to rethink the design of the application a little...

Customer Info Form sent to .txt file on desktop via button click

Good evening,
I am attempting to finish this last little bit of my VB.net project for school. It's the good ol' pizza ordering system. After the welcome screen the user is prompted to enter first name/last name/email and then click submit (if they leave any box empty it will pop up w/ msgbox). I need to send the information that is input into textbox1, textbox2, and textbox3 into a .txt file on the desktop if possible.
Here is the code
Public Class CustomerInfo
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
MsgBox("Please Enter Your Information")
' Forces user to input something in to all three boxes before being allowed to proceed
'Prompts user to complete form with a message box.
'Once information is complete, clicking the submit button will close this form and open the Selection Form.
Dim desktopPath As String =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim fileName As String = "customer.txt"
Dim fullPath As String = IO.Path.Combine(desktopPath, fileName)
IO.File.AppendAllLines(fullPath, {"textbox1.text & textbox2.text & textbox3.text"})
else
Selection.Show()
Me.Hide()
End If
End Sub
I attempted to upload a screenshot of the windows form, but am new to the forum. It's very basic, text boxes 1, 2, and 3 with a "Submit" button below them.
Thanks in advance for any assistance provided.
Kyle

How to use two forms to search in one and display the records to the other using Visual Basic 2010, SQL Server 2008

I have 2 forms in my visual basic 2010 project. Both are connected with an SQL 2008 Server where i keep a db with customers records(name,surname,address, etc).
What i want to do is to give certain criteria in the first form, lets say name,and hit a search button. The program should search the db if the specified name exists.If it exists i want to show the results in the second form. By results i mean the name , surname, address,etc. If it does not exist i want to prompt the user to insert data into the db.
The problem is that i know how to do this work in a single form but not how to make the two forms "communicate" with each other (meaning that one does the search and the other the projection-adding part).
Any help is appreciated
Edit
I am sorry guys but i cannot figured it out. I tried setting the filters but all i get is errors. I will post whatever i have written so far in case someone can help me out. So here it is.
As i stated earlier i know how to search for the data in a single form(lets call it DisplayForm). As Olivier mentioned i have textboxes for the records, a combo box with the search criteria(name , surname,id) and a Search button that perfoms the search and populates the text boxes. The code for the search button is this:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim intPosition As Integer
'Combo box code
Select Case cboField.SelectedIndex
Case 0
ObjDataView.Sort = "surname"
Case 1
ObjDataView.Sort = "id"
End Select
If cboField.SelectedIndex = 0 Then
intPosition = ObjDataView.Find(txtSearch.Text)
ElseIf cboField.SelectedIndex = 1 Then
intPosition = ObjDataView.Find(CType(txtSearch.Text, Integer))
End If
If intPosition = -1 Then
MessageBox.Show("No customer found", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
Else
objCurrencyManager.Position = intPosition
End If
End Sub
I want this procedure be done in a second form (lets call it FormSearch) and populate the textboxes in form DisplayForm. I know that it is easy enough for someone that knows VB but i am tottally a newbie and desperatly need help.
Thanks a lot for any advice
In your display form add this code
Public Sub SetFilter(name as String)
' Set the filter here
End Sub
In your filter form do this
DirectCast(Application.OpenForms(0), DisplayForm).SetFilter(NameTextBox.Text)
Here I assume that DisplayForm is your main form and that it's name is DisplayForm. You would call this in a button click event hanlder, for instance.
EDIT in response to comment:
You said that you know how to do it on a single form. I do not know how you do it, but probably you have textboxes for the name, the address, etc. that you are looking for. Then probably you press a button called btnSearch. By pressing this button you trigger
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch_Click.Click
' The search logic goes here
End Sub
What I am telling you is to replace btnSearch_Click by Public Sub SetFilter(name as String) that is public. You would move those search fields and the button to the second form and from there call SetFilter of the first form in order to communicate with it and tell it to search a customer. In my example, I pass only a name, but I left it up to you to pass more parameters as required.
Assuming that your customer form is called "DisplayForm", you could access it like this
Dim cust As DisplayForm
cust = DirectCast(Application.OpenForms("DisplayForm"), DisplayForm)
cust.SetFilter(NameTextBox.Text)
My answer was only about the communication between the two forms, assuming that you already managed the other part.
Searching for "VB.NET Tutorial: Working With Data" on YouTube results in six videos on this subject. Video #3 in particular shows how to use filters.
EDIT #2. Here is a little more help:
I do not know how you are opening the two forms. Since you need a reference to the display form in the search form, it is easier to open the search form as the main form at application startup. At Form_Load in the search form, you then open the display form by code
Code in the search form:
' Reference to the display form
Private _displayForm As DisplayForm
' Create the display form and open it
Private Sub SearchForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
_displayForm = New DisplayForm()
_displayForm.Show()
End Sub
' Get the search parameters and tell the display form to set the filter
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
Dim field As String, search As Object
Select Case cboField.SelectedIndex
Case 0
field = "surname"
search = txtSearch.Text
Case 1
Dim n As Integer
field = "id"
If Integer.TryParse(txtSearch.Text, n) Then
search = n
Else
MessageBox.Show("Please enter a number for id search", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
Return
End IF
End Select
_displayForm.SetFilter(field, search)
End Sub
Code in the display form that actually performs sorting and searching:
Public Sub SetFilter(ByVal field As String, ByVal search As Object)
Dim intPosition As Integer
ObjDataView.Sort = field
intPosition = ObjDataView.Find(search)
If intPosition = -1 Then
MessageBox.Show("No customer found", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
Else
objCurrencyManager.Position = intPosition
End If
End Sub

Help with events

I have multiple textboxes which I want them to perform the same thing upon clicking them. By default I can use the handles textbox1.click for 1 single textbox as shown below but I am not sure how to do handle multiples of them. Of course I can write a handler for every single textbox but I have about 50 of them. I am sure there must be a more efficient way. Please advice. Thanks.
Sub TextBox1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click
If Button9.Text = "Make Changes" Then
If TextBox2.Text <> "" Then
Frm_Cine1.Show()
Frm_Cine1.chooseCine(ComboBox1.SelectedItem)
Else
MsgBox("Please check input!")
Exit Sub
End If
End If
End Sub
why don't you create a customizable textbox?
If Button9.Text = "Make Changes" Then
If TextBox2.Text <> "" Then
These two lines are going to be same for all those 50 buttons?
If yes, then I think you can assign same event handler to each of the button's click event.
Other way is, create one private method which takes one string as an argument and returns boolean value depending on whether your string is blank or not and call this method from all the 50 button's click event.
Thanks for all your advices, I am not sure if this is what you guys are suggesting but apparently it is how I wanted it to work:
Sub TextBoxs_click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles TextBox2.Click, TextBox3.Click, TextBox4.Click 'This part is disturbing if I have 50 textboxes...
'For Each obj As Control In Panel2.Controls
If sender.GetType.ToString = "System.Windows.Forms.TextBox" Then
Dim txtbox As TextBox = sender
textbox_verification(txtbox)
End If
'Next
End Sub
Sub textbox_verification(ByVal txtbox As TextBox)
If Button9.Text = "Make Changes" Then
If txtbox.Text <> "" Then
Frm_Cine1.Show()
Frm_Cine1.chooseCine(ComboBox1.SelectedItem, "FILE1-->This should be a variable")
Else
MsgBox("Please check timings input!")
Exit Sub
End If
End If
End Sub
If you indeed need to use the same click handler for multiple test boxes, you can use the AddHandler command to associate the click event of each test box with the handler routine, as shown:
AddHandler TextBoxX.Click AddressOf TextBox1_Click
You will need to add this statement to your program (maybe in the form load routine) once for each text box you want to handle. (Using the name of each text box in place of the "TextBoxX" in the above code.)