c# - Can I access properties of a ListViewItem.Tag rather than just the "ToString" method? -
be patient, not sure how best ask 1 ...
- i have listview control on windows form , add items names of text files in directory.
- each of files loaded object "resultfile" , assigned many properties based on text within - "string type", "int numberoflines", "bool isgeneric" etc.
- a user can select/deselect freely make decisions based on text file type have selected already. rough idea, if have selected 1 "type=="x1" don't want them able select one. if select 2 or more isgeneric==true, want give them warning ...
i started extending listviewitem this
public class mylistviewitem : listviewitem { public string type { get; set; } public int numberoflines { get; set; } public bool isgeneric { get; set; } public mylistviewitem(string s) : base(s) { } }
i happy first time i've extended control (yes, i'm new this) ... allows me add own types of "listviewitems" , can access properties make decisions found "tag" property , thought tie object , access items directly. looked @ msdn , said can attach "any" object don't know how use it.
i don't seem able except access default object methods.
mylistview.item[1].tag.tostring();
seems can ...
am missing listviewitem.tag property??
the tag
property takes object of type object
, class inherited object can stored within it. if had object this:
public class myitem { public string type { get; set; } public int numberoflines { get; set; } public bool isgeneric { get; set; } }
you can assign object tag field, so:
var myobj = new myitem(); myobj.type = "type 1"; mylistview.items[1].tag = myobj;
and retrieve object so:
var myobj = (myitem)mylistview.items[1].tag; var type = myobj.type;
only after cast myitem
can access custom properties. otherwise you'll object
properties , methods, 1 of .tostring()
.
Comments
Post a Comment