简体   繁体   English

在Visual Basic中仅读取文本文件中的单词

[英]Reading Only Words in Text File in Visual Basic

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. 我正在为我的Visual Basic类开发一个项目,该项目应该在其中读取文件并在列表框中显示信息(员工姓名和工资)。

I have a total of 4 forms. 我总共有4种表格。 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. 第一种形式实际上不显示任何内容,它仅具有菜单项以打开文件,选择其他3种形式中的任何一种并退出该形式。 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 lblTotal.Text = dblSalary * intMonths

But I don't know how to store just the salary of the selected employee in the dblSalary variable? 但是我不知道如何仅将所选雇员的薪水存储在dblSalary变量中?

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 这是我到目前为止编写的代码,但是,当用户从主窗体中单击File-> Open时,它只是打开一个打开文件对话框

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 这是我的文本文件,名为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: 我会:

  1. Create 2 new List variables - Dim Names as new List(Of String) & Dim Salaries as new List(Of Double) 创建2个新的List变量-将Dim Names as new List(Of String)Dim Salaries as new List(Of Double)
  2. 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 逐行读取文本文件,并在给定文件格式的情况下,每条偶数行将添加到Salaries列表中,而每条奇数行将添加到Names列表中
  3. 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: 鉴于您的评论说您想将数据存储在一个类中,您可以在以下方面做更多的事情:

  1. Create your Class with the 2 attributes and whatever other methods you need - Call it EmployeeData , say 使用2个属性以及所需的其他任何方法创建您的Class-将其EmployeeData ,例如
  2. Create a List Of(EmployeeData) 创建一个List Of(EmployeeData)
  3. 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) 一次循环浏览文本文件两行(意思是在第一行中读取,捕获名称,在下一行中读取,在salary中读取,然后循环)
  4. In each loop, once you've captured the data, add a new EmployeeData to your list with the 2 pieces of captured information. 在每个循环中,一旦您捕获了数据,便将新的EmployeeData以及捕获的2条信息添加到列表中。
  5. 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. 编写一小段代码,将其从EmployeeData列表中仅提取薪水或姓名作为新列表,您可以将其用作数据源绑定到列表框。

Hope this makes sense. 希望这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM