如何将2个listview对象一起滚动? [英] How do I scroll 2 listview objects together?

查看:95
本文介绍了如何将2个listview对象一起滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



ich创建一个包含2个Listviews的Windows窗体应用程序。我想一起滚动2个列表视图(垂直和水平)?我该怎么做?任何人有想法或示例代码?



谢谢!

Hello,

ich create an windows forms application with 2 Listviews. I want to scroll 2 listviews together (vertical and horizontal)? How can i do it? Anyone has an idea or example code?

Thankyou!

推荐答案



请尝试为此开发一些自定义控件



以下是我尝试过的方案示例代码,但可能有错误尝试纠正它。



我为listview开发了一个自定义控件类,将其命名为CustListView,并在该类中添加了scroll事件并在windows窗体中使用它。

Hi,
Please try to develop some custom controls for this

The below is the sample code I have tried with you scenario , but there may be bugs in it try to rectify it.

I developed a custom control class for listview named it as CustListView and added scroll event in that class and used it in windows forms.
using System.Windows.Forms;

namespace ListViewSharedScroll
{
    class CustListView : ListView 
    {
        public event ScrollEventHandler Scroll;
        private const int WM_HSCROLL = 0x114;
        private const int WM_VSCROLL = 0x115;
        protected virtual void OnScroll(ScrollEventArgs e)
        {
            ScrollEventHandler handler = this.Scroll;
            if (handler != null) handler(this, e);
        }
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_VSCROLL)
            { // Trap WM_VSCROLL
                OnScroll(new ScrollEventArgs((ScrollEventType)(m.WParam.ToInt32() & 0xffff), -1, 0, ScrollOrientation.VerticalScroll));
            }
            else if (m.Msg == WM_HSCROLL)
            {
                OnScroll(new ScrollEventArgs((ScrollEventType)(m.WParam.ToInt32() & 0xffff),-1,0,ScrollOrientation.HorizontalScroll));
            }
        }

       
       
    }
}



之后我在我的windows窗体代码中使用了这个自定义类


After that I used this custom class inside my windows forms code

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ListViewSharedScroll
{
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

        int TopItemIndex = 0;
        CustListView ListView1 = new CustListView();
        CustListView ListView2 = new CustListView();
        private void Form1_Load(object sender, EventArgs e)
        {
            

            ListView1.Size = new Size(201, 146);
            ListView1.Margin = new Padding(3, 3, 3, 3);
            ListView1.Location = new Point(48, 85);
            ListView1.Scrollable = true;
            ListView1.View = View.Details;
            
            

            ListView2.Size = new Size(201, 146);
            ListView2.Margin = new Padding(3, 3, 3, 3);
            ListView2.Location = new Point(280, 85);
            ListView2.Scrollable = true;
            ListView2.View = View.Details;

            ListView1.Columns.Add("Header", 100);
            ListView1.Columns.Add("Details", 100);

            ListView2.Columns.Add("Header", 100);
            ListView2.Columns.Add("Details", 100);


            for (int i = 0; i < 50; i++)
            {
            
                ListView1.Items.Add(new ListViewItem(new string[] { "Alpha"+i.ToString(), "Some details"+i.ToString() }));
                ListView1.Items.Add(new ListViewItem(new string[] { "Bravo" + i.ToString(), "More details" + i.ToString() }));

                ListView2.Items.Add(new ListViewItem(new string[] { "Alpha" + i.ToString(), "Some details" + i.ToString() }));
                ListView2.Items.Add(new ListViewItem(new string[] { "Bravo" + i.ToString(), "More details" + i.ToString() }));
            }

            

            ListView1.Scroll += ListView1_Scroll;
            ListView2.Scroll += ListView1_Scroll;

            this.Controls.Add(ListView1);
            this.Controls.Add(ListView2);


        }

        private void ListView1_Scroll(object sender, ScrollEventArgs e)
        {

            if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
            {
                if (TopItemIndex != ListView1.TopItem.Index || TopItemIndex !=  ListView2.TopItem.Index)
                {
                    if (TopItemIndex != ListView1.TopItem.Index)
                    {
                        ListView1.EnsureVisible(ListView1.TopItem.Index);
                        ListView2.EnsureVisible(ListView1.TopItem.Index);
                        TopItemIndex = ListView1.TopItem.Index;
                    }
                    else
                    {
                        ListView1.EnsureVisible(ListView2.TopItem.Index);
                        ListView2.EnsureVisible(ListView2.TopItem.Index);
                        TopItemIndex = ListView2.TopItem.Index;
                    }

                    
                }
            }

            if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
            {
                ScrollH(1);          
            }
            
        }

        private void ScrollH(int pixelsToScroll)
        {
            const Int32 LVM_FIRST = 0x1000;
            const Int32 LVM_SCROLL = LVM_FIRST + 20;
            SendMessage(ListView1.Handle, LVM_SCROLL, (IntPtr)pixelsToScroll, IntPtr.Zero);
            SendMessage(ListView2.Handle, LVM_SCROLL, (IntPtr)pixelsToScroll, IntPtr.Zero);
        }


       
    }
}



我希望这将是一个很好的参考让你接近你的解决方案。



参考文章:

http://stackoverflow.com/questions/1851620/handling-scroll-event-on-listview-in-c-sharp [ ^ ]

http://stackoverflow.com/questions / 626315 / winforms-listview-remembering-scrolled-location-on-reload [ ^ ]

http://stackoverflow.com/questions/473148/c-sharp-listview-how-do-i-add-items-to-columns -2-3-and-4-etc [ ^ ]

http://stackoverflow.com/questions/7146567/winforms-listview-not-showing-items-in-detailsview [ ^ ]

http://stackoverflow.com / questions / 372034 / how-do-i-listen-for-scrolling-in-a listview [ ^ ]

http://stackoverflow.com/questions/372034/how -do-i-listen-for-rolling-in-a listview [ ^ ]

http://bytes.com/topic/c-sharp/answers/255418-scrolling-listview [ ^ ]



谢谢,

所有最佳


I hope this will be a good reference for you to approach your solution.

Referenced articles :
http://stackoverflow.com/questions/1851620/handling-scroll-event-on-listview-in-c-sharp[^]
http://stackoverflow.com/questions/626315/winforms-listview-remembering-scrolled-location-on-reload[^]
http://stackoverflow.com/questions/473148/c-sharp-listview-how-do-i-add-items-to-columns-2-3-and-4-etc[^]
http://stackoverflow.com/questions/7146567/winforms-listview-not-showing-items-in-detailsview[^]
http://stackoverflow.com/questions/372034/how-do-i-listen-for-scrolling-in-a-listview[^]
http://stackoverflow.com/questions/372034/how-do-i-listen-for-scrolling-in-a-listview[^]
http://bytes.com/topic/c-sharp/answers/255418-scrolling-listview[^]

Thanks,
All the Best


这篇关于如何将2个listview对象一起滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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