473,657 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Efficient way of selecting an item in a Virtual ListView

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.Vir tualListSize = servers.Count;
if (server != null)
{
foreach(ListVie wItem lvi in listServers.Ite ms)
{
if (server == (Server)lvi.Tag )
{
lvi.Selected = true;
break;
}
}
}

Nov 17 '05 #1
10 11331
"Danny Tuppeny" <gr****@dannytu ppeny.commmmmm> wrote in message
news:43******** *************** @ptn-nntp-reader04.plus.n et...
<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
RetrieveVirtual Item method, and still no joy :-(
Nov 17 '05 #2
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Danny Tuppeny" <gr****@dannytu ppeny.commmmmm> wrote in message
news:43******** *************** @ptn-nntp-reader04.plus.n et...
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.Vir tualListSize = servers.Count;
if (server != null)
{
foreach(ListVie wItem lvi in listServers.Ite ms)
{
if (server == (Server)lvi.Tag )
{
lvi.Selected = true;
break;
}
}
}

Nov 17 '05 #3
Hi,

what is "virtual mode" ?

cheers,
"Danny Tuppeny" <gr****@dannytu ppeny.commmmmm> wrote in message
news:43******** **************@ ptn-nntp-reader01.plus.n et...
"Danny Tuppeny" <gr****@dannytu ppeny.commmmmm> wrote in message
news:43******** *************** @ptn-nntp-reader04.plus.n et...
<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
RetrieveVirtual Item method, and still no joy :-(

Nov 17 '05 #4
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:uW******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

what is "virtual mode" ?


Virtual Mode is for when you already have your items in an array. Rather
than create loads of ListViewItems, you do:

list.VirtualMod e = true;
list.VirtualLis tSize = 100;
list.RetrieveVi rtualItem += new
System.Windows. Forms.RetrieveV irtualItemEvent Handler(this.li stServers_Retri eveVirtualItem) ;

and define an event handler:

private void listServers_Ret rieveVirtualIte m(object sender,
RetrieveVirtual ItemEventArgs e)
{
NewsServer server = servers[e.ItemIndex];
e.Item = new ListViewItem(se rver.Username + " on " + server.Host
+ ":" + server.Port.ToS tring(), 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 :-((
Nov 17 '05 #5
He's using .NET Framework 2.0.

Jason

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

what is "virtual mode" ?

cheers,
"Danny Tuppeny" <gr****@dannytu ppeny.commmmmm> wrote in message
news:43******** **************@ ptn-nntp-reader01.plus.n et...
"Danny Tuppeny" <gr****@dannytu ppeny.commmmmm> wrote in message
news:43****** *************** **@ptn-nntp-reader04.plus.n et...
<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
RetrieveVirtu alItem method, and still no joy :-(


Nov 17 '05 #6
"Jason Newell" <no****@nospam. com> wrote in message
news:%2******** ********@TK2MSF TNGP14.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(
Nov 17 '05 #7
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 RetriveVirtualI tem 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=tru e 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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Danny Tuppeny" <gr****@dannytu ppeny.commmmmm> wrote in message
news:43******** *************** @ptn-nntp-reader03.plus.n et...
"Jason Newell" <no****@nospam. com> wrote in message
news:%2******** ********@TK2MSF TNGP14.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(

Nov 17 '05 #8
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:%2******** *******@tk2msft ngp13.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 RetriveVirtualI tem 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

Also it's stated in the doc that an exception is throw IF you set
VirtualMode=tru e 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.Select edIndices[0]].Selected = true;

or in full, my code is:

listServers.Vir tualListSize = servers.Count;
if (selectedServer != null)
{
for(int i = 0; i < servers.Count; i++)
{
if (selectedServer == servers[i])
{
listServers.Ite ms[i].Selected = true;
break;
}
}
}

And it works :-))))
Nov 17 '05 #9
Hi,
"Danny Tuppeny" <gr****@dannytu ppeny.commmmmm> wrote in message
news:43******** *************** @ptn-nntp-reader03.plus.n et...
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
wrote in message news:%2******** *******@tk2msft ngp13.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 RetriveVirtualI tem 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


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.

Also it's stated in the doc that an exception is throw IF you set
VirtualMode=tru e 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.Select edIndices[0]].Selected = true;

or in full, my code is:

listServers.Vir tualListSize = servers.Count;
if (selectedServer != null)
{
for(int i = 0; i < servers.Count; i++)
{
if (selectedServer == servers[i])
{
listServers.Ite ms[i].Selected = true;
break;
}
}
}

And it works :-))))


Good to know :)

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

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1629
by: Glenn | last post by:
I'm creating a virtual ListView with C#. I've seen a few code posts stating that a virtual ListView will not work if the View is LargeIcon or SmallIcon. Is this true? My project requires the LargeIcon view style, and so far I've been unable to get the images to display. The text (captions) don't seem to be a problem. My only guess is that the ImageList isn't "really" being set to the ListView, although it does add space for the images....
1
3398
by: Bob Geltz | last post by:
I am able to populate a ListView with several detail lines (several columns). When finished, I would like to pre-select the first item in the list (before the user interacts with the list). This way, if a user clicks on the OK button, there will be a default selection. I know how to use ListView.SelectedItems to get the selection, I just don't know how to make an item be selected without the user actually clicking on a line. For a...
2
6300
by: Coffee guy | last post by:
I need to locate an item in ListView, quickly. I have a listview with items, for each I added a unique "Tag" property. Is there a simple way to rapidly search through these items to find based on its Tag value? Tia, Stu
0
1315
by: Arnold the Aardvark | last post by:
I'm reimplimenting an old project in order to learn about the WindowsForms classes. My project (C++Builder) used a virtual listview. The data was in a structure made out of STL containers. So the question is: can I easily use a virtual listview. At this stage I am not interested in learning about anything too 'advanced'. Implementing this in BCB was simplicity itself - set one property and write one method - so I naturally thought the...
2
12896
by: yxq | last post by:
Hello I want to first item of listview selected automatically, i write the code, but it does not work. ListView1.Focus() ListView1.Item(0).Select = True Thanks
2
7044
by: yxq | last post by:
Hello, I want to hide an item in listview but not remove it, how to do? Thank you
1
5380
by: Carl Johansson | last post by:
I have a virtual ListView (VirtualMode set to true). To make it more convenient for the users of the ListView to select its items, I've set the ListView's CheckBoxes property to true. However, in virtual mode no checkboxes appear. That is, when the View style is set to Details the text labels are indented to make room for checkboxes, but no checkboxes are displayed. Am I missing something? I think this ought to be as straight forward as...
0
931
by: Lloyd Sheen | last post by:
I was in a thread talking about how to populate a listview from a Linq query. This got my curiosity up so I created a usercontrol which is a listview which can be populated from a Linq query. I has all the same properties / methods available to a listview but behind the scenes it is really a virtual listview. It is quick to load (33K items in less than a second including the query). It can be sorted and uses the dynamic linq vb...
1
1261
by: George21 | last post by:
Hi. I'm trying to delete selected item in listview. Can anyone help me? How can i delete selected item in Listview from menu using for loop?
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.