472,958 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 3972
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.