我想为一系列事件创建自定义事件参数。我正在使用第三方 X/Y 示波器,在其中绘制强度与频率。 该示波器能够在其上放置"标记",这些标记只是不同频率的小三角形。 这些标记支持诸如鼠标进入标记、执行单击以及鼠标离开标记时等事件。 因此,对于两个标记,这是代码:
private void createEvents()
{
this.scope2.MarkerGroups[0].Click += new EventHandler(Marker0_Click);
this.scope2.MarkerGroups[0].MouseEnter += new EventHandler(Marker0_Enter);
this.scope2.MarkerGroups[0].MouseLeave += new EventHandler(Marker0_Leave);
this.scope2.MarkerGroups[1].Click += new EventHandler(Marker1_Click);
this.scope2.MarkerGroups[1].MouseEnter += new EventHandler(Marker1_Enter);
this.scope2.MarkerGroups[1].MouseLeave += new EventHandler(Marker1_Leave);
}
// And now the event handlers
private void Marker0_Click(object sender, EventArgs e)
{
//do something;
}
private void Marker0_Enter(object sender, EventArgs e)
{
//do something
}
private void Marker0_Leave(object sender, EventArgs e)
{
// do something
}
private void Marker1_Click(object sender, EventArgs e)
{
//do something;
}
private void Marker1_Enter(object sender, EventArgs e)
{
//do something
}
private void Marker1_Leave(object sender, EventArgs e)
{
// do something
}
现在这对两个标记来说很好....但我需要其中的 80 个。 我可以把整个事情写出来,但必须有更好的方法。 所以我是这样开始的:
private void createMarkerEvents()
{
for (int i = 0; i < 80; i++)
{
this.scope2.MarkerGroups[i].Click += new EventHandler(Marker_Click);
this.scope2.MarkerGroups[i].MouseEnter += new EventHandler(Marker_Enter);
this.scope2.MarkerGroups[i].MouseLeave += new EventHandler(Marker_Leave);
}
}
private void Marker_Click(object sender, EventArgs e)
{
//do something;
}
private void Marker_Enter(object sender, EventArgs e)
{
//do something
}
private void Marker_Leave(object sender, EventArgs e)
{
// do something
}
所以问题是如何将实际的标记编号从事件传递到事件处理程序?一定有办法。谢谢,汤姆
如果要标识标记组,可以将object sender
强制转换为MarkerGroup
对象
private void AnyMarker_Click(object sender, EventArgs e)
{
MarkerGroup group = (MarkerGroup)sender;
int indexOfMarkerGroup = this.scope2.MarkerGroups.IndexOf(group);
//do something;
}
OFF:您应该定义一个自定义的 EventArgs 类:
public class MyEventArgs : EventArgs
{
public int MyCustomProperty {get;set;}
}
然后在您的活动中使用它:
public event EventHandler<MyEventArgs> ButtonPressed;
使用自定义参数的火灾事件:
if(ButtonPressed != null)
{
ButtonPressed(this, new MyEventArgs { MyCustomProperty = 1 });
}
编辑完整示例:
private void createMarkerEvents()
{
for (int i = 0; i < 80; i++)
{
this.scope2.MarkerGroups[i].Click += new EventHandler(Marker_Click);
this.scope2.MarkerGroups[i].MouseEnter += new EventHandler(Marker_Enter);
this.scope2.MarkerGroups[i].MouseLeave += new EventHandler(Marker_Leave);
}
}
private void Marker_Click(object sender, EventArgs e)
{
// When markergroup fires and event, it passes reference to itself as `sender` parameter
// so we can get access it
MarkerGroup mg = (MarkerGroup)sender; // this marker has fired a click event
// Now you know which marker has fired event
// if you want to determine it's index in MarkerGroups collection:
int index = this.scope2.MarkerGroup.IndexOf(mg);
// now you know MarkerGroup and it's index
}
private void Marker_Enter(object sender, EventArgs e)
{
//do something
}
private void Marker_Leave(object sender, EventArgs e)
{
// do something
}