|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Efficient way of selecting an item in a Virtual ListViewI'm trying to find the best way to select an item in a ListView using the Virtual mode. My "items" are business classes, and I have a reference to once I want to select (passed from another dialog). One way is to spin through the items and compare the .Tag property to my business object (code below), however I fear the casts are probably costly (there could be a lot of servers). I also thought about spinning through the servers list and comparing directly, then using the index to set the selected item, however to use foreach(), I don't have the index number, and I'm not sure of the performance of a for(int i...) loop in comparison to a foreach. My code currently works, but I'd like to make sure this is as fast as possible (it'll be in many places, with very big lists soon!) List<Server> servers = Server.GetAll(); listServers.VirtualListSize = servers.Count; if (server != null) { foreach(ListViewItem lvi in listServers.Items) { if (server == (Server)lvi.Tag) { lvi.Selected = true; break; } } } "Danny Tuppeny" <groups@dannytuppeny.commmmmm> wrote in message <snip>news:4342ca70$0$49766$ed2e19e4@ptn-nntp-reader04.plus.net... I lied - my code doesn't currently works. listView.Items is empty in virtual mode... So in addition to the mots efficient way, does anyone actually have a way that works? I've tried allsorts, including setting .Selected to true in the RetrieveVirtualItem method, and still no joy :-( Hi,
what is "virtual mode" ? cheers, Show quoteHide quote "Danny Tuppeny" <groups@dannytuppeny.commmmmm> wrote in message news:4342d990$0$7052$ed2619ec@ptn-nntp-reader01.plus.net... > "Danny Tuppeny" <groups@dannytuppeny.commmmmm> wrote in message > news:4342ca70$0$49766$ed2e19e4@ptn-nntp-reader04.plus.net... > <snip> > > I lied - my code doesn't currently works. listView.Items is empty in > virtual mode... So in addition to the mots efficient way, does anyone > actually have a way that works? > > I've tried allsorts, including setting .Selected to true in the > RetrieveVirtualItem method, and still no joy :-( > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote Virtual Mode is for when you already have your items in an array. Rather in message news:uWOy5zRyFHA.3864@TK2MSFTNGP12.phx.gbl... > Hi, > > what is "virtual mode" ? than create loads of ListViewItems, you do: list.VirtualMode = true; list.VirtualListSize = 100; list.RetrieveVirtualItem += new System.Windows.Forms.RetrieveVirtualItemEventHandler(this.listServers_RetrieveVirtualItem); and define an event handler: private void listServers_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) { NewsServer server = servers[e.ItemIndex]; e.Item = new ListViewItem(server.Username + " on " + server.Host + ":" + server.Port.ToString(), 0); } This way, only items that are seen in the list are created. However, it seems to break SelectItems et al, so I can't find a way to programatically select something when using virtual mode :-(( He's using .NET Framework 2.0.
Jason Ignacio Machin ( .NET/ C# MVP ) wrote: Show quoteHide quote > Hi, > > what is "virtual mode" ? > > cheers, > > > "Danny Tuppeny" <groups@dannytuppeny.commmmmm> wrote in message > news:4342d990$0$7052$ed2619ec@ptn-nntp-reader01.plus.net... > >>"Danny Tuppeny" <groups@dannytuppeny.commmmmm> wrote in message >>news:4342ca70$0$49766$ed2e19e4@ptn-nntp-reader04.plus.net... >><snip> >> >>I lied - my code doesn't currently works. listView.Items is empty in >>virtual mode... So in addition to the mots efficient way, does anyone >>actually have a way that works? >> >>I've tried allsorts, including setting .Selected to true in the >>RetrieveVirtualItem method, and still no joy :-( >> > > > "Jason Newell" <nospam@nospam.com> wrote in message Trying!news:%23xu2jzbyFHA.2312@TK2MSFTNGP14.phx.gbl... > He's using .NET Framework 2.0. Yeah, sorry, I forgot this is just a c# group! :) Still, I've not come across a single way to programatically select an item in a ListBox using VirtualMode... Is this not a fundamental requirement? :o( Hi,
Well, I'm still using 1.1 at the office so I haven't play with something as especific as this, I just checked the doc ( pretty scarse btw) and it seems that you have to handle RetriveVirtualItem event, which apparently expect a ListViewItem, so at the end you do create ListViewItems :) Also it's stated in the doc that an exception is throw IF you set VirtualMode=true AND Items has elements. so what you are seeing is consistent with the doc. My advice is to simply use the ListView as usual :) cheers, -- Show quoteHide quoteIgnacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation "Danny Tuppeny" <groups@dannytuppeny.commmmmm> wrote in message news:43440fcb$0$73620$ed2619ec@ptn-nntp-reader03.plus.net... > "Jason Newell" <nospam@nospam.com> wrote in message > news:%23xu2jzbyFHA.2312@TK2MSFTNGP14.phx.gbl... >> He's using .NET Framework 2.0. > > Trying! > > Yeah, sorry, I forgot this is just a c# group! :) > > Still, I've not come across a single way to programatically select an item > in a ListBox using VirtualMode... Is this not a fundamental requirement? > :o( > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote Yep, but you don't have to create 50,000 of them. You create the 6 or so the in message news:%23ErAzVeyFHA.156@tk2msftngp13.phx.gbl... > Well, I'm still using 1.1 at the office so I haven't play with something > as especific as this, I just checked the doc ( pretty scarse btw) and it > seems that you have to handle RetriveVirtualItem event, which apparently > expect a ListViewItem, so at the end you do create ListViewItems :) user can see, and as they scroll you add more individually - performance on a *massive* list is fantastic :D > Also it's stated in the doc that an exception is throw IF you set Not quite, I was trying to read Items after setting virtual items. It turns > VirtualMode=true AND Items has elements. so what you are seeing is > consistent with the doc. out, there was a bug in my original code, and you do actually get at times via .Items (to read, not to set), like this: listView1.Items[listView.SelectedIndices[0]].Selected = true; or in full, my code is: listServers.VirtualListSize = servers.Count; if (selectedServer != null) { for(int i = 0; i < servers.Count; i++) { if (selectedServer == servers[i]) { listServers.Items[i].Selected = true; break; } } } And it works :-)))) Hi,
"Danny Tuppeny" <groups@dannytuppeny.commmmmm> wrote in message Whoaa, I have never had such a huge amount of data in a listview, you are news:434430b4$0$73590$ed2619ec@ptn-nntp-reader03.plus.net... > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> > wrote in message news:%23ErAzVeyFHA.156@tk2msftngp13.phx.gbl... >> Well, I'm still using 1.1 at the office so I haven't play with something >> as especific as this, I just checked the doc ( pretty scarse btw) and it >> seems that you have to handle RetriveVirtualItem event, which apparently >> expect a ListViewItem, so at the end you do create ListViewItems :) > > Yep, but you don't have to create 50,000 of them. You create the 6 or so > the user can see, and as they scroll you add more individually - > performance on a *massive* list is fantastic :D right using the "regular" way is out of the question. Show quoteHide quote > Good to know :)>> Also it's stated in the doc that an exception is throw IF you set >> VirtualMode=true AND Items has elements. so what you are seeing is >> consistent with the doc. > > Not quite, I was trying to read Items after setting virtual items. It > turns out, there was a bug in my original code, and you do actually get at > times via .Items (to read, not to set), like this: > > listView1.Items[listView.SelectedIndices[0]].Selected = true; > > or in full, my code is: > > listServers.VirtualListSize = servers.Count; > if (selectedServer != null) > { > for(int i = 0; i < servers.Count; i++) > { > if (selectedServer == servers[i]) > { > listServers.Items[i].Selected = true; > break; > } > } > } > > And it works :-)))) > I was checking yesterday a Owned draw list control and it also may have solved your problem, take a look at it in www.opennetcf.org the article is interesting, the good thing about this approach is that you could do it without creating any listviewitem . cheers, -- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote Nor me, it was an example! :)in message news:uUj53gnyFHA.1252@TK2MSFTNGP09.phx.gbl... >> Yep, but you don't have to create 50,000 of them. You create the 6 or so >> the user can see, and as they scroll you add more individually - >> performance on a *massive* list is fantastic :D > > Whoaa, I have never had such a huge amount of data in a listview, you are > right using the "regular" way is out of the question. I like it because it means you don't have to create ListViewItem's that'd never be used, and it's scalable should you need it :) You think you only need 6 items, but wait until your user gets hold of your app, and finds a way to abuse some "new" functionality into it! ;o))) > I was checking yesterday a Owned draw list control and it also may have I don't think creating a few ListViewItems is a problem, and I'm a fan of > solved your problem, take a look at it in www.opennetcf.org the article is > interesting, the good thing about this approach is that you could do it > without creating any listviewitem . using the built in controls where I can - shipped apps will be smaller, and I'm liking the look of ClickOnce :) Hi,
Is the server variable a reference to an instance you know is in the list? note I'm not refering to two object with EXACT the same values, but the same reference. If so, you do not need to cast , it will compare the reference and it will be as fast as you can get :) cheers, -- Show quoteHide quoteIgnacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation "Danny Tuppeny" <groups@dannytuppeny.commmmmm> wrote in message news:4342ca70$0$49766$ed2e19e4@ptn-nntp-reader04.plus.net... > Hi All, > > I'm trying to find the best way to select an item in a ListView using the > Virtual mode. My "items" are business classes, and I have a reference to > once I want to select (passed from another dialog). One way is to spin > through the items and compare the .Tag property to my business object > (code below), however I fear the casts are probably costly (there could be > a lot of servers). I also thought about spinning through the servers list > and comparing directly, then using the index to set the selected item, > however to use foreach(), I don't have the index number, and I'm not sure > of the performance of a for(int i...) loop in comparison to a foreach. My > code currently works, but I'd like to make sure this is as fast as > possible (it'll be in many places, with very big lists soon!) > > List<Server> servers = Server.GetAll(); > listServers.VirtualListSize = servers.Count; > if (server != null) > { > foreach(ListViewItem lvi in listServers.Items) > { > if (server == (Server)lvi.Tag) > { > lvi.Selected = true; > break; > } > } > } > > >
Memory address of an object
How do I check for complete file enum defined in a linked .cs file Type Comparison DataSource & DataMember property at custom control. AsyncCallback Explain why my code is not working Secondary Thread start Method in Main Thread ListBox.Items.Contains Serializable object containg MarshalByRefObject Object Controls.Count |
|||||||||||||||||||||||