Place list item at specific position in CheckBox List - c#

I have two checkboxlist
checkboxlist1 has 5 items and checkboxlist2 has 3 items
During a button event click, i need to move the selected items from checkboxlist1 to checkboxlist2
but i need to make sure the items selected from checkboxlist1 needs to be placed above or below an item checked in checkboxlist2
how to add items at a specific position in checkboxlist2 and move the remaining items down
Below code will actually put the item at the end
checkboxlist2.Items.Add(listitemselectedfromcheckboxlist1)

try this
checkboxlist2.Items.Insert(index , listitemselectedfromcheckboxlist1)

Have you tried manipulating Items by using Insert method overload ?
I mean
checkboxlist2.Items.Insert(index:3,item);

How about using this for multiple selections,
checkboxlist1.SelectedIndices.Cast<int>().ToList().ForEach(x =>
{
checkboxlist2.Items.Insert(x, checkboxlist1.Items[x]);
});
Hope this helps...

Related

Listbox multiple selection always selected all

I have one problem with listbox.
I use a listbox and the count is added when selecting the listbox items. But I have two items in the listbox that are always selected and the count is always 2.
What do I do to select a single or mutiple selection?
Two items always selected because of below two reasons:
1) They are selected in the inline code - selected = "true"
2) They are getting selected in the PageLoad method of codebehind and you are calling without Not IspostBack.
Please check.
One fact is you've set Mutliple selection mode.
another fact, is when you insert an item it is selected, if add another both are selected.
So after you add an item set
ListBox1.SelectedIndex = -1;
Otherwise, check suggestions given by Saurabh
Set ListSelectionMode to Multiple and then iterate through the listbox items
foreach (ListItem item in ListBox1.Items)
{
if(item.Selected)
{
count++;
}
}

How to get the index of the recently selected item in a list box

I have a listbox in which i have some items. I select some items in the listbox. The condition is that i want the selection to be continuous. If I select any other item in listbox which is not continuous with the selection,that item should be deselected immediately. I need to have the index of recently selected item which i tried to get with the help of SelectionChangedEvent but it gives me the index of first selected item. How to do that?
You're looking for the last item in the SelectedIndices collection.
Keep the indicies of those continously selected items somewhere.
When any item is selected or unselected, catch this in SelectionChanged event. Check the SelectedIndicies collection, like SLaks suggested, to see if something except than your collection is selected, or if some of it's items were unselected. If you need, restore the presentation of listBox.

listview item indexing

I am using a listview with 6 items per page. When I choose an item from the 6 items it should take me to a page with 3 items per page BUT the selected item (on previous page) as FIRST item in the list on the new page. Basically how do I set which items shows first in the listview? Is that a property?
There is no property for setting the first item. If you are using a datasource, the items will be displayed in the order they occur in the datasource, which is generally the order that the database returns them.
If you need a different ordering, you can code a loop through your datasource and assign the items to the ListView manually.

selection issue in listview or TreeView

Hello i have a list view control,
While the form is being loaded i fill the list, i have aprrox. say 100+ items.
While filling i check some parameters and decide which item/row need to be selected.
i set the Selected property to true... refer the code below:
some lines here .....
ListViewItem listViewItem = new ListViewItem("COL1");
listViewItem.SubItems.Add("COL2");
check for some condition and then
listViewItem.Selected = true;
this.m_lstViewCtrl.Items.Add(listViewItem);
This does select the item, there are no issues with it...
however, say the ctrl is sized to see onlu say some 15 items, but the selection is say some 35th item.... currently the scroll bar appears the item is selected but i have to scroll to see what was selected?
is it possible to scroll to the selected item so that is selection is clearly visible...
Will the same apply for a Treeview?
Use the EnsureVisible property on the ListViewItem.
UPDATE: So, your code would be as follows:
listViewItem.Selected = true;
listViewItem.EnsureVisible();
this.m_lstViewCtrl.Items.Add(listViewItem);

Display the items in DropDownList

In my project i have used four number of dropdownlists in a single form.
when im selecting an item from the 1st dropdownlist,the dropdownlist2 has to display the items that matches the selected item from dropdowmlist1.
Please clear my doubt.,
Use the SelectedIndexChanged-event on the first dropdownlist. Everytime the user selects a different item in the dropdownlist, this event is fired. In the method body you can now react to the change and filter the items of the second dropdownlist to match the selected item of the first dropdownlist. You can either do that manually in a loop or use LINQ (Enumerable.Select).
first of all its called a combobox.
Implement the OnSelectedItemChanged event from the combobox to fill the 2nd combobox
linky

Categories

Resources