读取制表符分隔文件到数组 [英] Reading Tab delimited file to array

查看:98
本文介绍了读取制表符分隔文件到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读本文时请记住,我使用vb.net 4.0编写一个wpf用户控件。我有一个ASCII文本文件,其中包含制表符分隔的客户联系信息(此处称为DB)...

Please keep in mind while reading this, I am using vb.net 4.0 to write a wpf user control. I have an ASCII text file that contains tab delimited customer contact info (herein refered to as DB)...

<vbTab> Customer Name <vbTab> Address 1 <vbTab> Address 2 <vbTab> Phone <vbTab> Fax <vbTab> Cell <vbTab> OtherPhone <vbTab> Contact <vbTab> email



总共有9个字段,其中一些可能在数据库中是空白的。

我需要将每行读入数组拆分每行 vbNewLine

然后每行到一个分组为 vbTab 的数组。



我将使用此数组使用数组中的Customer Name字段填充 Combobox ,另外8个 TextBox 带有相应字段的控件。



ComboBox 更改我希望其他8 TextBoxes 进行相应的更新。



最后,当 ComboBox
手动更改为原始数据库中不存在的值,或者如果客户更改了任何联系信息,我想将新的或修订的记录添加到数据库中。我还想按客户名称对数据库进行排序。

我知道这个问题很多,但我在VBA 6.0中工作并试图转换为.Net 4.0

发布原始VBA代码会有帮助吗?


All in all there are 9 fields, some may be blank within the DB.
I need to read each line into an array splitting each line at vbNewLine,
then each line to an array splitting at vbTab.

I will be using this array to populate a Combobox with the Customer Name field in the array and 8 other TextBox controls with the corresponding fields.

When the ComboBox changes I want the other 8 TextBoxes to update accordingly.

Finally, when the ComboBox is manually changed to a value that is not present in the original DB, or if any contact info is changed for a customer, I want to add the new or revised record to the DB. I also want to sort the DB by customer name.
I know this is asking a lot at once, but I had this working in VBA 6.0 and am trying to convert to .Net 4.0
Would it be helpful to post the original VBA code?

推荐答案

你在VB6中使用了一个结构吗?无论如何.Net非常不同。在尝试将VB6代码直接导入.Net之前,您应该阅读一些关于.Net的文章。



有很多方法可以完成解析分隔文件。下面是一个简单的代码示例:



Were you using a Structure in VB6? Anyhow .Net is very different. You should read some articles about .Net before you attempt to just port VB6 code directly into .Net.

There are many ways to accomplish parsing a delimited file. Below is a simple example of code that will work:

Using fs As StreamReader = File.OpenText("C:\File.txt")
   While fs.Peek() <> -1
	Dim line As String = fs.ReadLine()
	Dim fields As String() = line.Split(New String() {"/t"}, StringSplitOptions.None)
	'Do more stuff like add it to a DataTable, array or generic list
   End While
End Using


您可以使用File.ReadAllText()来读取文件,而.Split方法会将字符串转换为数组。



数组有排序方法。





如果你可以在向阵列添加内容时重新写入文件,我相信file.writealltext可以用于输出。
You can use File.ReadAllText() to read teh file and the .Split method will turn the string into an array.

Arrays have a sort method.


If you can just re-write the file when something is added to the array, I believe the file.writealltext would work for the output.


肖恩,



放松它并不难。他们使VB.Net对VB6程序员非常友好。



有些人可能会说友好,以至于引起混乱,但讨论对你没有帮助。



您没有看到StreamReader类的原因是您没有导入必要的命名空间。 VB会在幕后自动为您导入几个,但您需要的是System.IO。 System.IO是为您加载的默认程序集之一,但它不会自动导入。



要查看为项目加载的程序集和名称空间,请转到在Project菜单中选择YourAssemblyName Properties;如果您在创建项目时未设置名称,则很可能是名称WindowsApplication1 Properties。您现在应该位于属性页面的应用程序选项卡上。选择引用选项卡以查看为项目加载的所有程序集。在该选项卡的底部,您将看到从这些程序集导入的默认名称空间。任何带有复选标记的内容都会自动提供。此时,您可以向下滚动到System.IO并检查一个,或者您的代码返回到您的代码并在代码顶部添加此语句。



导入System.IO



请记住,这只是为您提供了指定您想要的类的捷径使用。您也可以输入以下内容:



Dim sr As New System.IO.StreamReader



如果此时使用StreamReader对你来说有点压倒性,VB提供了一个帮助类,用于读取名为TextFieldParser [ ^ ]。在该示例中,您需要将分隔符设置为VbTab(请参阅熟悉的常量)而不是显示的逗号。



一旦读入阵列,就可以了可以将ComboBox的DataSource属性设置为等于所需的数组。在这个例子中,我使用了一个字符串数组列表作为中央存储设备。



Sean,

Relax it is not that hard. They made VB.Net very friendly to VB6 programmers.

Some might argue to friendly to the point that it causes confusion, but that discussion will not help you.

The reason you are not seeing the StreamReader class is that you have not imported the necessary namespace. VB imports several for you automatically behind the scenes, but the one that you need is System.IO. System.IO is in one of the default assemblies loaded for you, but it is not automatically imported.

To see which assemblies and namespaces are loaded for your project, go the Project Menu and select "YourAssemblyName Properties"; if you did not set a name when you created the project, this most likely will be name "WindowsApplication1 Properties". You should now be on the Application tab of the properties page. Select the References tab to see all the assemblies loaded for your project. On the bottom of that tab you will see the default namespaces imported from those assemblies. Anything with a check-mark is automatically available. At this point you could scroll down to System.IO and check that one, or you code go back to your code and add this statement at the top of the code.

Imports System.IO

Remember that this just provides you a short cut to specifying what class you want to use. You could just as well type this:

Dim sr As New System.IO.StreamReader

If using the StreamReader is a bit overwhelming for you at this point, VB provides a helper class for reading delimited files called TextFieldParser[^]. In that example, you would need the set the delimiter to VbTab (see a familiar constant) instead of the comma shown.

Once you have your arrays read in, you can set the ComboBox''s DataSource property equal to the needed array. In this example, I have used a list of string arrays as a central storage device.

Public Class Form1

    '' combosource could hold your string arrays
    Dim combosource As New List(Of String())

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        '' simulate reading your data
        '' adding string arrays to combosource
        combosource.Add({"1", "2"})
        combosource.Add({"4", "42"})

        '' Note the zero based indexing here
        ComboBox1.DataSource = combosource(0)
        ComboBox2.DataSource = combosource(1)
    End Sub
End Class


这篇关于读取制表符分隔文件到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆