473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

addhandler and creating buttons at run time

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.
Nov 20 '05 #1
15 3992
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
Nov 20 '05 #2
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...
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

Nov 20 '05 #3
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...
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

Nov 20 '05 #4
The DirectCast statement simply converts the "sender" variable (which is
declared as an Object) to a Button. Essentially, if you click on Button1 you
get Button1 passed to the Click event handler as an object. Cast it back to
a Button and you can do anything you want with it including accessing it's
properties such as name, text, position, etc....

See this discussion for some more information. It's a very similar
problem... at least the part about adding multiple buttons...

http://www.devcity.net/forums/topic.asp?tid=4087&page=2
"Paul Mars" <pa************@netzero.com> wrote in message
news:%2****************@TK2MSFTNGP09.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...
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


Nov 20 '05 #5
The DirectCast statement simply converts the "sender" variable (which is
declared as an Object) to a Button. Essentially, if you click on Button1 you
get Button1 passed to the Click event handler as an object. Cast it back to
a Button and you can do anything you want with it including accessing it's
properties such as name, text, position, etc....

See this discussion for some more information. It's a very similar
problem... at least the part about adding multiple buttons...

http://www.devcity.net/forums/topic.asp?tid=4087&page=2
"Paul Mars" <pa************@netzero.com> wrote in message
news:%2****************@TK2MSFTNGP09.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...
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


Nov 20 '05 #6
Thank you Cor and Scott. Between u both I finally got it. Thanks again.

Paul

BTW, I thought I salved it by doing Dim btnButton(63) as Button, then

Dim intCount As Integer

btnButton(intCount) = New Button

AddHandler btnButton(intCount).Click, AddressOf GameBtns

I still do not understand why this does not work.

tx again.
Nov 20 '05 #7
Thank you Cor and Scott. Between u both I finally got it. Thanks again.

Paul

BTW, I thought I salved it by doing Dim btnButton(63) as Button, then

Dim intCount As Integer

btnButton(intCount) = New Button

AddHandler btnButton(intCount).Click, AddressOf GameBtns

I still do not understand why this does not work.

tx again.
Nov 20 '05 #8
Cor
Hi Paul,

Greath that you got it something further.

From this I cannot see it why the rest is not working, paste the code from
your button event in.

As far as I can see should this part should be right.

Cor
Nov 20 '05 #9
Cor
Hi Paul,

Greath that you got it something further.

From this I cannot see it why the rest is not working, paste the code from
your button event in.

As far as I can see should this part should be right.

Cor
Nov 20 '05 #10
Cor, u mis-understand me. Based on what u told me, and then on Scott's
further explanation, it works. I have moved on.

Before I got it to work, I tried the Dim btnButton(63) as Button and that
did not work. I think it should, so I was still pondering why it did not
work. My teacher said something about creating each button with a different
name. I suppose that this is not important, just that since I thought of it
all by myself, it was kinda a ego buster when it did not work.

So, if u can comment on why my solution did not work, then I am interested
in learning why. Otherwise thanks for the solution that DID work.

I am now working on the next sub in my project.

Paul

"Cor" <no*@non.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Paul,

Greath that you got it something further.

From this I cannot see it why the rest is not working, paste the code from
your button event in.

As far as I can see should this part should be right.

Cor

Nov 20 '05 #11
Cor, u mis-understand me. Based on what u told me, and then on Scott's
further explanation, it works. I have moved on.

Before I got it to work, I tried the Dim btnButton(63) as Button and that
did not work. I think it should, so I was still pondering why it did not
work. My teacher said something about creating each button with a different
name. I suppose that this is not important, just that since I thought of it
all by myself, it was kinda a ego buster when it did not work.

So, if u can comment on why my solution did not work, then I am interested
in learning why. Otherwise thanks for the solution that DID work.

I am now working on the next sub in my project.

Paul

"Cor" <no*@non.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Paul,

Greath that you got it something further.

From this I cannot see it why the rest is not working, paste the code from
your button event in.

As far as I can see should this part should be right.

Cor

Nov 20 '05 #12
Cor
Hi Paul,

When you say dim mybutton(63) as button
you only declare an array as a placeholder for buttons.

That you have to fill with buttons.

dim mybutton(0) as new button
now there is real a button on the first adres of that array.

I once made a sample, now that you know how to do it, it can only give some
extra on your solution so I am not afraid to show it you.

I think there is all in as Scott and I told you.

(It only needs a project with a form and than you can paste it in)

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim start As Integer = 4
Dim top As Integer = 25
Dim i As Integer
Dim nowdate As DateTime = DateTime.Now
Dim mybutton(System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month)) As Button

' I use here the days in the month to create that placeholder for the
buttons

For i = 0 To System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month) - 1

'for every day in the month I create a button in that array

mybutton(i) = New Button
mybutton(i).TextAlign = ContentAlignment.MiddleCenter
mybutton(i).Width = 40
mybutton(i).Height = 20
mybutton(i).FlatStyle = FlatStyle.Flat
mybutton(i).BackColor = Drawing.Color.AntiqueWhite
mybutton(i).Location = New System.Drawing.Point(start, top)
mybutton(i).Text = (i + 1).ToString
mybutton(i).Cursor = Cursors.Hand
Me.Controls.Add(mybutton(i))
AddHandler mybutton(i).Click, AddressOf mybutton_Click
AddHandler mybutton(i).MouseHover, AddressOf mybutton_Hoover
AddHandler mybutton(i).MouseLeave, AddressOf mybutton_Leave

'here you see those handlers added

start = start + 40
If (i + 1) Mod 5 = 0 Then
top = top + 20
start = 4
End If
Next
End Sub

Private Sub mybutton_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)

Dim thisbutton As Button = DirectCast(sender, Button)
MessageBox.Show("The day is: " & thisbutton.Text)
End Sub
Private Sub mybutton_Hoover _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AliceBlue
End Sub
Private Sub mybutton_Leave _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AntiqueWhite
End Sub

I would say, try it, you can only learn from it I think

Cor

Nov 20 '05 #13
Cor
Hi Paul,

When you say dim mybutton(63) as button
you only declare an array as a placeholder for buttons.

That you have to fill with buttons.

dim mybutton(0) as new button
now there is real a button on the first adres of that array.

I once made a sample, now that you know how to do it, it can only give some
extra on your solution so I am not afraid to show it you.

I think there is all in as Scott and I told you.

(It only needs a project with a form and than you can paste it in)

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim start As Integer = 4
Dim top As Integer = 25
Dim i As Integer
Dim nowdate As DateTime = DateTime.Now
Dim mybutton(System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month)) As Button

' I use here the days in the month to create that placeholder for the
buttons

For i = 0 To System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month) - 1

'for every day in the month I create a button in that array

mybutton(i) = New Button
mybutton(i).TextAlign = ContentAlignment.MiddleCenter
mybutton(i).Width = 40
mybutton(i).Height = 20
mybutton(i).FlatStyle = FlatStyle.Flat
mybutton(i).BackColor = Drawing.Color.AntiqueWhite
mybutton(i).Location = New System.Drawing.Point(start, top)
mybutton(i).Text = (i + 1).ToString
mybutton(i).Cursor = Cursors.Hand
Me.Controls.Add(mybutton(i))
AddHandler mybutton(i).Click, AddressOf mybutton_Click
AddHandler mybutton(i).MouseHover, AddressOf mybutton_Hoover
AddHandler mybutton(i).MouseLeave, AddressOf mybutton_Leave

'here you see those handlers added

start = start + 40
If (i + 1) Mod 5 = 0 Then
top = top + 20
start = 4
End If
Next
End Sub

Private Sub mybutton_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)

Dim thisbutton As Button = DirectCast(sender, Button)
MessageBox.Show("The day is: " & thisbutton.Text)
End Sub
Private Sub mybutton_Hoover _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AliceBlue
End Sub
Private Sub mybutton_Leave _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AntiqueWhite
End Sub

I would say, try it, you can only learn from it I think

Cor

Nov 20 '05 #14
tks, I will play with that code later. Right now I have a deadline for my
project and I am stuck again, in my Random sub. I always do this. That is,
set my goals way too high.

P
Nov 20 '05 #15
tks, I will play with that code later. Right now I have a deadline for my
project and I am stuck again, in my Random sub. I always do this. That is,
set my goals way too high.

P
Nov 20 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jeffrey A. Voigt | last post by:
Can someone take a quick glace at my code and tell me why my AutoPostBackHandler function does not get fired off at all? What I'm trying to do is get all of the Buttons and DropDownList controls...
7
by: UGH | last post by:
I am adding image buttons dynamically and I need to add event handler when the user clicks on one of those image buttons which will have different id for reports. Here is my code LnkImage =...
2
by: Joe | last post by:
I have a datgrid with a button column that contains link buttons. I have added code to the ItemDataBound event of the datagrid that adds a handler to each link button. Here is the code: ...
8
by: Paul Mars | last post by:
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...
12
by: Tom | last post by:
I use dynamically created controls all the time. I.E. I create the control in code then use AddHandler to add the necessary delegates for processing (like Click, etc). Does one have to call...
8
by: John Austin | last post by:
I need to understand why if I add a control and use AddHandler to connect its click event, it will work in Page_Load, but not in a Button_Click. The idea is that the user types some data, presses...
15
by: Nathan Sokalski | last post by:
I have a section of my code that dynamically creates LinkButtons to allow the user to go to the page containing a question they have not answered. The code that creates the LinkButton is called, as...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.