selection issue in listview or TreeView - c#

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);

Related

list view selection mode none in windows form applications

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

How to use ensurevisible method in listview?

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.

prevent item losing focus when refreshing list view in c#

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();

c# wpf checkbox content from one list to another

I have a list where i dynamically store checkboxes(with grid content in it). Next to the list i have a button who,when clicked, must put the selected checkboxes with content in the right list. When delete from right list is clicked, the selected item(s) on the right side needs to be deleted.
How is this possible? Suppose i have this code:
CheckBox cbox = new CheckBox();
Grid panel= new Grid();
panel.Width = 260;
cbox.Content = panel;
You can use:
Listbox.ItemTemplate to represent the data you want (so that it looks like CheckBox:Label)
Use 2 ObservableCollections to store 2 lists that you have
In your buttons you can bind to Current Item in the List it is associated with
On button click, it will get current select Item and Add/Remove from ObservableCollections as needed
Let me know if you need specific details. The good thing about ObservableCollections is that whenever collection changes, anything bound to it (like ListBox) will get updated.
so that any manupilation with the data is outside View so to speak and done on ViewModel.

ListView problem: it does not Jump to the selected item

Ok I am saying lsvAvailable.Items[index].Selected = true;
and HideSelection is false so it shows the gray back color on the selected item, which is what I want .. but if the found item is somewhere down the list so I need scrolling to SEE it. it does Not JUMP to that item...and still I need to ba able to continue typing ( let's say I am typing in a text box like a search box and it is showing the item in the listView)
Call the ListViewItem.EnsureVisible method:
lsvAvailable.Items[index].EnsureVisible();

Categories

Resources