Programmatically Selecting An Item in a Listview

  • Thread starter Thread starter Jin Kim
  • Start date Start date
J

Jin Kim

I'm trying to select an item in a listview using C#
and .NET Compact Framework. Is there anyway to do this by
passing in the index of the item I want selected?

Thanks
 
Here it is in VB it is probably very simulat in c#

Here I am searching the Dropdown for a certin item then setting the
Selectedindex to it

Dim i As Int16 = 0

Me.DropDown1.SelectedIndex = i

While Me.DropDown1.SelectedItem.Text <> "Item 4 Description" And i <
Me.DropDown1.Items.Count

Me.DropDown1.SelectedIndex = i

i = i + 1

End While
 
Thanks Brad for the suggestions.

Hello Jin,

There is no selectedIndex property in listView, but you can use
ListViewItem.Selected property to select the item. It can work in .net CF.

listView1.Items[0].Selected =true;

Does this answer your question? Thanks.

Lewis

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "Jin Kim" <[email protected]>
| Sender: "Jin Kim" <[email protected]>
| Subject: Programmatically Selecting An Item in a Listview
| Date: Tue, 29 Jul 2003 09:48:18 -0700
| Lines: 5
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNV8TaeJL5MfQ8bQ1WeWViKHFuPnw==
| Newsgroups: microsoft.public.dotnet.framework
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:50057
| NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
| X-Tomcat-NG: microsoft.public.dotnet.framework
|
| I'm trying to select an item in a listview using C#
| and .NET Compact Framework. Is there anyway to do this by
| passing in the index of the item I want selected?
|
| Thanks
|
 
Hi Lewis,

I tried that and got:

(268): 'System.Windows.Forms.ListView.Items' denotes
a 'property' where a 'method' was expected

Thanks for your help.
Jin
 
Hi Jin,

If you are using C#, you can copy this code snippet:
listView1.Items[0].Selected =true;

Note: C# uses square brackets for indexers.

If it is in VB.NET, then you can use: listView1.Items(0).Selected =true

Please let me know if it can work. Thanks.

Best Regards,
Lewis

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
| Content-Class: urn:content-classes:message
| From: "Jin Kim" <[email protected]>
| Sender: "Jin Kim" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: Programmatically Selecting An Item in a Listview
| Date: Wed, 30 Jul 2003 07:58:59 -0700
| Lines: 62
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNWqxs0xeNdjOA0SQ2kQPpnkoVYng==
| Newsgroups: microsoft.public.dotnet.framework
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:50132
| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| X-Tomcat-NG: microsoft.public.dotnet.framework
|
| Hi Lewis,
|
| I tried that and got:
|
| (268): 'System.Windows.Forms.ListView.Items' denotes
| a 'property' where a 'method' was expected
|
| Thanks for your help.
| Jin
|
| >-----Original Message-----
| >Thanks Brad for the suggestions.
| >
| >Hello Jin,
| >
| >There is no selectedIndex property in listView, but you
| can use
| >ListViewItem.Selected property to select the item. It can
| work in .net CF.
| >
| >listView1.Items[0].Selected =true;
| >
| >Does this answer your question? Thanks.
| >
| >Lewis
| >
| >This posting is provided "AS IS" with no warranties, and
| confers no rights.
| >
| >--------------------
| >| Content-Class: urn:content-classes:message
| >| From: "Jin Kim" <[email protected]>
| >| Sender: "Jin Kim" <[email protected]>
| >| Subject: Programmatically Selecting An Item in a
| Listview
| >| Date: Tue, 29 Jul 2003 09:48:18 -0700
| >| Lines: 5
| >| Message-ID: <[email protected]>
| >| MIME-Version: 1.0
| >| Content-Type: text/plain;
| >| charset="iso-8859-1"
| >| Content-Transfer-Encoding: 7bit
| >| X-Newsreader: Microsoft CDO for Windows 2000
| >| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| >| Thread-Index: AcNV8TaeJL5MfQ8bQ1WeWViKHFuPnw==
| >| Newsgroups: microsoft.public.dotnet.framework
| >| Path: cpmsftngxa06.phx.gbl
| >| Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.framework:50057
| >| NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
| >| X-Tomcat-NG: microsoft.public.dotnet.framework
| >|
| >| I'm trying to select an item in a listview using C#
| >| and .NET Compact Framework. Is there anyway to do this
| by
| >| passing in the index of the item I want selected?
| >|
| >| Thanks
| >|
| >
| >.
| >
|
 
Hello, first time post here...
The selecting a row from a listview, I had the same problem.
Hope this helps, it works for me...may not be the prettiest of code..
This will display the second column in a message box.

Code:
[size=2][color=#0000ff]private[/color][/size][size=2][color=#0000ff]void[/color][/size][size=2] listview1_SelectedIndexChanged([/size][size=2][color=#0000ff]object[/color][/size][size=2] sender, [/size][size=2][color=#008080]EventArgs[/color][/size][size=2] e){
 
[/size][size=2][color=#0000ff]	for[/color][/size][size=2] ([/size][size=2][color=#0000ff]int[/color][/size][size=2] x = 0; x < listview1.Items.Count; x++){
 
[/size][size=2][color=#0000ff]		 if[/color][/size][size=2] (listview1.Items[x].Selected)[/size]
[size=2][color=#008080]		 MessageBox[/color][/size][size=2].Show(listview1.Items[x].SubItems[1].Text);
	}
 
}
 
[/size]

Bye, :)

LoneFerret
___________________________________
 
problems with setting selected index listview items

I also had a lot of problems I got this message

error message Object reference not set to an instance of an object

to this.lvColumns.Items[selectedText].Selected = true; in

if (lvColumns.Items.Count >0)

{ lvColumns.Focus();

if (myselectedText != "")

{

lvi.Selected = true;

this.lvColumns.Items[myselectedText].Selected = true;



}

}
where lvcomuns was my listbox
which was odd as in debug mode you could see it was not null.

I got round it with
if (lvi.Text == selectedText)

{

lvi.Selected =
true;

}

where
l

ListViewItem = vi = lvColumns.Items.Add(somestuff);

I also found this useful to get at text of focused item
if (lvColumns.SelectedItems.Count >0)

{

lvColumns.Focus();

myselectedText = lvColumns.FocusedItem.Text;

}

 
Back
Top