listview error c#
I have a ListView. I want to select the first element from it and then invoke a method. The thing is that though i have the element in the listview I dont know why the following code is not working when i want to select the element. need help. thx
listview.Items[0].Selected = true开发者_Go百科;
listview.Select();
int c =listview.SelectedItems.Count;
MessageBox.Show(c + ": and : " + listview.Items[0].ToString()); .//here the c=0 and it should be 1:(
// Thread.Sleep(3000);
method();
I have:
ListViewItem it = new ListViewItem("a");
it.SubItems.Add("s");
it.SubItems.Add(""v");
it.Tag = call;
listview.Items.Add(it);
for
System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("fdasf");
System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("sdfsadf");
System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem("gdsgdfg");
and
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem4,
listViewItem5,
listViewItem6});
this.listView1.Location = new System.Drawing.Point(84, 88);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(121, 97);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.List;
this works and c is 1
listView1.Items[0].Selected = true;
listView1.Select();
int c =listView1.SelectedItems.Count;
MessageBox.Show(c + ": and : " + listView1.Items[0].ToString());
// Thread.Sleep(3000);
method();
The problem is probably somewhere else.
You seem to be using the correct way to select an item:
listview.Items[0].Selected = true;
listview.SelectedIndex = 1; try this.
精彩评论