滚动条单击事件方法运行两次c#

时间:2016-08-13 00:03:42

标签: c# scrollbar

我有一个用户控件,它使用垂直滚动条来浏览它。每次我点击滚动条的最小按钮时,它都会正确执行被调用的方法,但不是退出,在被调用方法返回后,它会再次调用该方法。基本上滚动条事件每次都会触发两次更新其值。为什么呢?

以下是我的代码的精简版....

private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
       ThreadTest((ushort)vScrollBar1.Value);
    }

    private void ThreadTest(ushort value)
    {
        if (mC6800Screen1.InvokeRequired)
        {
            TestCallBack tcb = new TestCallBack(displayVirtualMemory);
            Invoke(tcb, new object[] {value});
        }
        else
        { 
            displayVirtualMemory(value);
        }
    }

    private void displayVirtualMemory(ushort value)
    {
        //This method simply displays the contents of an array [0 - 0xffff]
        //16 rows at a time.  
        for (ushort row = value; row < value + 400; row += 16)
        {
            //Compute the index offset (16 bits per row)
            string indexOffset = row.ToString("X4");
            var indexChars = indexOffset.ToCharArray();
            //Display memory index
            for (ushort arrayPosition = 0; arrayPosition < indexOffset.Length; arrayPosition++)
            {
                mC6800Screen1.screenMemory[screenPosition] = (byte)indexChars[arrayPosition];
                screenPosition += 2;
            }

            screenPosition += 4;
        }       
        //the data is copyied to a bitmap which is transfered to a user control
        //once all the processing is complete.
        mC6800Screen1.Invalidate();
        mC6800Screen1.Update();
    }

1 个答案:

答案 0 :(得分:1)

经过大量的网页浏览后,我发现在初始的SmallIncrement事件之后正在创建一个EndScroll事件。为什么会发生这种情况超出了我的灰质!我更改了vScrollbar事件处理程序,如下所示:

private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
        if (e.Type == ScrollEventType.EndScroll) return;
        ThreadTest((ushort)vScrollBar1.Value);
    }