基本上,我在Form(Windows窗体应用程序)上有两个列表框syncListView1和syncListView2,我试图使其滚动条同步
我已经用谷歌搜索了一些,我发现了这个类,但是它似乎不起作用:
class SyncListBox : System.Windows.Forms.ListBox
{
[Category("Action")]
private const int WM_HSCROLL = 0x114;
private const int WM_VSCROLL = 0x115;
public event ScrollEventHandler OnHorizontalScroll;
public event ScrollEventHandler OnVerticalScroll;
private const int SB_LINEUP = 0;
private const int SB_LINELEFT = 0;
private const int SB_LINEDOWN = 1;
private const int SB_LINERIGHT = 1;
private const int SB_PAGEUP = 2;
private const int SB_PAGELEFT = 2;
private const int SB_PAGEDOWN = 3;
private const int SB_PAGERIGHT = 3;
private const int SB_THUMBPOSITION = 4;
private const int SB_THUMBTRACK = 5;
private const int SB_PAGETOP = 6;
private const int SB_LEFT = 6;
private const int SB_PAGEBOTTOM = 7;
private const int SB_RIGHT = 7;
private const int SB_ENDSCROLL = 8;
private const int SIF_TRACKPOS = 0x10;
private const int SIF_RANGE = 0x1;
private const int SIF_POS = 0x4;
private const int SIF_PAGE = 0x2;
private const int SIF_ALL = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetScrollInfo(
IntPtr hWnd, int n, ref ScrollInfoStruct lpScrollInfo);
private struct ScrollInfoStruct
{
public int cbSize;
public int fMask;
public int nMin;
public int nMax;
public int nPage;
public int nPos;
public int nTrackPos;
}
protected override void WndProc(ref System.Windows.Forms.Message msg)
{
if (msg.Msg == WM_HSCROLL)
{
if (OnHorizontalScroll != null)
{
ScrollInfoStruct si = new ScrollInfoStruct();
si.fMask = SIF_ALL;
si.cbSize = Marshal.SizeOf(si);
GetScrollInfo(msg.HWnd, 0, ref si);
if (msg.WParam.ToInt32() == SB_ENDSCROLL)
{
ScrollEventArgs sargs = new ScrollEventArgs(
ScrollEventType.EndScroll,
si.nPos);
OnHorizontalScroll(this, sargs);
}
}
}
if (msg.Msg == WM_VSCROLL)
{
if (OnVerticalScroll != null)
{
ScrollInfoStruct si = new ScrollInfoStruct();
si.fMask = SIF_ALL;
si.cbSize = Marshal.SizeOf(si);
GetScrollInfo(msg.HWnd, 0, ref si);
if (msg.WParam.ToInt32() == SB_ENDSCROLL)
{
ScrollEventArgs sargs = new ScrollEventArgs(
ScrollEventType.EndScroll,
si.nPos);
OnVerticalScroll(this, sargs);
}
}
}
base.WndProc(ref msg);
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// scrolled
//
this.Size = new System.Drawing.Size(120, 95);
this.ResumeLayout(false);
}
}
并在后面的代码中:
private void syncListView2_OnVerticalScroll(object sender, ScrollEventArgs e)
{
syncListView1.TopIndex = syncListView2.TopIndex;
}
private void syncListView1_OnVerticalScroll(object sender, ScrollEventArgs e)
{
syncListView2.TopIndex = syncListView1.TopIndex;
}
从文章复制/粘贴代码,甚至不花时间阅读有关代码和用户评论的所有内容,这确实是一种不好的做法。您不是第一个被困在这个地方的人。...因为一个用户已经评论了该问题
因此,您可能错过的是订阅事件。有两个选项可以做到这一点:
syncListView1_OnVerticalScroll
。使用第二个列表框执行相同的操作,但选择syncListView1_OnVerticalScrol2
在具有列表框的窗体中,打开代码视图。应该在InitializeComponent()
调用方法的地方有构造函数。在该方法之后,添加以下代码行。
syncListView1.OnVerticalScroll + = this.syncListView1_OnVerticalScroll; syncListView2.OnVerticalScroll + = this.syncListView2_OnVerticalScroll;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句