Selecting a ListViewItem!

G

greg

Alright, googled this and found a ton of articles but none seem to help me.

Trying to set the selected item in a ListView. I'll just throw out some
pseudo-code here and it's basically not working. What am I missing?

foreach item in listview.items
if (some condition)
item.selected = true; // i've set a breakpoint here, it's called.

textBox1.text = listView1.selecteditems[0].text

Argument out of range happens. selectedItems is empty. Yet while I'm
debugging, in the Immiediate window I can evaluate

?listView1.items[0].selected
true

?listView1.SelectedItems
.... Count : 0 ...

From google I've tried executing listView1.Select() but that does nothing.

Is there some event I should manually be calling to force it to update the
SelectedItems list?

Thanks,
Greg
 
L

Linda Liu[MSFT]

Hi Greg,

I performed a test based on your decription but didn't reproduce the
problem on my side.

In my test, I add a ListView and a TextBox and a Button on a form. The
following is the code in the form.
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
this.listView1.View = View.Details;

// add a ColumnHeader in the ListView
ColumnHeader colheader = new ColumnHeader();
colheader.Text = "column1";
this.listView1.Columns.Add(colheader);

// add some items in the ListView
ListViewItem item = new ListViewItem("11");
this.listView1.Items.Add(item);
item = new ListViewItem("22");
this.listView1.Items.Add(item);
item = new ListViewItem("33");
this.listView1.Items.Add(item);
}

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.listView1.Items.Count; i++)
{
if (this.listView1.Items.Text == "22")
{
this.listView1.Items.Selected = true;
break;
}
}
if (this.listView1.SelectedItems.Count > 0)
{
this.textBox1.Text = this.listView1.SelectedItems[0].Text;
}
}
}

Run the application. Click the button on the form and text "22" appears in
the TextBox.
Could you please provide the code snippet in your application that is
relevant to the problem, or send me a simple project that could just
reproduce the problem?

I look forward to your reply!

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

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

greg

The following simplified code is not working.

Note that at the comment ABC, what I've done is put in "textbox1.text =
anItem.text" as a workaround which works.. but I'm confused why this isn't
working?

generateTextBoxContents throws the error.

Thanks,

Greg


public void setValues(String idValues)
{
foreach (ListViewItem anItem in listView1.Items)
if
(anItem.SubItems[SetColumnToSearchOn].Text.ToUpper().Equals(idValues.ToUpper()))
{
anItem.Selected = true;
// ABC
break;
}
generateTextBoxContents();
}

// Updates text box contents.
private void generateTextBoxContents()
{
textBox1.Text = "";

textBox1.Text = listView1.SelectedItems[0].Text;
}
 
P

Peter Duniho

The following simplified code is not working.

It's also not a complete sample of code.
Note that at the comment ABC, what I've done is put in "textbox1.text =
anItem.text" as a workaround which works.. but I'm confused why this
isn't
working?

If and when you post a concise-but-complete sample of code that reliably
demonstrates the problem, it's possible someone could tell you.

Until then, it's not likely you'll get a specific answer.

Pete
 
L

Linda Liu[MSFT]

Hi Greg,

Thank you for your reply!

The code snippet you provided in the previous reply should have no problem.

To solve the problem quickly, would you please create a simple project that
could just reproduce the problem and send it to me? To get my actual email
address, remove 'online' from my displayed email address.

I look forward to your reply.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
G

greg

Unfortunately, I cannot duplicate this bug in a second project.

In debugging mode, I've stepped through my real project and a test project
in attempt to re-create the bug. Both follow the same simple logic flow

- Set .selected to true
- Call a function
- This function iterates through all SelectedItems.

In my real project, SelectedItems is empty. In the new project, it is of
size one. I'll have to settle with the work around for now as I do not have
the time to try and reproduce it.
 
L

Linda Liu[MSFT]

Hi Greg,

Thank you for your reply and detailed information!

Since you couldn't reproduce the problem in a test project, I think that
the selected items in the ListView are modified somewhere else.

Since you don't have time to try and reproduce the problem, I will close
this issue temporarily. If you have time and reproduce the problem in the
future, please re-open this issue and I will work with you then.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top