vb.net - 从文本文件更改信息在列表框中的显示方式?
问题描述
有没有办法改变文本文件中信息在列表框中的显示方式?我想将文本文件中的名称显示为名字,然后是姓氏,并且仍然按姓氏的字母顺序排列。现在它在列表框中显示为姓氏,名字。文本文件中的名称格式为“Langley Judy,321-1111”
Public Class frmMembership
' Read all lines from text file
Dim file() As String = IO.File.ReadAllLines("MemberPhones.txt")
Private Sub ModifyToolStripMenuItem_Click(sender As Object, e As EventArgs)
End Sub
Private Sub frmMembership_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Display()
End Sub
' Displays users from text file and sorts by last name
Public Sub Display()
lstDisplay.Items.Clear()
For Each n In file
lstDisplay.Items.Add(n.ToString.Split(",")(0))
Next
lstDisplay.Sorted = True
txtName.Text = ""
txtPhone.Text = ""
End Sub
' Closes program and writes new lines to text file
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
IO.File.WriteAllLines("MemberPhones.txt", file)
End Sub
' Query to display users phone number and name when selected in output textboxes
Private Sub lstDisplay_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstDisplay.SelectedIndexChanged
Dim query = From p In file
Let name As String = p.ToString.Split(",")(0)
Let phone = p.ToString.Split(",")(1)
Where name = lstDisplay.SelectedItem
Select name, phone
txtName.Text = query.ToList.First.name.ToString
txtPhone.Text = query.ToList.First.phone.ToString
End Sub
Private Sub ModifyToolStripMenuItem_Click_1(sender As Object, e As EventArgs) Handles ModifyToolStripMenuItem.Click
If lstDisplay.Text <> "" Then
' Matches array element with name from text file
For i As Integer = 0 To file.Count - 1
If (file(i).Split(",")(0)) = lstDisplay.Text Then
file(i) = txtName.Text + "," + txtPhone.Text
End If
Next
Display()
Else
MsgBox("Please add information")
End If
End Sub
Private Sub AddToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AddToolStripMenuItem.Click
If (txtName.Text <> "") And (txtPhone.Text <> "") Then
ReDim Preserve file(file.Count) 'Adds one more index to the array
file(file.Count - 1) = txtName.Text + "," + txtPhone.Text
Display()
Else
MsgBox("Please add information")
End If
End Sub
Private Sub DeleteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteToolStripMenuItem.Click
Dim deleteName As String = lstDisplay.SelectedItem
'Deleting data and now it will hold one less
Dim tempName(lstDisplay.Items.Count - 2) As String
If deleteName <> "" Then
'User will select name to delete here
Dim query = From n In file
Let name As String = n.Split(",")(0)
Where name <> deleteName
Select n
'New data will be written into the text file
ReDim file(tempName.Count - 1)
file = query.ToArray
Display()
Else
MsgBox("Nothing data to delete")
End If
End Sub
End Class
解决方案
我的文本文件如下所示(FirstName
并LastName
用逗号分隔):
Mary,Smith,555-1212
John,Jones,333-1212
George,Washington,713-2212
Peter,Pan,313-3435
该类将如下所示:
Public Class MemberPhone
Public Property FirstName As String
Public Property LastName As String
Public Property Phone As String
'Allows you to create a new MemberPhone without having to set properties individually
Public Sub New(fName As String, lName As String, Number As String)
FirstName = fName
LastName = lName
Phone = Number
End Sub
'This is called by the ListBox for what to display
Public Overrides Function ToString() As String
Return $"{FirstName} {LastName} - {Phone}"
End Function
End Class
并在表格中:
Private PhoneList As New List(Of MemberPhone)
Private Sub FillPhoneList()
Dim lines = File.ReadAllLines("MemberPhones.txt")
For Each line In lines
Dim splits = line.Split(","c)
PhoneList.Add(New MemberPhone(splits(0), splits(1), splits(2)))
Next
'This orders the PhoneList by LastName
PhoneList = (From mp In PhoneList
Order By mp.LastName).ToList
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillPhoneList()
'Binds the ListBox to the ordered PhoneList
ListBox1.DataSource = PhoneList
End Sub
整个对象被插入到列表框中的每个项目中。该对象的所有属性都可用。
Private Sub lstDisplay_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
txtName.Text = DirectCast(ListBox1.SelectedItem, MemberPhone).FirstName
txtPhone.Text = DirectCast(ListBox1.SelectedItem, MemberPhone).Phone
End Sub
我更改了一些控件名称以匹配我的测试项目,所以我不会有这么多的红色曲线。
推荐阅读
- android - “ImageView”中的“center”和“centerCrop”有什么区别?
- java - Maven依赖管理中的“群压”
- python - Numba `nogil` + dask 线程后端导致没有加速(计算速度较慢!)
- laravel - 可以在 laravel 项目中集成 2 个项目 vuejs
- r - 在R中的公式表达式中添加控制变量字符串
- html - 使特定元素只能由屏幕阅读器阅读
- scala - ClassNotFoundException 火花提交纱线
- linux - 如何在grafana中显示实例ID?
- node.js - NodeJS 的复杂种子生成器
- c - 为什么这个常量表达式不是常量