Know which dynamically added control was clicked

  • Thread starter Thread starter Charles May
  • Start date Start date
C

Charles May

I have a form that creates a button for each employee in the database. I
have added the click handler and have this working. The buttons contain the
employee's name for their respective .Text and I have included a number in
the tag field. My problem is now, finding out which button is being clicked
so that I can pull the information from the database.

How can I get the tag value of the button clicked?

Also... what type of information can I store in the tag field of the
control? I would like to store the EmployeeID (which is generated by
MSAccess Autonumber) so that when the button is clicked, I can query the
database to get the information needed.

Thanks

Charlie
 
I have a form that creates a button for each employee in the database. I
have added the click handler and have this working. The buttons contain the
employee's name for their respective .Text and I have included a number in
the tag field. My problem is now, finding out which button is being clicked
so that I can pull the information from the database.

How can I get the tag value of the button clicked?

Also... what type of information can I store in the tag field of the
control? I would like to store the EmployeeID (which is generated by
MSAccess Autonumber) so that when the button is clicked, I can query the
database to get the information needed.

In the Button Click event hander add a line:

Dim btn as Button = sender

Then you can reference all properties of the button that signalled the
event with the btn variable, as in btn.Tag.
 
Thanks Zacks

I knew it had something to do with sender but I couldn't figure it out.

Now you might be able to answer this for me.

Right now, I have a button on the form and when I click the button the new
buttons are created and added.

In my code I have the following

AddHandler btn.Click, AddressOf btn_Click

What I would like to do is generate everything while the frmSplash is up.
The only thing I am having a problem with is creating the handler on one
form to be used on another.

so frmSplash is displayed at startup and querys the database, then from
there creats the buttons on frmMain and creates the handler for the
btn_Click to work on frmMain when it is displayed.

Any ideas?

Thanks for all your help
Charlie

I have a form that creates a button for each employee in the database. I
have added the click handler and have this working. The buttons contain
the
employee's name for their respective .Text and I have included a number in
the tag field. My problem is now, finding out which button is being
clicked
so that I can pull the information from the database.

How can I get the tag value of the button clicked?

Also... what type of information can I store in the tag field of the
control? I would like to store the EmployeeID (which is generated by
MSAccess Autonumber) so that when the button is clicked, I can query the
database to get the information needed.

In the Button Click event hander add a line:

Dim btn as Button = sender

Then you can reference all properties of the button that signalled the
event with the btn variable, as in btn.Tag.
 
so frmSplash is displayed at startup and querys the database, then from
there creats the buttons on frmMain and creates the handler for the
btn_Click to work on frmMain when it is displayed.

frmSplash should not be querying the database. frmSplash should just
be informational only. Basically, in frmMain, you should show
frmSplash, then query the database and set up your buttons, finally
hide frmSplash and show frmMain. In other words, frmMain should be
the one that queries the database, creates the buttons, sets up the
event handlers for the buttons.

Chris
 
frmSplash should not be querying the database.  frmSplash should just
be informational only.  Basically, in frmMain, you should show
frmSplash, then query the database and set up your buttons, finally
hide frmSplash and show frmMain.   In other words, frmMain should be
the one that queries the database, creates the buttons, sets up the
event handlers for the buttons.

I agree with Chris. Just close the spash form in the main form's load
event after it finishes creating all of the dynamic controls. The main
form's load event completes before the main form is actually shown.
 
Thanks Chris,
That's actually what I ended up doing. It was simpler and more easy for
following the code.

Thanks for the input though

Charlie
 
Back
Top