OnClick处理程序 [英] OnClick Handler

查看:59
本文介绍了OnClick处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#项目中,我有一个PictureBoxes数组。

我为每个图片框组成一个事件处理程序,因为它是使用i作为

索引创建的。 />
PictureBox1 [i] .Click + = new System.EventHandler(ClickHandler);


在事件处理程序中,我想确定单击框的索引。

下面的代码不返回索引。


public void ClickHandler(Object sender,System.EventArgs e)

{

textBox1.Text = sender.ToString();

}


任何帮助都将不胜感激。

谢谢,

Jim

In a C# project I have an array of PictureBoxes.
I form an event handler for each picturebox as it is created using i as an
index.
PictureBox1[i].Click += new System.EventHandler(ClickHandler);

In the event handler I would like to determine the index of the clicked box.
The code below does not return the index.

public void ClickHandler(Object sender, System.EventArgs e)
{
textBox1.Text = sender.ToString();
}

Any help would be appreciated.
Thanks,
Jim

推荐答案

2008年5月5日星期一20:45:00 -0700,Jim < Ji*@discussions.microsoft.com

写道:
On Mon, 05 May 2008 20:45:00 -0700, Jim <Ji*@discussions.microsoft.com
wrote:

在C#项目中,我有一个PictureBoxes数组。

我为每个图片框组成一个事件处理程序,因为它是使用我作为



索引创建的。

Pictur eBox1 [i] .Click + = new System.EventHandler(ClickHandler);


在事件处理程序中我想确定点击的索引

框。
In a C# project I have an array of PictureBoxes.
I form an event handler for each picturebox as it is created using i as
an
index.
PictureBox1[i].Click += new System.EventHandler(ClickHandler);

In the event handler I would like to determine the index of the clicked
box.



我认为至少有两种方法是合理的。


第一种方法是使用Tag属性PictureBox控件。你

可以将控件的索引分配给Tag属性,然后读取它

稍后在处理程序中退出:


PictureBox1 [i]。点击+ = ClickHandler;

PictureBox1 [i] .Tag = i;


和......


public void ClickHandler(对象发送者,EventArgs e)

{

控制ctl =(控制)发件人;

int i =(int)ctl.Tag;

}


或者,您可以使用匿名方法处理

捕获索引,并将其传递给实际完成工作的方法:


for(int i = 0; i< PictureBox1.Length; i ++)

{

//注意你需要一个局部变量

//是每次迭代数组的新范围,

//这样每个匿名方法都会获得一个新的捕获的

//变量。否则,它们都将使用

//相同的变量,因此相同的值。


int iCaptured = i;


PictureBox1 [i] .Click + = delegate(object sender,EventArgs e)

{ClickHandler(sender,iCaptured); };

}


和...


public void ClickHandler(object sender,int i)

{

//用我做的东西
}


两者都应该可以正常工作。使用标签可能更简单,更简单,但是如果你已经使用了标签,那么匿名

方法应该是一个不错的选择。 br />

Pete

There are at least two approaches I can think of that would be reasonable.

The first involves using the Tag property of the PictureBox control. You
can assign the index of the control to the Tag property, and then read it
back out later in the handler:

PictureBox1[i].Click += ClickHandler;
PictureBox1[i].Tag = i;

and...

public void ClickHandler(object sender, EventArgs e)
{
Control ctl = (Control)sender;
int i = (int)ctl.Tag;
}

Alternatively, you could use an anonymous method for the handler that
captures the index, and pass that to a method that actually does the work:

for (int i = 0; i < PictureBox1.Length; i++)
{
// Note that you need a local variable that
// is newly scoped for each iteration of the array,
// so that each anonymous method gets a new captured
// variable. Otherwise, they will all wind up using
// the same variable and thus the same value.

int iCaptured = i;

PictureBox1[i].Click += delegate(object sender, EventArgs e)
{ ClickHandler(sender, iCaptured); };
}

and...

public void ClickHandler(object sender, int i)
{
// do stuff with i
}

Either should work fine. Using the Tag is probably simpler and easier,
but if you''re already using the Tag for something else, the anonymous
method approach should be a good alternative.

Pete


" Peter Duniho" < Np ********* @nnowslpianmk.com在留言中写道

news:op *************** @ petes-computer.local ...
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...

我能想到至少有两种方法是合理的。
There are at least two approaches I can think of that would be reasonable.



你可以只搜索数组。

You could just search the array.


第一个涉及使用PictureBox控件的Tag属性。你
The first involves using the Tag property of the PictureBox control. You



这种事情通常最好避免,因为你现在有一个数据存储在2个位置可以报告不同结果的
。看到我们只是在谈论用户点击图片框然后搜索

数组将是最简单和快速的。如果数组以任何方式修改

那么标签不需要更新。


Michael

This sort of thing is generally best avoided as you now have a piece of data
stored in 2 locations that could report different results. Seeing that we''re
just talking about the user clicking on a picturebox then searching the
array would be simplest and fast enough. If the array is modified in any way
then tags don''t need to be updated.

Michael


2008年5月5日星期一21:37:33 -0700,MichaelC< mi ** @ nospams.comwrote:
On Mon, 05 May 2008 21:37:33 -0700, MichaelC <mi**@nospams.comwrote:

" Peter Duniho" ; < Np ********* @nnowslpianmk.com在留言中写道

news:op *************** @ petes-computer.local ...
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...

>至少有两种我能想到的方法是合理的。
>There are at least two approaches I can think of that would be
reasonable.



您可以搜索数组。


You could just search the array.



你可以。我不同意它是最干净,最优雅的方法。

You could. I disagree that it''s the cleanest, most elegant approach.


>第一个涉及使用PictureBox的Tag属性控制。

>The first involves using the Tag property of the PictureBox control.
You



这种事情通常最好避免,因为你现在有一块

数据
存储在可报告不同结果的2个位置。


This sort of thing is generally best avoided as you now have a piece of
data
stored in 2 locations that could report different results.



首先,一条数据不是存储的在两个地方。可以说它是两个地方代表的
,但是a)它真的只存储在一个地方,

和b)没有根本错误这样做。


毕竟,如果您的异议是合法的,那么Forms命名空间中的整个Control hiearachy

实现是有缺陷的。它基本上与

相同,任何数据结构都保持对引用项目的集合元素的反向引用。


First, the "piece of data" isn''t "stored" in two locations. Arguably it''s
represented in two places, but a) it''s really only stored in one place,
and b) there''s nothing fundamentally wrong with doing it that way.

After all, if your objection was legitimate, the whole Control hiearachy
implementation in the Forms namespace is flawed. It does basically the
same thing, as does any data structure that maintains a back-reference to
an element of a collection that refers to an item.


看到我们只是在谈论用户点击图片框然后搜索

数组将是最简单的并且足够快。如果数组以任何

方式修改

那么标签不需要更新。
Seeing that we''re
just talking about the user clicking on a picturebox then searching the
array would be simplest and fast enough. If the array is modified in any
way
then tags don''t need to be updated.



谁说阵列会以任何方式被修改?


我不同意维护标签属性如果包含

集合的更改更复杂。但是,这并不是一个明确的要求,即使是这样,重申

数组并重置所有Tag属性也不是问题。如果数组发生变化只要

集合本身变化的频率低于

集合中的对象被点击(看起来很可能),理论上你会更好。

在它发生变化时迭代该集合,而不是每次点击鼠标时发生的每个单独的时间。


当然,只要阵列很小(并且它应该是),无论如何都不是重要的成本。但我强烈反对你的批评,即

使用Tag属性是一种不受欢迎的方法。


Pete

Who said the array would ever be modified in any way?

I don''t disagree that maintaining the Tag property if the containing
collection is changed is more complicated. However, that wasn''t ever a
stated requirement and even if it was, it''s not a problem to reiterate the
array and reset all the Tag properties if the array changes. As long as
the collection itself changes less frequently than an object in the
collection is clicked (which seems likely) you''re theoretically better off
iterating the collection just when it changes, rather than every single
time a mouse click happens.

Granted, as long as the array is small (and it ought to be), it''s not a
significant cost either way. But I strongly dispute your criticism that
the use of the Tag property is somehow an undesirable approach.

Pete


这篇关于OnClick处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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