Reading Only Words in Text File in Visual Basic - vb.net

I'm working on a project for my Visual Basic class in which I'm supposed to read in a file and display the information (employee names and salaries) in a list box.
I have a total of 4 forms.
The first form doesn't actually display anything, it simply has the menu items to open the file, select any of the other 3 forms, and exit the form.
In the second form (names), only the employee names read in from the file are displayed in the list box.
In the third form (salaries), only the employee salaries read in from the file are displayed in the list box.
The fourth form is basically just like the second and displays the employee names read in from the file.
The problem is, I don't know how to do this so that only certain parts of the file are displayed in the list boxes (names and salaries). Also, in the fourth form I have to ask the user to enter the amount of months that they would like to calculate the salary for the selected employee and then multiply their salary by the number of months entered by the user. I know how to do this, except for how I would go about getting the salary. For example, I'm thinking it would be something like this:
lblTotal.Text = dblSalary * intMonths
But I don't know how to store just the salary of the selected employee in the dblSalary variable?
Here's what code I have written so far, however it's simply opening an open file dialog box when the user clicks File->Open from the main form
Public Class Main
Private Sub OpenFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenFileToolStripMenuItem.Click
Dim open As New OpenFileDialog
open.Filter = "text files |*.txt|All Files|*.*"
open.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
End Sub
End Class
And here is my text file titled employees.txt
Steve McGarret
1500.00
Danny Williams
1300.00
Matthew Casey
1700.00
Kelly Severide
1750.00
I hope that's clear, if not let me know and I can try to clarify.
Thanks in advance.

Since this is homework, I won't post a specific solution, but I'll post the idea of how I would do it...
I would:
Create 2 new List variables - Dim Names as new List(Of String) & Dim Salaries as new List(Of Double)
Read the text file line by line and, given the file format, each even line will add to the Salaries list and every odd line will add to the Names list
Set each list as the DataSource for the appropriate listbox
Hope this helps and gives you an idea of how to proceed, at the very least.
UPDATE:
Given your comment saying you wanted to store the data in a class, you could do something more along the following:
Create your Class with the 2 attributes and whatever other methods you need - Call it EmployeeData, say
Create a List Of(EmployeeData)
Loop through the text file 2 lines at a time (meaning read in the first line, capture the name, read in the next line, read in the salary, THEN loop)
In each loop, once you've captured the data, add a new EmployeeData to your list with the 2 pieces of captured information.
Write a small bit of code that will extract from your EmployeeData list only the salaries or only the names as a new list that you can use to bind to your listbox as the datasource.
Hope this makes sense.

Related

Search text file and put matching results in listbox

I have a VB.NET project in which there is a form where there is a TextBox control, a ListBox control and an external text file that contains a list of outlook folder paths for client emails.
Essentially, the user enters into the text box the name of a client and/or their unique reference number, presses the search button (yes - I know I could make the results appear as they type, I want a button!) and it comes up with the matching results for the company name or serial number that are in the text file and puts them in the list box, with the full path of the outlook email folder.
For example:
If I put into the textbox: "06967759-274D-40B2-A3EB-D7F9E73727D7"
It would put the following result into the listbox:
"EIS Admin\Contacts{06967759-274D-40B2-A3EB-D7F9E73727D7}"
And the user can then go to that folder and find the email(s).
I have gone through several revisions both of my own code and code pasted from online with people having the same issue, only to have Visual Studio throw no errors, run the code and have no luck, with it doing nothing but clearing the list box, and not showing matching results of any kind.
I understand this may be a repeat question but I am extremely confused, can't get anything to work and need some help regarding my issue.
Here is the current code (from online - not mine):
lbx_OFL_Results.Items.Clear()
Dim i As Integer
For i = 0 To lbx_OFL_Results.Items.Count - 1
If i > lbx_OFL_Results.Items.Count - 1 Then Exit For
If Not lbx_OFL_Results.Items(i).Contains(tbx_FindText.Text) Then
lbx_OFL_Results.Items.Remove(lbx_OFL_Results.Items(i))
i -= 1
End If
Next
The list box is called "lbx_OFL_Results"
The textbox is called "tbx_FindText"
I start by clearing the list box of all items (when the form loads, it fills the list box will all lines of the text file, so I need to clear it).
Form Load Event Code:
Dim lines1() As String = IO.File.ReadAllLines("C:\ProgramData\WPSECHELPER\.data\Outlook Folder Wizard\outlookfolders.txt")
lbx_OFL_Results.Items.AddRange(lines1)
For the rest of the code it seems to be doing some form of a 'sort search' then removing any excess results.
If anyone can suggest edits to my code, or new code then that would be sublime.
Thanks.
Thanks to #Jimi for the answer.
Code:
listbox.Items.Clear()
listbox.BeginUpdate()
For i as Integer = 0 To lines1().Length - 1
If lines1(i).Contains(searchbox.Text) Then
listbox.Items.Add(lines1(i))
End If
Next
listbox.EndUpdate()
I have another question which solves how to make this search non case-sensitive. It can be found here.

linking a cell in a userform

I have a user form that posts scores to a person's file. The way it works is the person starts by entering their name and then answers a series of 13 questions. The UserForm is attached to TestingFile1 and once they are finished they click a button and it "posts" the results to their file Example X:rep testing\2018 reps\SchJ.xlms (The test file is located in a different directory) the cell I would like to link to takes all the results and shows an average. The data goes to the rep but the form goes to the rep's supervisor. I would like to add a box on the testing form that once the rep enters their name, based on the name it would show their current average in the form (this would be before the current test. That current cell in the Reps file that holds the running average is o26.
Having never tried this before this is what I currently have but it's not going to work.
Private Sub TextBox1_Change()
If CSRep.Text = "Alyssa Vangundy" Then
Textbox1.Text = X:\Audit Tracking\Reps\2018 Reps\[VanA.xlsx]VanA'!O26
End Sub

Auto Find and display objects when input keywords in VB

I'm a newbie in VB form programming. I got a task today, it seems so hard for me. basically, I have 2 forms named: Form1 and Form2. Form1 use for management info of students such as: student's code, name, phone number, etc..i have a button in Form1, when I press it, Form2 will be invoked. i have completed it.
the problem is: in Form2, I just have 1 textbox and 1 listbox. how should it work? => when I type in textbox with 1 keyword relate to info of students in Form1, Listbox will display automatically all of students's name that relate to the keyword I typed . I didn't know any idea to do it automatically. anyone can help me please give me a way. Thanks you so much.
Based on the solution which you use to store list of students there could be different possible solutions. But for all of them, using TextChanged event of that TextBox you can set the filter for the main list which contains students.
If you use a DataTable to store students, you can use DefaultView.RowFilter of the DataTable:
dt.DefaultView.RowFilter = string.Format("Name Like '{0}*'", TextBox1.Text)
Also if you are using generic lists for storing students, you can use linq to do so:
var data = list.Where(Function(s) s.Name.StartsWith(x.Name.StartsWith(TextBox1.Text)) _
.ToList()
listBox1.DataSource = New BindingList(Of Student)(data)
Also if interaction between forms was an issue for you, take a look at this post.

Write individual listbox items into different text boxes and repeat until all text boxes are full

I'm programming in Visual Basic.
I have one form.
Form 1 contains:
nameTextBox
addNameButton
namesListBox
generateButton
week1TextBox
week2TextBox
week3TextBox
week4TextBox
The user needs to go to Form 1 and type a name in the text box, then add the name to the List Box. The user will add 4 names to the List Box. So, the ListBox will contain the names: Adam, Brenda and Carol.
When the generateButton is clicked, the 3 names have to be written to the text boxes in that order. So week1TextBox should contain "Adam", week2TextBox should contain "Brenda", etc... but once the last name (in this case "Carol") is written into the text box, the loop should start over. Ultimately, there may be up to 50 week text boxes (so week50TextBox). So the loop needs to repeat over and over.
As there is a lack of source code in your question, I'm really not sure exactly how the layout should look, I can only offer some advice/suggestions.
I would recommend creating your listbox control, input textbox, and button to add names to the listbox. In addition to these, though, also add a scrollable panel. (Not sure what the exact term for that control is in VB.net; it's been a long time since I've worked with that language.) Because it sounds like there might be a variable number of items on the panel, when the user goes to generate the list of names, I would use the following rough pseudocode:
Dim OutputTexts As New ArrayList ' This is only here if you want to work with these textboxes later
Private Sub CreateOutput() Handles btnGenerate.Click
pOutputPanel.Controls.Clear()
OutputTexts.Clear()
Dim NextX As Integer = 0 ' Pretty much unnecessary value, but included in case you want to mess with this
Dim NextY As Integer = 0
For i As Integer = 0 To Convert.ToInt32(txtWeekCount.Text)
Dim txtName As New TextBox
txtName.Text = lbNameList.Item(i Mod lbNameList.Items.Count)
txtName.Location = new Point(NextX, NextY) ' Play with this as necessary
NextY += 50 ' Play with this as necessary
OutputTexts.Add(txtName)
pOutputPanel.Controls.Add(txtName)
Next
End Sub
Again, this is very much pseudocode, so I would not encourage copying and pasting, but give it a read, make sure you understand all of it, and then try implementing something similar. There might be an easier way to do it, but I have not programmed in VB.NET in probably over 2 years (at least). Nonetheless, the most important thing in here is the following line: lbNameList.Item(i Mod lbNameList.Items.Count). By Mod-ing your indexing variable, you will be accessing items sequentially, and then repeating from the start of the ListBox items collection once i is out of range.
I would also encourage you to dynamically generate your TextBox controls as needed rather than manually adding in 50 or more TextBox controls.

Extracting Substrings from textboxes in VB

I am working on a VB program for school. I am having some trouble extracting a substring from a string and I would really appreciate some help.
The form has different text boxes and one of them is where you type in a person's full name into one text box. The listbox on the form, when hitting the compute button, is supposed to display only the person's last name.
I am not sure how I am supposed to extract just the last name of the string of whatever name is typed into the text box.
All I got so far is:
Dim name As String
name = txtName.Text
(txtName is the name of the text box)
Okay, so I added:
lstOut.Items.Add(name.Substring(6))
That to my code. The name I typed in for an example when I ran the program was Helen Woods. 6 is in the substring because that is where the space starts and when I clicked compute, it listed only the last name, just like I wanted. But, this only works if the first name is five letters long. I need a way to make the program automatically find the space in between the two names.
EDIT:
When I add:
lstOut.Items.Add(name.IndexOf(""))
The listbox gives me a 0 whenever I type in a name and hit the compute button.
try this:
Private Sub GetLastName()
dim lsName as new List(Of String)
dim name as string
for each name in txtName.text.split(" ")
lsName.Add(name)
next
lstOut.items.Add(lsName.item(lsName.count-1))
end sub
You can call this procedure at your button event.