addhandler和在运行时创建按钮 [英] addhandler and creating buttons at run time

查看:59
本文介绍了addhandler和在运行时创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时创建用户可选择的按钮数量,需要

响应他们的点击事件。我的代码:


Dim btnButton as Button(全局)


然后在子程序中:

对于intNumBtnsDown = 0到intNumBtns - 1


pointLocate.Y + = 70


pointLocate.X = 0


for intNumBtnsAcross = 0 to intNumBtns - 1


pointLocate.X + = 70

btnButton = New Button


使用btnButton


..Name =" btn" &安培; CStr(intNumBtnsDown)& CStr(intNumBtnsAcross)


....


结束


pnlBtns.Controls。添加(btnButton)


AddHandler btnButton.Click,AddressOf GameBtns


问题是GameBtns认为创建的每个按钮都具有相同的名称

这是创建的最后一个按钮的名称。老师说需要用个别名字来暗淡

按钮,如" btn &安培; CStr(intNumBtnsDown)&

CStr(intNumBtnsAcross)",然后用相同的替换addhandler事件。

他不记得怎么做,是忙着他挑战我想出来吧b $ b。他说我可以使用互联网,当我问他时,他说

我可以使用ngs。请帮助。

I am creating a user selectable quantity of buttons at run time and need to
respond to their click events. My code:

Dim btnButton as Button (global)

Then in a sub:
For intNumBtnsDown = 0 To intNumBtns - 1

pointLocate.Y += 70

pointLocate.X = 0

For intNumBtnsAcross = 0 To intNumBtns - 1

pointLocate.X += 70

btnButton = New Button

With btnButton

..Name = "btn" & CStr(intNumBtnsDown) & CStr(intNumBtnsAcross)

....

End With

pnlBtns.Controls.Add(btnButton)

AddHandler btnButton.Click, AddressOf GameBtns

Problem is that GameBtns thinks that each button created has the same name
which is the name of the last button created. Teacher says need to Dim the
button with the individual names, as ""btn" & CStr(intNumBtnsDown) &
CStr(intNumBtnsAcross)"", then replace the addhandler event with the same.
He does not remember how to do this, is busy and he challenged me to figure
it out. He said that I could use the internet and when I asked, he said that
I could use ngs. Help please.

推荐答案

嗨保罗,


你写的很好,你是学生,比我们好可以采取学生

的方法,这是有帮助的,而不是给代码。


你可以添加到每个控件相同的处理程序。因此,当您创建它时,您可以将
始终将相同的事件代码添加到控件中。重要的是

当您为具有正确签名的事件添加处理程序时,

该事件。


什么我有时做(当我有很多相同的控件时)只是从IDE创建一个事件,最后删除事件处理程序,给事件

另一个名称,然后将其添加到我需要该事件的所有控件中。


如果我们可以通过使用发件人对象看到它扔了它。

为此,我给你一些代码,因为你必须告诉它是什么样的控制

(或者它是一个控件)。


这可以是


如果是直播(发送者,控制).name = blablabla



如果是直播(发送者,复选框).checked = true


我希望这会给你一些想法吗?


Cor
Hi Paul,

Nice that you wrote you are a student, than we can take the students
approach and that is helping and not give code.

You can add to every control the same handler. So when you create it, you
can add all the time the same event code to the control. Important is that
when you add an handler for an event that is has the right signature from
that event.

What I sometimes do (when I have a lot of the same controls) is just create
one event from the IDE, delete the event handler at the end, give the event
another name and than add that to all my controls which needed that event.

In the event we can than see which did throw it by using the sender object.
For that I give you some code, because you have to tell what kind of control
it was (or that it was a control).

This can be something as

If directcast(sender,control).name = blablabla
or
If directcast(sender,checkbox).checked = true

I hope that this gives you some ideas?

Cor


Cor,谢谢你的帮助。


在我的程序中按钮的数量cre ated不是常数,所以你的b $ b第3段不起作用,对吗?我仍然迷失在太空中。请注意,在我的

代码中,单击任何创建的按钮会触发Addhandler委托

GameBtns,但此委托认为创建的所有按钮都具有相同的名称。

也许这就是发件人对象进来的地方,但我还没有完全理解这一点。我确实查找了DirectCast关键字在帮助和我

不明白它的内容,对不起。我之前曾尝试查找如何在帮助中响应由运行时创建的对象触发的事件,但

一无所获。也许我没有使用正确的搜索词。


我尝试过如果DirectCast(发件人,按钮).Name = blablabla,但我该怎么用

的名字?


保罗


PS:我只是想了想,很多想法,但这个可能不会< br $> b $ b让我难堪。我可以像你在段落3中说的那样,然后使用代码将按钮名称添加到事件句柄中吗?也许这就是你的意思。

" Cor" < no*@non.com>在消息中写道

news:uk ************** @ TK2MSFTNGP10.phx.gbl ...
Cor, thanks for your help.

In my program the number of buttons created is not a constant, so your
paragraph 3 would not work, right? I am still lost in space. Note that in my
code clicking any button created does trigger the Addhandler delegate
"GameBtns", but this delegate thinks all buttons created have the same name.
Maybe this is where the Sender Object come in, but I have not gotten a full
understanding of this yet. I did look up "DirectCast keyword" in help and I
do not understand what it says, sorry. I previously tried looking up how to
respond to events triggered by objects created during run time in help, but
found nothing. Maybe I did not use the right search word(s).

I did try If DirectCast(Sender, Button).Name = blablabla, but what do I use
for the name?

Paul

PS: I just had a thought, well many thoughts, but this one might not
embarrass me. Can I do like u say in paragraph 3 and then use code to add
the button names to the event handle?? Maybe that is what u meant.
"Cor" <no*@non.com> wrote in message
news:uk**************@TK2MSFTNGP10.phx.gbl...
嗨Paul,
你可以添加到每个控件同样的处理因此,当您创建它时,您可以将相同的事件代码一直添加到控件中。重要的是
当您为具有正确签名的事件添加处理程序时

我有时会做什么(当我有很多相同的事情时)控件)只需
从IDE创建一个事件,最后删除事件处理程序,给
事件另一个名称,然后将其添加到需要该事件的所有控件中。
<事实上,我们可以通过使用发送者
对象看到它扔了它。为此,我给你一些代码,因为你必须告诉它是什么样的
控制(或者它是一个控件)。

这可以是
<如果是直播(发件人,控制).name = blablabla

如果是直播(发件人,复选框).checked = true

我希望这会给你一些想法?

Hi Paul,

Nice that you wrote you are a student, than we can take the students
approach and that is helping and not give code.

You can add to every control the same handler. So when you create it, you
can add all the time the same event code to the control. Important is that
when you add an handler for an event that is has the right signature from
that event.

What I sometimes do (when I have a lot of the same controls) is just create one event from the IDE, delete the event handler at the end, give the event another name and than add that to all my controls which needed that event.

In the event we can than see which did throw it by using the sender object. For that I give you some code, because you have to tell what kind of control it was (or that it was a control).

This can be something as

If directcast(sender,control).name = blablabla
or
If directcast(sender,checkbox).checked = true

I hope that this gives you some ideas?

Cor



Cor,谢谢你的帮助。


在我的程序中,创建的按钮数量不是常数,所以你的

第3段不起作用,对吗?我仍然迷失在太空中。请注意,在我的

代码中,单击任何创建的按钮会触发Addhandler委托

GameBtns,但此委托认为创建的所有按钮都具有相同的名称。

也许这就是发件人对象进来的地方,但我还没有完全理解这一点。我确实查找了DirectCast关键字在帮助和我

不明白它的内容,对不起。我之前曾尝试查找如何在帮助中响应由运行时创建的对象触发的事件,但

一无所获。也许我没有使用正确的搜索词。


我尝试过如果DirectCast(发件人,按钮).Name = blablabla,但我该怎么用

的名字?


保罗


PS:我只是想了想,很多想法,但这个可能不会< br $> b $ b让我难堪。我可以像你在段落3中说的那样,然后使用代码将按钮名称添加到事件句柄中吗?也许这就是你的意思。

" Cor" < no*@non.com>在消息中写道

news:uk ************** @ TK2MSFTNGP10.phx.gbl ...
Cor, thanks for your help.

In my program the number of buttons created is not a constant, so your
paragraph 3 would not work, right? I am still lost in space. Note that in my
code clicking any button created does trigger the Addhandler delegate
"GameBtns", but this delegate thinks all buttons created have the same name.
Maybe this is where the Sender Object come in, but I have not gotten a full
understanding of this yet. I did look up "DirectCast keyword" in help and I
do not understand what it says, sorry. I previously tried looking up how to
respond to events triggered by objects created during run time in help, but
found nothing. Maybe I did not use the right search word(s).

I did try If DirectCast(Sender, Button).Name = blablabla, but what do I use
for the name?

Paul

PS: I just had a thought, well many thoughts, but this one might not
embarrass me. Can I do like u say in paragraph 3 and then use code to add
the button names to the event handle?? Maybe that is what u meant.
"Cor" <no*@non.com> wrote in message
news:uk**************@TK2MSFTNGP10.phx.gbl...
嗨Paul,
你可以添加到每个控件同样的处理因此,当您创建它时,您可以将相同的事件代码一直添加到控件中。重要的是
当您为具有正确签名的事件添加处理程序时

我有时会做什么(当我有很多相同的事情时)控件)只需
从IDE创建一个事件,最后删除事件处理程序,给
事件另一个名称,然后将其添加到需要该事件的所有控件中。
<事实上,我们可以通过使用发送者
对象看到它扔了它。为此,我给你一些代码,因为你必须告诉它是什么样的
控制(或者它是一个控件)。

这可以是
<如果是直播(发件人,控制).name = blablabla

如果是直播(发件人,复选框).checked = true

我希望这会给你一些想法?

Hi Paul,

Nice that you wrote you are a student, than we can take the students
approach and that is helping and not give code.

You can add to every control the same handler. So when you create it, you
can add all the time the same event code to the control. Important is that
when you add an handler for an event that is has the right signature from
that event.

What I sometimes do (when I have a lot of the same controls) is just create one event from the IDE, delete the event handler at the end, give the event another name and than add that to all my controls which needed that event.

In the event we can than see which did throw it by using the sender object. For that I give you some code, because you have to tell what kind of control it was (or that it was a control).

This can be something as

If directcast(sender,control).name = blablabla
or
If directcast(sender,checkbox).checked = true

I hope that this gives you some ideas?

Cor



这篇关于addhandler和在运行时创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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