I have a list box and I need to modify the list based on the content. I am trying to do this but it does not do any thing.
string itemRemove = "Apple";
lstFruits.Items.Remove(itemRemove);
The problem is that in a ListBox control, you cant remove an item like you remove them from List<T>(i.e using an enumerator). You have to you have to loop using an index, starting at the last item, like this :
for (int n = lstFruits.Items.Count - 1; n >= 0; --n)
{
string itemRemove = "Apple";
if (lstFruits.Items[n].ToString().Contains(itemRemove))
{
lstFruits.Items.RemoveAt(n);
}
}
Related
I basically populate a comboBox with a range of numbers using the following code:
comboBox1.Items.AddRange(Enumerable.Range(0, 50).Cast<object>().ToArray())
The thing is that I'd like to have the unit of measurement comparing soon after the numbers. Thus my question, how can I add a string to each item of my comboBox?
You can do it using a Linq expression:
comboBox1.Items.AddRange(Enumerable.Range(0, 50).Select(x => x.ToString() + " sufix").Cast<object>().ToArray())
Additionally to the solution provided by Diego Rafauel Souza you could also append the text to each item of the combobox:
for (var index = 0; index < comboBox1.Items.Count; index++)
{
var item = comboBox1.Items [index];
comboBox1.Items[index] = $"{item} {suffix}";
}
This simply adds the suffix to every item of the comboBox.
I am trying to iterate through two listBoxes for a program I am coding. Both listBoxes will have a different item count inside of it.
Basically, I want my program to get the selectedItem from one listBox and use the string or text from that item to replace the text from EACH and EVERY single item in the other listBox.
Once it's done using the selectedItem from the original listBox for all the items in the other listBox, I want it to go to the next item in the original listBox and do the same process all over again.
It should repeat this UNTIL it has gone through ALL of the items in the original listBox.
Hopefully that made sense....
Here is some example code I made. I created two for loops so that it could iterate through both listBoxes.
for (int i = 0; i < listBoxOriginal.Items.Count; i++)
{
string linkurl = listBoxOriginal.Items[i].ToString() + "..";
listBoxNewListBox.SelectedIndex = 0;
for (int o = 0; o < listBoxNewListBox.Items.Count; o++)
{
string s = listBoxNewListBox.Items[o] as string;
string newurl = s.Replace("DOMAIN", linkurl);
listBoxNewListBox.SelectedIndex++;
}
}
My issue is, when the inner for loop finishes iterating completely it errors out. I know the error is because it reached the end of the listBox and can't go any further, but I don't know how else to iterate through the listBox without having the items selected.
What it should do is, once it reaches the end of "listBoxNewListBox" it should go to the next item in "listBoxOriginal", and perform the same process all over again until it's done going through every item in "listBoxOriginal".
Any help would be appreciated!
I think problem is because of SelectedIndex at list. I think it's just going to far.
Here is a little modification:
for (int i = 0; i < listBoxOriginal.Items.Count; i++)
{
string linkurl = listBoxOriginal.Items[i].ToString() + "..";
for (int o = 0; o < listBoxNewListBox.Items.Count; o++)
{
string s = listBoxNewListBox.Items[o] as string;
string newurl = s.Replace("DOMAIN", linkurl);
listBoxNewListBox.SelectedIndex = o;
}
}
Here is explanation:
When in inner loop you are doing this operation: listBoxNewListBox.SelectedIndex++ you are setting this index as 1 more than index of loop. That means, if we look at very last iteration of inner loop, this index is set with value which is already to high. This is probably reason why application throws an exception.
So basically I am using a FOR loop to add and remove options from a listbox.
It functions correctly when selected 1 option (from either remove or select) and it functions correctly when I select two separate options (For example, item[0] and item[4]).
However, I am having trouble when I try to select all options or when I select two items that are side by side ([2],[3].. etc)
Here is my loop for the select function:
protected void btnSelect_Click(object sender, EventArgs e)
{
for (int intCounter = 0; intCounter < lbSnacks.Items.Count; intCounter++)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSelected.Items.Add(lbSnacks.Items[intCounter]);
}
}
for (int intCounter = 0; intCounter < lbSnacks.Items.Count; intCounter++)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSnacks.Items.Remove(lbSnacks.Items[intCounter]);
}
}
}
The error is basically taking the item and putting it into the "selected" listbox but it is leaving behind one of the two options in the original "snacks" box.
Any ideas?
The problem is, when you remove an item, all of the other items shift downwards - which means the next loop iteration (since it increments your index) "skips" one item.
There are various ways to handle this. The simplest is to just loop backwards:
for (int intCounter = lbSnacks.Items.Count-1; intCounter >= 0; intCounter--)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSelected.Items.Add(lbSnacks.Items[intCounter]);
lbSnacks.Items.Remove(lbSnacks.Items[intCounter]);
}
}
This way, when the items "shift", it doesn't matter, since you've already dealt with those items.
So here is the code I have the allows me to delete selected items in my listbox.
ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(lstOutput);
selectedItems = lstOutput.SelectedItems;
if (lstOutput.SelectedIndex != -1)
{
for (int i = selectedItems.Count - 1; i >= 0; i--)
lstOutput.Items.Remove(selectedItems[i]);
}
else
MessageBox.Show("Debe seleccionar un email");
The problem is that I have labels at the top that show what the output is. I also have statistics at the bottom of the page. The way the code is now, I am able to delete those, which I don't want. I am unsure of how to prevent these from being deleted.
first of all the fire line is unneeded and you can merger the two first line to:
ListBox.SelectedObjectCollection selectedItems = lstOutput.SelectedItems;
now for the remove of the item that you want to keep. you can make a condition.
for (int i = selectedItems.Count - 1; i >= 0; i--)
{
if(selectedItems[i] is /*here is where you come and check if the current selected item is of the type you don't want to delete*/)
lstOutput.Items.Remove(selectedItems[i]);
}
if you'll tell me what's the type of the "labels at the top" and "statistics at the bottom" i'll put it in the answer
EDIT
in similar to what you said you can do:
List<object> fixedItems = new List<object>();
fixedItems.Add(/*Your labels and statistics*/);
and then do
for (int i = selectedItems.Count - 1; i >= 0; i--)
{
if(fixedItems.Contains(selectedItems[i]) == false)
lstOutput.Items.Remove(selectedItems[i]);
}
for the list you'll need to add
using System.Collections.Generic;
at the beginning of the page
I'm using a ListView in Details mode to display a list. I want to change the current index in two ways: firstly, by a mouse click (which works now), and secondly with + and - buttons. The problem is that when I click the button, the list loses focus and the row highlight disappears. How do I keep the highlight?
EDIT: Okay, I found the HideSelection property. But how do I change the selected index from the outside?
You can do something simple like this
this.listView1.Items[0].Selected = true;
Or you can iterate throught the list of items and find the one that you want to select.
private void PlusButtonClick()
{
int newIndex = 0;
for (int x = 0; x < listView1.Items.Count; x++)
{
if(listItem.Selected);
{
listItem.Selected = false;
newIndex = x++;
break;
}
}
this.listView1.Items[newIndex].Selected = true;
}