I have ListView which shows images from an ImageList. Now wanted to get index of all checked images in ListView.
List<int> list = new List<int>(); // in list index of all checked images on clicking button should be saved.
private void button2_Click(object sender, EventArgs e)
{
ListView.CheckedListViewItemCollection checkedItems = lstview1.CheckedItems;
foreach (ListViewItem item in checkedItems)
{
list.add[// How can i get index of checked item ];
}
}
ListView already has the CheckedIndices property. You probably ought to use it directly, but you can get a List<> out of it with a Linq one-liner:
var list = listView1.CheckedIndices.Cast<int>().ToList();
Well, I'm not sure I understand your question completely, but you can get the index of a ListViewItem with item.Index.
ListView.CheckedListViewItemCollection checkedItems = lstview1.CheckedItems;
foreach (ListViewItem item in checkedItems)
{
// This will fill the list with ListViewItems that are checked
list.add(listview1.Items[item.Index]);
}
Related
I am populating a list-view with passwords.
Then I want to take the selected items text and pass it to a text box when it is clicked.
So far I have:
private void passwordListView_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem listViewItem = new ListViewItem();
listViewItem = passwordListView.SelectedItems[0];
passwordTextBox.Text = listViewItem.Text;
}
It works the first time I press it and it populates the textbox but then if I click a different password in the list-view it throws an exception.
Have I left out something blatantly obvious?
When the Selected Index is changed in a winforms ListView the first event is for the item that has now been deselected. So at that point SelectedItems is empty.
Check for this through if (passwordListView.SelectedItems.Count == 0) return;
After this you will get a second event, which will be for the new selection, and you can act on this.
Btw, you don't need to make a new ListViewItem as you are in your snippet, this will save the extra unnecessary creation:
ListViewItem listViewItem = passwordListView.SelectedItems[0];
Maybe you could try this :
private void passwordListView_SelectedIndexChanged(object sender, EventArgs e)
{
if(passwordListView.SelectedItems.Count > 0)
passwordTextBox.Text = passwordListView.SelectedItems.First().Text;
}
I've got this code that populates a ListBox when a flyout is opened:
private void flyoutOpenPhotosets_Opened(object sender, object e)
{
lstbxPhotosets.ItemsSource = PhotraxSQLiteUtils.GetPhotosets();
foreach (String pset in App.CurrentlyMappedPhotosets)
{
int lstbxIndex = lstbxPhotosets.Items.IndexOf(pset);
if (lstbxIndex >= 0)
{
lstbxPhotosets.Items[lstbxIndex].? what now?
}
}
}
GetPhotosets returns a List. That part works (the list box is populated with the appropriate string values)
The problem is with the rest of the code (the foreach block).
CurrentlyMappedPhotosets is also a List. I want matching members among the strings in CurrentlyMappedPhotosets and those in the ListBox to cause the item in the ListBox to be selected when the flyout displays.
I was hoping to do be able to do something like this:
lstbxPhotosets.Items[lstbxIndex].Selected = true;
...but lstbxPhotosets is disallowing that.
So how can I programmatically select specified ListBox items?
Use
lstbxPhotosets.SelectedIndex = lstbxIndex
I am trying to add a clone copy of all checked items in a listviewOne to listviewTwo.
along with other text box values to the listviewTwo. But I am getting error .. Please someone help me..
private void button4_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.Items)
if (item.Checked)
{
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add(textBox1.Text);
lvi.SubItems.Add(textBox2.Text);
lvi.SubItems.Add(textBox3.Text);
lvi.SubItems.Add(textBox4.Text);
lvi.SubItems.Add(item.Text);
listView2.Items.Add(lvi);
}
}
This line has the error:
lvi.SubItems.Add((ListViewItem)item.Clone());
You can't add a ListViewItem to the SubItems collection. You can either add a ListViewSubItem or add a string (which creates a new item with that string).
In this case, maybe it's good enough if you create a new sub-item using the text from the checked items in listView1. Change the line to:
lvi.SubItems.Add(item.Text);
Ok, so you want all the subItems from listView1 added to the item of listView2. Try this:
foreach (ListViewItem item in listView1.Items)
{
if (item.Checked)
{
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add(textBox1.Text);
lvi.SubItems.Add(textBox2.Text);
lvi.SubItems.Add(textBox3.Text);
lvi.SubItems.Add(textBox4.Text);
foreach (ListViewSubItem subItem in item.SubItems)
{
lvi.SubItems.Add(subItem);
}
listView2.Items.Add(lvi);
}
}
I am working with windows form application using c#. I have two list boxes i.e listbox1 and listbox2 and one button i.e btnall. listbox1 is bind using databind and listbox2 is empty. I want to copy all the items from listbox1 which is binded to other listbox2 which is empty. On the click of btnAll_click event.
I am trying this
private void btnAll_Click(object sender, EventArgs e)
{
listbox2.Items.AddRange(listbox1.Items);
}
but I am getting data.datarowview instead of the values.
You could loop through the items in LIstbox1 and add them one at a time like this:
foreach (var item in Listbox1.Items)
{
listbox2.Items.Add(item.ToString());
}
Would that do what you want?
Try this
listbox2.DataSource = listbox1.Items;
If you are populating the list control via the DataSource property try setting DataSource and DataMember. and dont forget :
listBox.DisplayMember = "displayMember";
listBox.ValueMember = "valueMember";
else try this:
var mylistSource = new List<string>();
foreach (var item in Listbox1.Items)
{
mylistSource.Add(item.ToString());
}
listBox2.DataSource = mylistSource;
What I have going on is i am using
string proj = listView2.SelectedItems[0].ToString();
to capture the item of my selected listview item, this works fine the first around, when i click on another listview item it throw the exception of
InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
Any thoughts?
private void listView2_SelectedIndexChanged(object sender, EventArgs e)
{
string proj = listView2.SelectedItems[0].ToString();
}
working:
string proj ="";
ListView.SelectedListViewItemCollection lv2si =
this.listView2.SelectedItems;
foreach (ListViewItem item in lv2si)
{
proj = item.ToString();
}
What if no item is selected in listview ? The collection will contain zero items and it will throw this exception.
Instead, obtain ListViewItemCollection and use foreach to iterate over it. As described in MSDN example:
ListView.SelectedListViewItemCollection breakfast =
this.ListView1.SelectedItems;
double price = 0.0;
foreach ( ListViewItem item in breakfast )
{
price += Double.Parse(item.SubItems[1].Text);
}
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selecteditems.aspx
You should test that the index value is valid before you access it.
private void listView2_SelectedIndexChanged(object sender, EventArgs e)
{
string proj;
if (listView2.SelectedItems.Count > 0)
proj = listView2.SelectedItems[0].ToString();
else
proj = string.Empty;
}
The SelectedIndexChanged event is raised whenever the Selected property of a ListViewItem changes. This means the event is raised twice: once when the first item is deselected, and a second time when the item you clicked on is selected. Since you are not checking whether SelectedItems has any items, you get the exception the first time the event is raised.
As #Jeffrey L Whitledge shows in his answer, you can use SelectedItems.Count to determine whether any items are selected.