addhandler and creating buttons at run time

P

Paul Mars

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.
 
C

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
 
P

Paul Mars

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.
 
S

Scott

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
 
P

Paul Mars

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.
 
C

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
 
P

Paul Mars

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
 
C

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
 
P

Paul Mars

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top