如何仅添加 10 个项目

发布于 2024-11-19 00:05:26 字数 853 浏览 5 评论 0原文

我已经取消了经理。 我只需要在列表视图中查看 10 个条目。 脑子已经沸腾了怎么办呢。

此代码将所有记录添加到视图列表中,但我只需要最后 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());

重要的是 - 未使用 linq

更新:

示例:

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

我需要 obly 10。如果 start undo4 then您需要显示最后或最多 10 条中的所有内容

I have undoredomanager.
And I need to view in listview only 10 entries.
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛上残留的泪 2024-11-26 00:05:26

如果我理解你的问题,你最多需要最后 10 个撤消和最后 10 个重做命令。获得撤消命令列表后,使用它获取最后 10 个命令:

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

并对重做命令执行相同的操作。在我看来,这不是最好的解决方案——实际上更像是一个拼凑,但它应该让你朝着正确的方向前进。

更好的解决方案是修改/增强 UndoRedoManager 类,以便 ListView 可以调用方法来获取最后 n 个撤消/重做命令的列表。类似于:

public List<Object> GetUndoCommands(int numberOfCommands);

然后 ListView 可以简单地调用该方法:

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

以及类似的重做命令。它从 UI 层中删除了一堆代码,并为您提供了在以后轻松切换最大项目数的灵活性,并且通常(再次在我看来)似乎是更好的方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文