0

我有一个在 VB.NET 中创建的订单表格,我有一个按订单填充的 ListBox。您可以双击订单,它会在订单表格中填充订单号。我遇到的问题是它用订单号和人名填充文本框。如何使用分隔符仅提取订单号而不提取名称。

Imports Business_Objects

Public Class frmSummary
  Private ctrl As Controller
  Dim listID As ArrayList

  Private Sub frmSummary_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ctrl = CType(MdiParent, frmMain).ctrl
    Dim list As ArrayList
    list = ctrl.GetOrders
    Dim order As Business_Objects.Order
    For Each order In list
      lstOrders.Items.Add(order.ID & "," & " " & order.Server)
    Next
  End Sub

  Private Sub lstOrders_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstOrders.DoubleClick
    Dim result As Boolean = False
    If lstOrders.Text <> "" Then
      result = True
      Dim frm As New OrderForm
      frm.MdiParent = Me.MdiParent
      frm.Show()
      frm.txtOrderNo.Text = lstOrders.Text
      frm.btnFetch.PerformClick()
    Else
      MessageBox.Show("there are no orders here to click")
    End If
  End Sub

  Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
    lstOrders.Items.Clear()
    ctrl = CType(MdiParent, frmMain).ctrl
    Dim list As ArrayList
    list = ctrl.GetOrders
    Dim order As Business_Objects.Order
    For Each order In list
      lstOrders.Items.Add(order.ID & " " & order.Server)
    Next
  End Sub
End Class
4

2 回答 2

0

如果您的所有数据都存储为单个字段,或者类似:

4322305 John Smith Carrots $3.00
845825 Sam White Oranges $1.25

然后您可以将每条记录读取为字符串,然后将其拆分为基于“”作为分隔符的数组。

代码看起来像:

dim myArray as string() = myLongTextRecord.Split(" ")

以这种格式,

textBoxName.Text = myArray[1]
于 2013-03-10T04:50:01.877 回答
0

您快到了。您可以使用 split 函数,但另一种方法是将 Order 对象直接添加到列表框而不是文本。

Private Sub frmSummary_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ctrl = CType(MdiParent, frmMain).ctrl
    Dim list As ArrayList
    list = ctrl.GetOrders
    Dim order As Business_Objects.Order
    For Each order In list
      lstOrders.Items.Add(order)
    Next
  End Sub

  Private Sub lstOrders_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstOrders.DoubleClick
    Dim result As Boolean = False
    If lstOrders.Text <> "" Then
      result = True
      Dim frm As New OrderForm
      frm.MdiParent = Me.MdiParent
      frm.Show()
      frm.txtOrderNo.Text = DirectCast(lstOrders.SelectedItem, Order).ID.ToString
      frm.btnFetch.PerformClick()
    Else
      MessageBox.Show("there are no orders here to click")
    End If
  End Sub

您需要进入 Order 对象并覆盖 .ToString 函数,以便列表框中的文本显示您想要的任何值(即。Return ID & "," & " " & Server

于 2013-03-10T13:29:11.747 回答