从列表框vb中读取单个单词

时间:2013-03-10 04:19:46

标签: vb.net

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

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

2 个答案:

答案 0 :(得分:0)

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

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

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

代码看起来像:

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

以这种格式,

textBoxName.Text = myArray[1]

答案 1 :(得分: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