开发者

How to add just 10 items

I have undoredomanager. And I need to view in listview only 10 entri开发者_StackOverflow中文版es. already seething brain how to do it.

This code is added to the viewlist all records, but I only need the last 10.

lvUndoStack.Items.Clear();
var list = new List<object>();
foreach (var command in UndoRedoManager.UndoCommands)
{
    list.Insert(0, command.ToString());
}
lvUndoStack.Items.AddRange(list.ToArray());
lvUndoStack.SelectedIndex = lvUndoStack.Items.Count - 1;
indexSeletedItemUndoStack = lvUndoStack.SelectedIndex;

list = new List<object>();
foreach (var command in UndoRedoManager.RedoCommands)
{
    list.Insert(0, command.ToString());
}
lvUndoStack.Items.AddRange(list.ToArray());

importantly - not used linq

Update:

example:

undo1
undo2
undo3
undo4
undo5
undo6
undo7
redo1
redo2
redo3
redo4
redo5

I need obly 10. if start undo4 then you need to show everything in the last or a maximum of 10


If I understand your question, you want (at most) the last 10 undo and the last 10 redo commands. After you get the List of undo commands, use this to get up to the last 10:

if (list.Count <= 10)
{
    lvUndoStack.Items.AddRange(list.ToArray());
}
else
{
    for (int i = 0; i < 10; i++)
    {
        lvUndoStack.Items.Add(list[0]);
    }
}

And do the same for the redo commands. That's not the best solution, IMO - really more of a kludge, but it should get you going in the right direction.

An even better solution would be to modify/enchance the UndoRedoManager class so that the ListView could call a method to get a list of the last n undo/redo commands. Something like:

public List<Object> GetUndoCommands(int numberOfCommands);

Then the ListView could simply call that method:

lvUndoStack.Items.AddRange(UndoRedoManager.GetUndoCommands(10).ToArray());

And something similar for the redo commands. It removes a bunch of code from your UI layer, and gives you the flexibility to easily switch the max number of items at a later date and just generally (again IMO) seems to be a better way to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜