0

我有这个包含列表框的表单

    Public Class Item
    Private sList As List(Of Container)

    Sub New(ByRef sList As List(Of Container))
        InitializeComponent()

        Me.sList = sList
    End Sub

    Private Sub Item_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        For Each c As Container In sList
            ListBox1.Items.Add(c)
        Next


        ListBox1.DataSource = sList
        ListBox1.DisplayMember = "sItem"
    End Sub
End Class

这将是类容器

Public Class Container

    Private _sItemName As String
    Private _sPrice As Single
    Private _iNumber As Integer
    Private _sItemString As String

    Sub New(ByVal _sItemName As String, ByVal _sPrice As Single, ByVal _iNumber As Integer, ByVal _sItemString As String)
        Me._sItem = _sItemName
        Me._sPrice = _sPrice
        Me._iNumber = _iNumber
        Me._sItemString = _sItemString
    End Sub

    Public Property sItemName() As String
        Get
            Return _sItemName
        End Get
        Private Set(value As String)
            _sItemName = value
        End Set
    End Property

    Public Property sItemString() As String
        Get
            Return _sItemString
        End Get
        Private Set(value As String)
            _sItemString = value
        End Set
    End Property

    Public Property sPrice() As Single
        Get
            Return _sPrice
        End Get
        Private Set(value As Single)
            _sPrice = value
        End Set
    End Property

    Public Property iNumber() As String
        Get
            Return _iNumber
        End Get
        Private Set(value As String)
            _iNumber = value
        End Set
    End Property

End Class

这就是我将它添加到输出的地方

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dgv As DataGridView = DataGridView1
    Dim rowIndex As Integer = dgv.SelectedRows(0).Index

    Dim _sItem As String = ""
    Dim _iNumber As Integer = TextBox1.Text
    Dim _sPrice As Single = dgv.Rows(rowIndex).Cells("Prix").Value * TextBox1.Text

    Dim format As String = "{0, -10} {1, -10} {2, 5}"

    _sItem = String.Format(format, dgv.Rows(rowIndex).Cells("Nom").Value.ToString, _sPrice.ToString("C2"), _iNumber.ToString)

    Console.WriteLine(_sItem)
    Dim container As New Container(_sItem, _sPrice, _iNumber, dgv.CurrentRow.Cells("Nom").Value.ToString)

    If (updateDGV(container) = True) Then
        sList.Add(container)
    End If

    TextBox1.Clear()
    TextBox1.Focus()
    updateLabel()
End Sub

所以我的问题是为什么 lsitbox 上的输出是这样的 在此处输入图像描述

控制台输出是

在此处输入图像描述

我想要的是列表框输出与控制台相同,但格式似乎不起作用,我不知道我应该如何进行这项工作我可以使用一些帮助和解释来解释为什么列表框不显示文本适当地

为了确保我很清楚:有没有办法让列表框输出看起来与控制台输出相同

编辑:列表视图

你可以看到数据的东西是绑定的,所以如果我这样做

来自列表框的容器访问

        Dim obj As Object = ListBox1.SelectedItem

    If (TypeOf obj Is Container) Then
        Dim c As Container = CType(obj, Container)

        Console.WriteLine(c.iNumber)
    End If

当我选择一行时,它可以返回我的对象​​,这就是我不知道如何处理列表视图和添加(对于每个Items.add(c)我不知道列表视图的等价物)

4

2 回答 2

2

控制台中有等宽字体。但不是小部件中的一个。

于 2013-09-30T11:29:17.783 回答
1

As proposed via comment, here comes a multi-column sample code with ListView:

With ListView1
    .View = View.Details
    .Columns.Add("")
    .Columns.Add("")
    .Columns.Add("")
End With

Dim inputs As List(Of Container) = New List(Of Container)
'populate "inputs"...
For Each item As Container In inputs
    Dim curEntry As New ListViewItem(New String() {item.sItem.ToString(), item.sPrice.ToString(), item.iNumber.ToString()})
    ListView1.Items.Add(curEntry)
Next

You can treat each column independently (alignment, width, etc.) via ListView1.Column(index).

于 2013-09-30T11:48:05.887 回答