滚动条点击事件方法运行两次c#
本文关键字:两次 运行 事件 方法 滚动条 | 更新日期: 2023-09-27 18:02:10
我有一个用户控件,它使用垂直滚动条来浏览它。每次单击滚动条的最小按钮时,它都会正确地执行所调用的方法,但在调用的方法返回后,它不会退出,而是第二次调用该方法。基本上滚动条事件触发两次,每次更新它的值。为什么?
这是我的代码的浓缩版本....
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();
}
在浏览了很多网页后,我发现在初始的SmallIncrement事件之后创建了一个EndScroll事件。我不明白为什么会发生这种事!我改变了vScrollbar事件处理程序如下:
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
if (e.Type == ScrollEventType.EndScroll) return;
ThreadTest((ushort)vScrollBar1.Value);
}