这是我的代码
Module Module1
Public Conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public sql As String
Public Function Connection()
Conn = New ADODB.Connection
Conn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & My.Application.Info.DirectoryPath & "\Database1.mdb"
Conn.Open()
End Function
将我的数据加载到ListView
Private Sub ApplicationDesignForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Connection()
sql = "Select * From App"
rs.Open(sql, Conn)
While Not rs.EOF
Dim item As ListViewItem = ListView1.Items.Add(rs.Fields("QRCode").Value)
item.SubItems.Add(rs.Fields("Category").Value)
item.SubItems.Add(rs.Fields("LastName").Value & ", " & rs.Fields("FirstName").Value & ", " & rs.Fields("MI").Value)
item.SubItems.Add(rs.Fields("Gender").Value)
item.SubItems.Add(rs.Fields("Address").Value)
ListView1.FullRowSelect = True
rs.MoveNext()
End While
Conn.Close()
将我的数据传输到文本框。
If Not ListView1.SelectedItems.Count = 0 Then
txtqrCode.Text = ListView1.Items(ListView1.SelectedIndices(0)).Text
cbCategory.Text = ListView1.Items(ListView1.SelectedIndices(0)).SubItems(1).Text
txtFirstName.Text = ListView1.Items(ListView1.SelectedIndices(0)).SubItems(2).Text
cbGender.Text = ListView1.Items(ListView1.SelectedIndices(0)).SubItems(3).Text
txtAddress.Text = ListView1.Items(ListView1.SelectedIndices(0)).SubItems(4).Text
但问题是全名只在名字文本框中,而不是由LastName,FirstName和Middle Initial文本框分隔。我被困住了,想要分开LastName,FirstName和MiddeInitial。非常感谢任何建议或意见。 Here is the result
答案 0 :(得分:0)
首先,我检查是否存在首字母,如果没有,则跳过额外的逗号
If rs.Fields("MI").Value & "" <> "" then
item.SubItems.Add(rs.Fields("LastName").Value & ", " & rs.Fields("FirstName").Value & ", " & rs.Fields("MI").Value)
else
item.SubItems.Add(rs.Fields("LastName").Value & ", " & rs.Fields("FirstName").Value)
end if
然后你可以适当地分解全名
' I'd use an intermediate variable here, just so you can see what is happening while debugging.
Dim FullName as string = ListView1.Items(ListView1.SelectedIndices(0)).SubItems(2).Text
Dim NMA() As String = Split(FullName, ", ", 3)
Redim Preserve NMA(2) '<--- make sure we have three parts
txtLastName.text = NMA(0)
txtFirstName.text = NMA(1)
txtInitials.text = NMA(2)
注意:拆分功能中的3将分割限制为三个项目。这似乎并不重要,但是,没有保证一些用户不会添加&#34;,&#34;名称字段中的某处...例如&#34; Charles,The 3rd&#34;在第一个名称..使用长度限制器确保在第二个&#34;,&#34;之后留下的任何东西。被加入到首字母字段中。
我也可以通过在那里使用 With block 来提高效率....
With ListView1.Items(ListView1.SelectedIndices(0))
....
Dim FullName as string = .SubItems(2).Text
....
End With
HOWEVER:分解字符串存在问题...姓氏可能有&#34;,&#34;包含在其中。因此,使用此方法,您的文本框可能不包含正确的数据库值。
因此,您最好从其他地方获取值。直接从数据库中再次,或者更好,在读取数据库时将它们存储到其他列表类型结构中。
像...一样的东西。
Private QR_Records As New List(Of QR_REcord)
Private Structure QR_Record
Public Code As String
Public Category As String
Public Name_Last As String
Public Name_First As String
Public Name_Initials As String
'etc
Public ReadOnly Property Name_Full() As String
Get
If Name_Initials.Length > 0 Then
Return (Name_Last & ", " & Name_First & ", " & Name_Initials)
Else
Return (Name_Last & ", " & Name_First)
End If
End Get
End Property
End Structure
然后从数据库读取器函数构建它并直接从所选列表索引引用它。
With QR_Records(ListView1.SelectedIndices(0))
....
Dim FullName as string = .Name_Full
....
End With
BONUS :使用此方法的另一个好处是,您可以在 VIRTUAL MODE 中使用ListView,这样可以提供更好的性能大量的记录。
由于这个原因,我实际上会将ListViewItem作为QR_Record的属性,如下所示
Public ReadOnly Property List_Item() as ListViewItem
Get
Dim Item As new ListViewItem
Item.Text = Code
item.SubItems.Add(Category)
item.SubItems.Add(Name_Full)
....
Return Item
End Get
End Property
说完了所有这些:正如其他人所指出的那样......这不是解决问题的正确方法。