c# - Displaying multiple properties from a List<T> in a horizontal fashion in a Listbox -
i'm reasonably new c# (apprentice, 5 months in, 3 weeks training) , 1 of assignments create event driven computer program using c# in form of shopping basket.
i'm nearing end of assignment , i'm designing shoppingbasketform
. have class orderitem
, contains properties productname
, quantity
etc. have class shoppingbasket
, contains property of list<orderitem>orderitems
.
how go making lstboxbasket
on form display list<orderitem>orderitems
properties horizontally in shopping basket fashion?
thanks in advance.
eg of ideal display, ignore code block, easiest way show it:
oranges 5 £1.20 apples 3 £0.80
oranges being productname
, 5 being quantity
, £1.20 being latestprice
.
as others have mentioned, if using datagridview
or listview
allowed, simple task.
but since must use listbox
, set drawmode
property ownerdrawnfixed
, , handle listbox.drawitem
event, this:
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] 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); }
it takes tweaking , have decide whether scale or clip horizontal overflow, have complete freedom on how items rendered.
Comments
Post a Comment