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;
}
Related
I'm trying to create previous and next buttons on a page to navigate through items in a drop down list. Clicking next should select the next item in the DDL and clicking previous should go to the previous item.
Here's something I've attempted for the next button but it just takes me to the last row.
protected void btnNext_Click(object sender, EventArgs e)
{
int currentSelection = DDL.SelectedIndex;
for (int i = currentSelection; i < DDL.Items.Count; i++)
{
string nextSelection = (DDL.Items[i].ToString());
DDL.SelectedValue = nextSelection;
}
}
You're looping through all the items in the list until you get to the last one, and selecting each item individually until the loop exits. Drop a breakpoint inside that loop and debug to see what I mean.
You don't need any looping here at all. What you'd want instead would be simply:
int nextIndex = DDL.SelectedIndex + 1;
if (nextIndex + 1 >= DDL.Items.Count)
return; // We're on the last item, do nothing (or whatever you like)
DDL.SelectedValue = DDL.Items[nextIndex].ToString();
You easily can change selected value by its index so you don't need change it by its value.
int i = ddl.SelectedIndex;
if (i+1!=ddl.Items.Count)
{
ddl.SelectedIndex = i + 1;
}
else
{
ddl.SelectedIndex = i;
}
as you said in comment it wont change index if the current index is the last one.
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);
}
}
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 want the code to disable default selection in dropdownlist in asp.net and also on selection of a particular data field the values are displayed in the txtbox below.the dropdown should be filled with system related data like eg c:drive, d ... etc dynamically at run time.
then you can manually append ""--select--"" in the first place in the dropdownlist by edit items. and set appenddatabounditems to true.. thus you will get select in the first place rather than the first drive by default......
ok then use this function and cal this function on the databound event of dropdownlist.
void RemoveDuplicateItems(DropDownList ddl)
{
for (int i = 0; i < ddl.Items.Count; i++)
{
ddl.SelectedIndex = i;
string str = ddl.SelectedItem.ToString();
for (int counter = i + 1; counter < ddl.Items.Count; counter++)
{
ddl.SelectedIndex = counter;
string compareStr = ddl.SelectedItem.ToString();
if (str == compareStr)
{
ddl.Items.RemoveAt(counter);
counter = counter - 1;
}
}
}
}
and call this function in dropdownlist_databound() event.
then you can manually append ""--select--"" in the first place in the dropdownlist by edit items. and set appenddatabounditems to true.. thus you will get select in the first place rather than the first drive by default......