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. 15 3993
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, 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
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
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
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
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.
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.
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
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
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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 =...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |