Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示列表中的多个属性<;T>;在列表框中以水平方式_C#_Winforms - Fatal编程技术网

C# 显示列表中的多个属性<;T>;在列表框中以水平方式

C# 显示列表中的多个属性<;T>;在列表框中以水平方式,c#,winforms,C#,Winforms,我对C#(学徒,5个月,3周的培训)相当陌生,我的任务之一是用C#以购物篮的形式创建一个事件驱动的计算机程序 我希望作业即将结束,我正在设计ShoppingBasketForm。我有一个类OrderItem,它包含ProductName和Quantity等属性。我还有一个类ShoppingBasket,它包含ListOrderItems属性 如何让表单上的lstBoxBasket以购物篮的方式水平显示ListOrderItems属性 提前谢谢 例如,理想的显示方式,忽略代码块,是最简单的显示方式

我对C#(学徒,5个月,3周的培训)相当陌生,我的任务之一是用C#以购物篮的形式创建一个事件驱动的计算机程序

我希望作业即将结束,我正在设计
ShoppingBasketForm
。我有一个类
OrderItem
,它包含
ProductName
Quantity
等属性。我还有一个类
ShoppingBasket
,它包含
ListOrderItems
属性

如何让表单上的
lstBoxBasket
以购物篮的方式水平显示
ListOrderItems
属性

提前谢谢

例如,理想的显示方式,忽略代码块,是最简单的显示方式:

Oranges    5     £1.20
Apples     3     £0.80

橙子是
ProductName
,5个是
数量
,1.20英镑是
最新价格

,正如其他人所提到的,如果允许使用
DataGridView
ListView
,这将是一项简单的任务

但是,由于必须使用
列表框
,因此可以将
DrawMode
属性设置为
OwnerDrawnFixed
,并处理
ListBox.DrawItem
事件,如下所示:

myListBox.DrawItem += new DrawItemEventHandler(this.DrawItemHandler);
myListBox.DrawMode = DrawMode.OwnerDrawnFixed;

private void DrawItemHandler(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    e.DrawFocusRectangle();

    OrderItem item = myListBox.Items[e.Index] as OrderItem;
    if (item == null) return;

    Rectangle nameRect = new Rectangle(e.Bounds.Location, new Size(e.Bounds.Width / 3, e.Bounds.Height));
    e.Graphics.DrawString(item.ProductName, Font, Brushes.Black, nameRect);

    Rectangle quantityRect = new Rectangle(...);
    e.Graphics.DrawString(item.Quantity.ToString(), Font, Brushes.Black, quantityRect);
}

这需要一些调整,您将不得不决定是缩放还是剪裁水平溢出,但您完全可以自由选择项目的渲染方式。

您尝试了什么?如果您什么也没试过,您应该尝试一下,然后返回出现的问题。请参阅:@EBrown我已尝试设置
DisplayMember
ValueMember
,但显然这不会以水平方式显示它,我不知道如何为列表框创建多个列,因此我尝试使用DataGrid,这获得了我想要的格式;但是规范要求我使用列表框,所以我不得不返回。我建议您为我们发布帖子,以进一步帮助您。@EBrown没有一个示例可以发布,我是问我将如何进行,这是一个问题,不是一个问题