Listbox item stay focused after selection - c#

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?

Related

How to move Items from one list to another by selecting the items in a datagrid

I have a couple of datagrids that uses a list of items as a source. I would like to move the item from list1 to list2 when the item in the grid is selected when I click a button or when I double click any of the cells in the row where the item is shown. This process will delete the selected item in the first list, and add it to the second one (it will disappear from the grid and be added to another grid which is linked to the 2nd list).
The items in both lists are part of the same class and both use the same constructor, so all of their parameters are the same.
I have been looking around the internet and trying different things myself but i cant quite find the solution, up to now this is what I came up with, but i cant make it to work.
public void Gladiators_Data_Grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string ItemMoving =
Data_Grid.Rows[e.RowIndex].Cells[0].Value.ToString();
var item = List1.FirstOrDefault(x => x.Name == ItemMoving);
if (item != null)
{
List1.Remove(item);
List2.Add(item);
}
}
The solution to my problem doesn't have to follow the pattern that I tried to use, anything that works will be highly appreciated, thanks in advance.
1) if you are not allowing the user to reorder the items, use the index of the selected row. It will be much faster.
2) the rest of your process is correct. However, you forgot to rebind the datagrids to the new lists. The grids do not update their UI automatically.
Small pseudo example:
var item = List1[grid1.currentrow.index];
List1.Remove(item);
List2.Add(item);
Grid1.datasource=list1;
Grid2.datasource=list2;

How do I properly select a row in ObjectListView?

I'm using ObjectListView with C# and .Net 4.0. I wrote code that reloads the listview and then re-selects the last selected index.
The re-selection code is quite simple:
olvListView.SelectedIndex = i;
This appears to work, because the item is selected. However, if I then click the up or down arrow, the selection jumps up to the second row (no matter what row I selected), suggesting that the selection was actually set on the first row, no matter what was the value of i.
What am I doing wrong here?
The underlying ListView Control distinguishes between 'selection' and 'focus'.
olvListView.SelectedIndex = i; changes the selection but not the focus. But the focused row is the one that the keyboard input relates to.
Either change the focus the as well
olvListView.SelectedIndex = i;
olvListView.FocusedItem = olvListView.SelectedItems[0];
or call
olvListView.SelectObject(aModelObject);
The second solution would be the preferred way to select an item when working with OLV, however you say you "wrote code that reloads the listview", so the reference to the original item is probably different. Maybe you should just refresh the items that changed, instead of reloading everything. That way you could preserve the selection.
Example if your olv have datasource from "class_z.list" and foreach have only one result.
foreach(class_z a in class_z.list.Where(x=>x.id==id_value))
{
olv.SelectedObject = z;
}

DropDownList Showing First Item Empty

Every time this Dropdown is showing first Item Blank
ComboBox cb;
List<string> namesCollection=new List<string>();
namesCollection.Add("---- Select ----");
namesCollection.Add("ABC1");
namesCollection.Add("ABC2");
namesCollection.Add("ABC3");
namesCollection.Add("ABC4");
foreach(string pname in namesCollection)
cb.Items.Add(pname);
Does anyone have solution for this ?
You appear to be defining the ComboBox right there in your code, so I'll assume it's actually displayed somewhere in your form / window.
It's normal for the ComboBox to display a blank line initially.
Specify the item you want to display, immediately after populating the ComboBox with data:
cb.SelectedIndex = 0;
You probably have a line feed in your items collection. Hard to see. Adding a cb.Items.Clear() before populating is probably a good idea anyway and will get rid of the problem, if you can't locate it.

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

Problems with selected indices in listview

I have an arraylist which contain objects of my own class. I want to fetch the object from the array list which has the index = selectedindex of listview.
I tried this :
TrackInformation t=(TrackInformation) SongList[listView1.SelectedIndices[0]];
TrackInformation is my class and SongList is an ArrayList of type TrackInformation.
listview1 does not allow multiple indices selection so I want the first element of the SelectedIndices collection.
I am getting ArgumentOutOfRangeException and it says value of '0' is not valid for 'index'.
Put this line before your code -
if(listView1.SelectedIndices.Count > 0)
{
TrackInformation t=(TrackInformation) SongList[listView1.SelectedIndices[0]];
}
The ListView.SelectedIndexChanged event has a quirk that bombs your code. When you start your program, no item is selected. Click an item and SelectedIndexChanged fires, no problem. Now click another item and the event fires twice. First to let you know, unhelpfully, that the first item is unselected. Then again to tell you that the new item is selected. That first event is going to make you index an empty array, kaboom. RV1987's snippet prevents this.
The error is because listView1.SelectedIndices is empty, do you have a row selected?
you probable want to wrap in a test
ListView.SelectedIndexCollection selected=listView1.SelectedIndicies;
if (selected.Count==0) {
// code for no items selected
} else {
TrackInformation t=(TrackInformation) SongList[selected[0]];
// rest of code to deal with t
}

Categories

Resources