I have list view and some columns "ID", "product name" and "Vendor". I can add, edit or delete those items and after "messing up" with items list view is refreshed something like this.
listView.Items.Clear();
foreach (var item in result)
{
ListViewItem lv = new ListViewItem(item.ID.ToString());
lv.SubItems.Add(item.Name.ToString());
lv.SubItems.Add(item.Vendor.ToString());
listView.Items.Add(lv);
}
so this method works fine except every time i refresh list focus of item is lost, which is logical because i deleted all items from list and filled it again.
So my problem is how can i keep focus on edited and newly added items, primary when i edit some items i would like listview not to scroll to the top but to stay on place where that edited item was.
I tried with method FindItemWithText and than setting focused items of listview to item that is found but it didn't work.
Is there ant solution to this kind of problem?
Save focused item's index to the variable:
int oldFocusedIndex = listView.SelectedItems[0].Index;
Later then you can restore focus executing:
listView.Items[oldFocusedIndex].Selected = true;
You can also make the item visible to the user due to the ListView's scroll bar will remain at the top after the Clear() method:
listView.Items[oldFocusedIndex].EnsureVisible();
Related
i am working on windows form application ..i have a list view.
in the list view some time i will select some row then i will click add button .that time selected rows will add to list view.
after that i want to make my listview selection mode none so i wrote code like this
List_Item.Select = Nothing ..
but this code throwing error? how i can make my list view selection mode none?
any help is very appreciable.thanks in advance
loop every item and set the selected field to false
For Each item As ListViewItem In Me.List_Item.Items
item.Selected = false
Next
I have a ListView contains 100 items, and we can do certain activities based on selected items. I have used EnsureVisible() method to adjust the visibility and my ListView refreshing time to time to update the data.
The problem now i am facing is if I select the first item in the list, I then start paging down (maybe select the 21th, 59th
and 75th) during this select, I could have highlighted the 1st, 24th and
56th when the control suddenly refreshes the page in this instance, only the 1st selected retains the focus and I have lost my other selections and have to go through again either individually or just quicker.
So my question is how i can select multiple items and do the action while ListView is refreshing during certain interval to fill data though EnsureVisible() is used?
This is my exisitng code:
if (_listviewFirst.SelectedItems.Count > 0)
{
_listviewFirst.SelectedItems[_listviewFirst.SelectedItems.Count - 1].EnsureVisible();
_listviewFirst.SelectedItems[0].EnsureVisible();
}
I would use the ListView.ItemSelectionChanged (MSDN) event and add/remove items (or their references) from a List of selected items. When your control is refreshed you should then iterate your selected items and update the items to selected in your ListView.
You will be able to take advantage of e.IsSelected and e.Item or e.ItemIndex in the event handler to do this.
I wouldn't use EnsureVisible at all for this.
I have a DataGridView where there is a cell which is a DataGridViewComboCell. Each DataGridViewComboCell is bound to a unique copy of a BindingList. When I remove an item from the binding list the comboboxes remove the entry I had removed from the bindinglist.
However, if that value is selected it stays as the selected item in the cell.
I tried doing a datagridview.refresh(), but it still didn't help. It is getting called from a tool strip menu item
// _contractLists is List<BindingList<String>> which is the datasource for a datagridviewcombobox
List<String> removedList = new List<string>();
_contractSelForm.ShowDialog();
_contractSelForm.GetandClearRemovedContracts(ref removedList);
foreach (BindingList<String> contractList in _contractLists)
{
// remove deleted favorites
foreach (string contract_name in removedList)
{
contractList.Remove(contract_name);
}
}
dataGridView1.Refresh();
dataGridView1.EndEdit();
Couple of things to note/look at:
1) You shouldn't need to call EndEdit after Refresh. If it needs to be called, you should call it before Refresh.
2) If your comboboxes have a DropDownStyle of DropDown, then I this is expected behavior.
From the MSDN documentation:
If you set the DropDownStyle property to DropDown, you can type any value in the editable area of the ComboBox.
To change this, either change the DropDownStyle to DropDownList or manually clear the value in code after removing the items.
I don't know if the title express what I want. I have a ListBox in WPF where I generate many elements. When I click on a element while still generating I want my selected item to not move down the list, so I cannot see it anymore, I want to stay in the exact position where I click on it.
If this is possible, can someone point some ideas on how to do it in C#?
Thanks.
Assuming that this is even a good idea and that you are using winforms
Step 1:
Determine the index of the selected item in the source.
Step 2:
When your adding items to the ListBox split the ListBox at the index where the item previously was insert the item at that point, then add on the remainder of the items, while making sure that you've removed the item if it is now elsewhere in the list.
Code:
//Let's assume that you know how to get the position of the item when it is clicked and save the
//item to a variable called OriginalItem
public void PutTheItemInTheSameSpot()
{
var listboxitems = (List<Integer>)YourListBox.DataSource;
var originalClikedItem = OriginalItem;
var topPart = new List<Integer>();
for (i = 0; i < itemPosition; i++)
{
topPart.Add(listboxItems[i]);
}
topPart.Add(originalClickedItem);
var bottomPart = listboxitems.Remove(toppart);
YourListBox.DataSource = toppart.AddRange(bottomPart);
}
Saw your edit about it being WPF
The could should work in idea.
Just a thought: you could try having your view respond to an event whenever an item is added to your ListBox. In the event handler, you could force the selected item to scroll into view presumably keeping it in the current "viewable" position:
listBox.ScrollIntoView(listBox.SelectedItem);
I've never tried this before so it may or may not produce the desired affect?
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);