vb.net读取txt文件,按照要求排序其中数据后显示在listbox控件中。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net读取txt文件,按照要求排序其中数据后显示在listbox控件中。相关的知识,希望对你有一定的参考价值。

TXT文件内容:
A
B
C
D
E
F
G
H
转换成:
A,C
B,D
E,G
F,H
后显示在listbox控件中。最好能写出主要代码。
嗯,我已经实现了部分功能,比如读取TXT文件到listbox中
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim open As New OpenFileDialog
open.Filter = "TXT|*.txt"
open.FileName = ""
open.ShowDialog()
If open.FileName = "" Then Exit Sub
ListBox1.Items.Clear()
Dim File_Name As String = open.FileName
Dim Lines() As String = File.ReadAllLines(File_Name)
Dim t As String
Timer1.Enabled = True
Timer1.Interval = 1000
up = Lines.Length - 1
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = Int(up)
For i As Integer = 0 To up
ProgressBar1.Value = i
t = Lines(i)
ListBox1.Items.Add(t)
Next
Timer1.Enabled = False
End Sub
但是如何按要求排序,还少一些思路。

说一个可行的方案。你可以用3个ListBox,当然其中两个个可以设为不可见。先读两行加入到ListBox1中,接着读两行写入ListBox2中,如此循环到文件结束。按照你给的例子,现在Listbox
1中的items是A,B,E,F;而ListBox2中的Items是C,D,G,H。接下来的你应该就明白了吧,把ListBox2中的Items和对应的ListBox1中Item加到ListBox3中。下面简单写下代码,主要是处理问题的思路,代码很简单。

Private Sub DoList(ByVal filePath As String)

Dim str As String
Dim num
Dim listCount
Dim sr As StreamReader

sr = New System.IO.StreamReader(filePath, System.Text.Encoding.Default)
str = sr.ReadLine()
Do While (sr.Peek >= 0)
For num=0 To 2
str = sr.ReadLine()
ListBox1.items.Add(str)
Next
num=0
For num=0 To 2
str = sr.ReadLine()
ListBox2.items.Add(str)
Next
Loop
sr.Close()
For listCount=o To ListBox2.Items.Count-1
ListBox3..Items.Add(ListBox1.Items(listCount).ToString+
参考技术A 这个代码很简单啦 自己动手 不然还是不能自己动手。去csdn上查询点资料吧 参考技术B so easy,最通用的冒泡排序法,就可以啊,用比较的大于号还是小于号控制正序还是倒序。你翻翻数据结构的课本,这么经典的问题。

以上是关于vb.net读取txt文件,按照要求排序其中数据后显示在listbox控件中。的主要内容,如果未能解决你的问题,请参考以下文章

java读取多个txt文件内容,并按照文件名称排序

关于VB.NET的CSV文件的读取

vb.net 怎么生成csv文件与怎么读取csv文件

JAVA读取文件 排序 写出文件

vb.net读写txt文件的问题

Python实现txt文件读取数字,排序,求均值?