在c#中自动从上到下滚动列表视图 [英] Automatically scroll the listview from top to bottom in c#

查看:116
本文介绍了在c#中自动从上到下滚动列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们.

我当前正在创建某种广告应用程序.这是简单的文本从上到下滚动,并将再次重复.我目前正在使用listview在c#中执行此操作.列表视图中显示的文本将来自数据库.我正在使用计时器自动刷新表单.问题是,每次加载表单时如何自动将其移动或从上到下自动滚动,并且每次完成时都会再次重复.

I am currently creating a some kind of advertisement app. It is simple text scrolling from top to bottom and will repeat again. I am currently using listview to do this in c#. The texts that will show in the listview will be coming from a database. I am using the timer to automatically refresh the form. The problem is how can I move it or scroll it from top to bottom automatically every time the form loads and will repeat again every time it finished.

谢谢.

这是King King先生要求的代码.

This is the code so far as requested by Mr. King King.

 private void timer1_Tick(object sender, EventArgs e)
    {
        this.Refresh();
        listView1.TopItem = listView1.Items.Cast<ListViewItem>().LastOrDefault();
    }

推荐答案

您可以将 ListView.TopItem 设置为最后一项,并且应确保滚动条位于底部:

You can set the ListView.TopItem to the last item and it should ensure the scrollbar to be at the bottom:

listView1.TopItem = listView1.Items[listView1.Items.Count-1];

您应该确保您的 listView1 至少包含一项,这是更安全的LINQ版本:

You should be sure your listView1 has at least 1 item, here is a LINQ version which is safer:

listView1.TopItem = listView1.Items.Cast<ListViewItem>().LastOrDefault();

更新

如果要按项目上下滚动,请尝试以下操作:

UPDATE

If you want to scroll from top to bottom by each item, try this:

int lastIndex;
private void timer1_Tick(object sender, EventArgs e)
{
    this.Refresh();
    int i = listView1.TopItem == null ? -1 : listView1.TopItem.Index;        
    if(i>-1) {
      if(i == lastIndex || i == listView1.Items.Count - 2) i = 0;
      lastIndex = i;
      listView1.TopItem = listView1.Items[++i];
    }
}

这篇关于在c#中自动从上到下滚动列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆